Skip to content

Commit c19e483

Browse files
committed
chore: simplify logger initialization
- Move from "log" level to "info" level - Use "silent" level in testing environment
1 parent 23e2aef commit c19e483

File tree

5 files changed

+18
-32
lines changed

5 files changed

+18
-32
lines changed

src/cli/checks.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ async function listCheckCommand (knex, options = {}) {
99
const checks = await getAllComplianceChecks(knex)
1010
const implementedChecks = checks.filter(check => check.implementation_status === 'completed')
1111
implementedChecks.forEach(check => {
12-
logger.log(`- ${check.code_name}: ${check.title}`)
12+
logger.info(`- ${check.code_name}: ${check.title}`)
1313
})
14-
logger.log('------------------------------------')
15-
logger.log(`Implemented checks: ${implementedChecks.length}/${checks.length}.`)
14+
logger.info('------------------------------------')
15+
logger.info(`Implemented checks: ${implementedChecks.length}/${checks.length}.`)
1616
return implementedChecks
1717
}
1818

src/cli/workflows.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ const commandList = [{
2424
const validCommandNames = commandList.map(({ name }) => name)
2525

2626
function listWorkflowCommand (options = {}) {
27-
logger.log('Available workflows: \n')
28-
commandList.forEach(({ name, description }) => logger.log(`- ${name}: ${description}`))
27+
logger.info('Available workflows:')
28+
commandList.forEach(({ name, description }) => logger.info(`- ${name}: ${description}`))
2929
return commandList
3030
}
3131

src/providers/index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ const fetchRepoByFullName = async (fullName) => {
6060

6161
const performScorecardAnalysis = async (repo) => {
6262
ensureGithubToken()
63-
logger.log(`Running OSSF Scorecard for repository (${repo.full_name})...`)
63+
logger.info(`Running OSSF Scorecard for repository (${repo.full_name})...`)
6464

6565
const start = new Date().getTime()
6666
const { stdout, stderr } = await exec(`docker run -e GITHUB_AUTH_TOKEN=${process.env.GITHUB_TOKEN} --rm ${ossfScorecardSettings.dockerImage} --repo=${repo.html_url} --show-details --format=json`)
@@ -70,7 +70,7 @@ const performScorecardAnalysis = async (repo) => {
7070
const data = JSON.parse(stdout)
7171
const end = new Date().getTime()
7272
data.analysis_execution_time = (end - start)
73-
logger.log(`OSSF Scorecard finished successfully for repository (${repo.full_name}) in ${data.analysis_execution_time / 1000}s`)
73+
logger.info(`OSSF Scorecard finished successfully for repository (${repo.full_name}) in ${data.analysis_execution_time / 1000}s`)
7474
return data
7575
}
7676

src/utils/index.js

+3-17
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
const { add, parseISO, isBefore } = require('date-fns')
22
const isURL = require('validator/lib/isURL.js')
3-
const pino = require('pino')({
3+
const pinoInit = require('pino')
4+
const logger = pinoInit({
45
transport: {
56
target: 'pino-pretty',
67
options: {
78
ignore: 'pid,hostname'
89
}
910
},
10-
level: process.env.PINO_LOG_LEVEL || 'trace'
11+
level: process.env.NODE_ENV === 'test' ? 'silent' : 'info'
1112
})
1213

1314
const validateGithubUrl = (url) => isURL(url, { protocols: ['https'], require_protocol: true }) && url.includes('github.com')
@@ -18,21 +19,6 @@ const ensureGithubToken = () => {
1819
}
1920
}
2021

21-
const defineLog = (type) => function () {
22-
if (process.env.NODE_ENV === 'test') {
23-
return () => {}
24-
}
25-
26-
return pino[type](...arguments)
27-
}
28-
29-
const logger = {
30-
info: defineLog('info'),
31-
error: defineLog('error'),
32-
warn: defineLog('warn'),
33-
log: defineLog('trace')
34-
}
35-
3622
const getSeverityFromPriorityGroup = (priorityGroup) => {
3723
const priorityType = priorityGroup[0]
3824
const priorityValue = parseInt(priorityGroup.slice(1), 10)

src/workflows/index.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ const updateGithubOrgs = async (knex) => {
2525
debug('Updating organization in database')
2626
await updateGithubOrganization(mappedData)
2727
}))
28-
logger.log('GitHub organizations updated successfully')
28+
logger.info('GitHub organizations updated successfully')
2929
}
3030

3131
const upsertGithubRepositories = async (knex) => {
@@ -65,12 +65,12 @@ const runAllTheComplianceChecks = async (knex) => {
6565
debug('Fetching all compliance checks')
6666
const complianceChecks = await getAllComplianceChecks()
6767
const implementedChecks = complianceChecks.filter(check => check.implementation_status === 'completed')
68-
logger.log('Running all implemented checks sequentially')
68+
logger.info('Running all implemented checks sequentially')
6969
for (const check of implementedChecks) {
70-
logger.log(`Running check (${check.code_name})`)
70+
logger.info(`Running check (${check.code_name})`)
7171
await checks[check.code_name](knex)
7272
}
73-
logger.log('All checks ran successfully')
73+
logger.info('All checks ran successfully')
7474
}
7575

7676
const upsertOSSFScorecardAnalysis = async (knex) => {
@@ -80,12 +80,12 @@ const upsertOSSFScorecardAnalysis = async (knex) => {
8080
if (repositories.length === 0) {
8181
throw new Error('No repositories found. Please add repositories before running this workflow.')
8282
}
83-
logger.log('Running OSSF Scorecard for all repositories')
84-
logger.log('This may take a while...')
83+
logger.info('Running OSSF Scorecard for all repositories')
84+
logger.info('This may take a while...')
8585
const chunks = chunkArray(repositories, ossfScorecardSettings.parallelJobs)
8686
for (let i = 0; i < chunks.length; i++) {
8787
const chunk = chunks[i]
88-
logger.log(`Processing chunk ${i + 1} of ${chunks.length} including ${chunk.length} repositories`)
88+
logger.info(`Processing chunk ${i + 1} of ${chunks.length} including ${chunk.length} repositories`)
8989
await Promise.all(chunk.map(async (repo) => {
9090
debug(`Running OSSF Scorecard for repository (${repo.full_name})`)
9191
try {
@@ -102,7 +102,7 @@ const upsertOSSFScorecardAnalysis = async (knex) => {
102102
}))
103103
}
104104

105-
logger.log('The OSSF Scorecard ran successfully')
105+
logger.info('The OSSF Scorecard ran successfully')
106106
}
107107

108108
module.exports = {

0 commit comments

Comments
 (0)