Skip to content

Commit 50dc320

Browse files
committed
Add namespaces to debug logs
1 parent 824e598 commit 50dc320

35 files changed

+201
-142
lines changed

src/cli.mts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import lookupRegistryAuthToken from 'registry-auth-token'
88
import lookupRegistryUrl from 'registry-url'
99
import updateNotifier from 'tiny-updater'
1010

11-
import { debugFn, debugLog } from '@socketsecurity/registry/lib/debug'
11+
import { debugDir, debugFn } from '@socketsecurity/registry/lib/debug'
1212
import { logger } from '@socketsecurity/registry/lib/logger'
1313

1414
import { cmdAnalytics } from './commands/analytics/cmd-analytics.mts'
@@ -186,8 +186,8 @@ void (async () => {
186186
)
187187
} catch (e) {
188188
process.exitCode = 1
189-
debugFn('Uncaught error (BAD!):')
190-
debugFn(e)
189+
debugFn('error', 'Uncaught error (BAD!):')
190+
debugDir('inspect', { error: e })
191191

192192
// Try to parse the flags, find out if --json or --markdown is set.
193193
let isJson = false
@@ -231,8 +231,7 @@ void (async () => {
231231
logger.error('\n') // Any-spinner-newline
232232
logger.fail(failMsgWithBadge(errorTitle, errorMessage))
233233
if (errorBody) {
234-
// Explicitly use debugLog here.
235-
debugLog(errorBody)
234+
debugDir('inspect', { errorBody })
236235
}
237236
}
238237

src/commands/audit-log/output-audit-log.mts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { createRequire } from 'node:module'
22

3-
import { debugFn } from '@socketsecurity/registry/lib/debug'
3+
import { debugDir, debugFn } from '@socketsecurity/registry/lib/debug'
44
import { logger } from '@socketsecurity/registry/lib/logger'
55

66
import constants from '../../constants.mts'
@@ -182,7 +182,8 @@ ${table}
182182
logger.fail(
183183
'There was a problem converting the logs to Markdown, please try the `--json` flag',
184184
)
185-
debugFn('catch: unexpected\n', e)
185+
debugFn('error', 'caught: unexpected error')
186+
debugDir('inspect', { error: e })
186187
return 'Failed to generate the markdown report'
187188
}
188189
}

src/commands/ci/fetch-default-org-slug.mts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export async function getDefaultOrgSlug(): Promise<CResult<string>> {
1111
const defaultOrgResult = getConfigValueOrUndef('defaultOrg')
1212

1313
if (defaultOrgResult) {
14-
debugFn('use: default org', defaultOrgResult)
14+
debugFn('notice', 'use: default org', defaultOrgResult)
1515
return { ok: true, data: defaultOrgResult }
1616
}
1717

@@ -51,7 +51,7 @@ export async function getDefaultOrgSlug(): Promise<CResult<string>> {
5151
}
5252
}
5353

54-
debugFn('resolve: org', slug)
54+
debugFn('notice', 'resolve: org', slug)
5555

5656
return {
5757
ok: true,

src/commands/fix/agent-fix.mts

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import semver from 'semver'
55

66
import { getManifestData } from '@socketsecurity/registry'
77
import { arrayUnique } from '@socketsecurity/registry/lib/arrays'
8-
import { debugFn, isDebug } from '@socketsecurity/registry/lib/debug'
8+
import { debugDir, debugFn, isDebug } from '@socketsecurity/registry/lib/debug'
99
import { logger } from '@socketsecurity/registry/lib/logger'
1010
import { runNpmScript } from '@socketsecurity/registry/lib/npm'
1111
import {
@@ -134,15 +134,15 @@ export async function agentFix(
134134
spinner?.stop()
135135
logger.info('No fixable vulns found.')
136136
if (alertsMap.size) {
137-
debugFn('inspect:', { alertsMap })
137+
debugDir('inspect', { alertsMap })
138138
} else {
139-
debugFn('inspect: { alertsMap: Map(0) {} }')
139+
debugFn('inspect', '{ alertsMap: Map(0) {} }')
140140
}
141141
return { ok: true, data: { fixed: false } }
142142
}
143143

144-
if (isDebug()) {
145-
debugFn('found: cves for', Array.from(infoByPartialPurl.keys()))
144+
if (isDebug('notice')) {
145+
debugFn('notice', 'found: cves for', Array.from(infoByPartialPurl.keys()))
146146
}
147147

148148
// Lazily access constants.packumentCache.
@@ -198,7 +198,7 @@ export async function agentFix(
198198
spinner?.indent()
199199

200200
if (getManifestData(partialPurlObj.type, name)) {
201-
debugFn(`found: Socket Optimize variant for ${name}`)
201+
debugFn('notice', `found: Socket Optimize variant for ${name}`)
202202
}
203203
// eslint-disable-next-line no-await-in-loop
204204
const packument = await fetchPackagePackument(name)
@@ -266,7 +266,7 @@ export async function agentFix(
266266
)
267267

268268
if (!oldVersions.length) {
269-
debugFn(`skip: ${name} not found\n`)
269+
debugFn('notice', `skip: ${name} not found\n`)
270270
// Skip to next package.
271271
cleanupInfoEntriesLoop()
272272
continue infoEntriesLoop
@@ -283,7 +283,7 @@ export async function agentFix(
283283
let hasAnnouncedWorkspace = false
284284
let workspaceLogCallCount = logger.logCallCount
285285
if (isDebug()) {
286-
debugFn(`check: workspace ${workspace}`)
286+
debugFn('notice', `check: workspace ${workspace}`)
287287
hasAnnouncedWorkspace = true
288288
workspaceLogCallCount = logger.logCallCount
289289
}
@@ -294,7 +294,7 @@ export async function agentFix(
294294

295295
const node = findPackageNode(actualTree, name, oldVersion)
296296
if (!node) {
297-
debugFn(`skip: ${oldId} not found`)
297+
debugFn('notice', `skip: ${oldId} not found`)
298298
continue oldVersionsLoop
299299
}
300300
infosLoop: for (const {
@@ -319,7 +319,7 @@ export async function agentFix(
319319
continue infosLoop
320320
}
321321
if (semver.gte(oldVersion, newVersion)) {
322-
debugFn(`skip: ${oldId} is >= ${newVersion}`)
322+
debugFn('notice', `skip: ${oldId} is >= ${newVersion}`)
323323
continue infosLoop
324324
}
325325
if (
@@ -328,7 +328,7 @@ export async function agentFix(
328328
b.workspace === branchWorkspace && b.newVersion === newVersion,
329329
)
330330
) {
331-
debugFn(`skip: open PR found for ${name}@${newVersion}`)
331+
debugFn('notice', `skip: open PR found for ${name}@${newVersion}`)
332332
if (++count >= limit) {
333333
cleanupInfoEntriesLoop()
334334
break infoEntriesLoop
@@ -366,7 +366,7 @@ export async function agentFix(
366366
)
367367
// eslint-disable-next-line no-await-in-loop
368368
if (!(await editablePkgJson.save({ ignoreWhitespace: true }))) {
369-
debugFn(`skip: ${workspace}/package.json unchanged`)
369+
debugFn('notice', `skip: ${workspace}/package.json unchanged`)
370370
// Reset things just in case.
371371
if (ciEnv) {
372372
// eslint-disable-next-line no-await-in-loop
@@ -461,12 +461,12 @@ export async function agentFix(
461461
)
462462
) {
463463
skipPr = true
464-
debugFn(`skip: branch "${branch}" exists`)
464+
debugFn('notice', `skip: branch "${branch}" exists`)
465465
}
466466
// eslint-disable-next-line no-await-in-loop
467467
else if (await gitRemoteBranchExists(branch, cwd)) {
468468
skipPr = true
469-
debugFn(`skip: remote branch "${branch}" exists`)
469+
debugFn('notice', `skip: remote branch "${branch}" exists`)
470470
} else if (
471471
// eslint-disable-next-line no-await-in-loop
472472
!(await gitCreateAndPushBranch(
@@ -613,8 +613,7 @@ export async function agentFix(
613613
cause: `Update failed for ${oldId} in ${workspace}${error ? '; ' + error : ''}`,
614614
}
615615
}
616-
debugFn('name:', name)
617-
debugFn('increment: count', count + 1)
616+
debugFn('notice', 'increment: count', count + 1)
618617
if (++count >= limit) {
619618
cleanupInfoEntriesLoop()
620619
break infoEntriesLoop

src/commands/fix/fix-branch-helpers.mts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,16 @@ export function getActiveBranchesForPackage(
3535
}
3636
}
3737

38-
if (isDebug()) {
38+
if (isDebug('notice')) {
3939
const fullName = resolvePackageName(partialPurlObj)
4040
if (activeBranches.length) {
4141
debugFn(
42+
'notice',
4243
`found: ${activeBranches.length} active branches for ${fullName}\n`,
4344
activeBranches,
4445
)
4546
} else if (openPrs.length) {
46-
debugFn(`miss: 0 active branches found for ${fullName}`)
47+
debugFn('notice', `miss: 0 active branches found for ${fullName}`)
4748
}
4849
}
4950

src/commands/fix/fix-env-helpers.mts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ async function getEnvRepoInfo(
1717
// Lazily access constants.ENV.GITHUB_REPOSITORY.
1818
const { GITHUB_REPOSITORY } = constants.ENV
1919
if (!GITHUB_REPOSITORY) {
20-
debugFn('miss: GITHUB_REPOSITORY env var')
20+
debugFn('notice', 'miss: GITHUB_REPOSITORY env var')
2121
}
2222
const ownerSlashRepo = GITHUB_REPOSITORY
2323
const slashIndex = ownerSlashRepo.indexOf('/')

src/commands/fix/git.mts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import semver from 'semver'
22

33
import { PackageURL } from '@socketregistry/packageurl-js'
4-
import { debugFn } from '@socketsecurity/registry/lib/debug'
4+
import { debugDir, debugFn } from '@socketsecurity/registry/lib/debug'
55
import { normalizePath } from '@socketsecurity/registry/lib/path'
66
import { escapeRegExp } from '@socketsecurity/registry/lib/regexps'
77
import { spawn } from '@socketsecurity/registry/lib/spawn'
@@ -246,7 +246,8 @@ export async function gitCreateAndPushBranch(
246246
return true
247247
} catch (e) {
248248
debugFn(
249-
`catch: git push --force --set-upstream origin ${branch} failed\n`,
249+
'error',
250+
`caught: git push --force --set-upstream origin ${branch} failed\n`,
250251
e,
251252
)
252253
}
@@ -284,9 +285,11 @@ export async function gitRepoInfo(
284285
return { owner, repo }
285286
}
286287
} catch {}
287-
debugFn('git: unmatched git remote URL format', remoteUrl)
288+
debugFn('error', 'git: unmatched git remote URL format')
289+
debugDir('inspect', { remoteUrl })
288290
} catch (e) {
289-
debugFn('catch: git remote get-url origin failed\n', e)
291+
debugFn('error', 'caught: git remote get-url origin failed')
292+
debugDir('inspect', { error: e })
290293
}
291294
return null
292295
}
@@ -315,7 +318,8 @@ export async function gitEnsureIdentity(
315318
try {
316319
await spawn('git', ['config', prop, value], stdioIgnoreOptions)
317320
} catch (e) {
318-
debugFn(`catch: git config ${prop} ${value} failed\n`, e)
321+
debugFn('error', `caught: git config ${prop} ${value} failed`)
322+
debugDir('inspect', { error: e })
319323
}
320324
}
321325
}),
@@ -373,7 +377,8 @@ export async function gitUnstagedModifiedFiles(
373377
data: rawRelPaths.map(relPath => normalizePath(relPath)),
374378
}
375379
} catch (e) {
376-
debugFn('catch: git diff --name-only failed\n', e)
380+
debugFn('error', 'caught: git diff --name-only failed')
381+
debugDir('inspect', { error: e })
377382

378383
return {
379384
ok: false,

src/commands/fix/handle-fix.mts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { debugFn } from '@socketsecurity/registry/lib/debug'
1+
import { debugDir, debugFn } from '@socketsecurity/registry/lib/debug'
22
import { logger } from '@socketsecurity/registry/lib/logger'
33
import { pluralize } from '@socketsecurity/registry/lib/words'
44

@@ -59,7 +59,8 @@ export async function handleFix({
5959
)
6060
ghsasCount = ghsas.length
6161
} else {
62-
debugFn('coana fail:', {
62+
debugFn('error', 'fail: Coana CLI')
63+
debugDir('inspect', {
6364
message: autoCResult.message,
6465
cause: autoCResult.cause,
6566
})
@@ -87,7 +88,8 @@ export async function handleFix({
8788
spinner?.stop()
8889

8990
if (!applyFixesCResult.ok) {
90-
debugFn('coana fail:', {
91+
debugFn('error', 'fail: Coana CLI')
92+
debugDir('inspect', {
9193
message: applyFixesCResult.message,
9294
cause: applyFixesCResult.cause,
9395
})

src/commands/fix/npm-fix.mts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { debugFn, isDebug } from '@socketsecurity/registry/lib/debug'
1+
import { debugDir, debugFn, isDebug } from '@socketsecurity/registry/lib/debug'
22

33
import { agentFix } from './agent-fix.mts'
44
import { getCiEnv, getOpenPrsForEnvironment } from './fix-env-helpers.mts'
@@ -77,7 +77,8 @@ export async function npmFix(
7777
}
7878
} catch (e) {
7979
spinner?.stop()
80-
debugFn('catch: PURL API\n', e)
80+
debugFn('error', 'caught: PURL API')
81+
debugDir('inspect', { error: e })
8182
return {
8283
ok: false,
8384
message: 'API Error',

src/commands/fix/open-pr.mts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { Octokit } from '@octokit/rest'
1010
import semver from 'semver'
1111

1212
import { PackageURL } from '@socketregistry/packageurl-js'
13-
import { debugFn } from '@socketsecurity/registry/lib/debug'
13+
import { debugDir, debugFn } from '@socketsecurity/registry/lib/debug'
1414
import { readJson, writeJson } from '@socketsecurity/registry/lib/fs'
1515
import { spawn } from '@socketsecurity/registry/lib/spawn'
1616
import { isNonEmptyString } from '@socketsecurity/registry/lib/strings'
@@ -37,7 +37,7 @@ function getOctokit() {
3737
// Lazily access constants.ENV.SOCKET_CLI_GITHUB_TOKEN.
3838
const { SOCKET_CLI_GITHUB_TOKEN } = constants.ENV
3939
if (!SOCKET_CLI_GITHUB_TOKEN) {
40-
debugFn('miss: SOCKET_CLI_GITHUB_TOKEN env var')
40+
debugFn('notice', 'miss: SOCKET_CLI_GITHUB_TOKEN env var')
4141
}
4242
_octokit = new Octokit({
4343
auth: SOCKET_CLI_GITHUB_TOKEN,
@@ -52,7 +52,7 @@ export function getOctokitGraphql(): typeof OctokitGraphql {
5252
// Lazily access constants.ENV.SOCKET_CLI_GITHUB_TOKEN.
5353
const { SOCKET_CLI_GITHUB_TOKEN } = constants.ENV
5454
if (!SOCKET_CLI_GITHUB_TOKEN) {
55-
debugFn('miss: SOCKET_CLI_GITHUB_TOKEN env var')
55+
debugFn('notice', 'miss: SOCKET_CLI_GITHUB_TOKEN env var')
5656
}
5757
_octokitGraphql = OctokitGraphql.defaults({
5858
headers: {
@@ -171,14 +171,15 @@ export async function cleanupOpenPrs(
171171
pull_number: prNum,
172172
state: 'closed',
173173
})
174-
debugFn(`close: ${prRef} for ${prToVersion}`)
174+
debugFn('notice', `close: ${prRef} for ${prToVersion}`)
175175
// Remove entry from parent object.
176176
context.parent.splice(context.index, 1)
177177
// Mark cache to be saved.
178178
cachesToSave.set(context.cacheKey, context.data)
179179
return null
180180
} catch (e) {
181181
debugFn(
182+
'error',
182183
`fail: close ${prRef} for ${prToVersion}\n`,
183184
(e as Error)?.message || 'unknown error',
184185
)
@@ -194,7 +195,7 @@ export async function cleanupOpenPrs(
194195
base: match.headRefName,
195196
head: match.baseRefName,
196197
})
197-
debugFn('update: stale', prRef)
198+
debugFn('notice', 'update: stale', prRef)
198199
// Update entry entry.
199200
if (context.apiType === 'graphql') {
200201
context.entry.mergeStateStatus = 'CLEAN'
@@ -205,7 +206,7 @@ export async function cleanupOpenPrs(
205206
cachesToSave.set(context.cacheKey, context.data)
206207
} catch (e) {
207208
const message = (e as Error)?.message || 'Unknown error'
208-
debugFn(`fail: update ${prRef} - ${message}`)
209+
debugFn('error', `fail: update ${prRef} - ${message}`)
209210
}
210211
}
211212
return match
@@ -477,7 +478,7 @@ export async function openPr(
477478
.join('\n')
478479
message += `:\n${details}`
479480
}
480-
debugFn(message)
481+
debugFn('error', message)
481482
}
482483
return null
483484
}
@@ -512,6 +513,7 @@ export async function setGitRemoteGithubRepoUrl(
512513
try {
513514
await spawn('git', ['remote', 'set-url', 'origin', url], stdioIgnoreOptions)
514515
} catch (e) {
515-
debugFn('catch: unexpected\n', e)
516+
debugFn('error', 'caught: unexpected error')
517+
debugDir('inspect', { error: e })
516518
}
517519
}

0 commit comments

Comments
 (0)