Skip to content

Commit

Permalink
fix: use name of level instead of label for logging prefixes (#7414)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukekarrys authored Apr 24, 2024
1 parent 7f4e667 commit 0708b3b
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 40 deletions.
6 changes: 1 addition & 5 deletions lib/utils/display.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,9 @@ const LEVEL_OPTIONS = {
},
error: {
index: 1,
label: 'ERR!',
},
warn: {
index: 2,
label: 'WARN',
},
notice: {
index: 3,
Expand All @@ -55,11 +53,9 @@ const LEVEL_OPTIONS = {
},
verbose: {
index: 6,
label: 'verb',
},
silly: {
index: 7,
label: 'sill',
},
}

Expand Down Expand Up @@ -338,7 +334,7 @@ class Display {
const title = args.shift()
const prefix = [
this.#logColors.heading(this.#heading),
this.#logColors[level](levelOpts.label ?? level),
this.#logColors[level](level),
title ? this.#logColors.title(title) : null,
]
this.#stderrWrite({ prefix }, ...args)
Expand Down
50 changes: 25 additions & 25 deletions smoke-tests/tap-snapshots/test/index.js.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -43,31 +43,31 @@ npm {NPM}
`

exports[`test/index.js TAP basic npm ci > should throw mismatch deps in lock file error 1`] = `
npm ERR! code EUSAGE
npm ERR!
npm ERR! \`npm ci\` can only install packages when your package.json and package-lock.json or npm-shrinkwrap.json are in sync. Please update your lock file with \`npm install\` before continuing.
npm ERR!
npm ERR! Invalid: lock file's abbrev@1.0.4 does not satisfy abbrev@1.1.1
npm ERR!
npm ERR! Clean install a project
npm ERR!
npm ERR! Usage:
npm ERR! npm ci
npm ERR!
npm ERR! Options:
npm ERR! [--install-strategy <hoisted|nested|shallow|linked>] [--legacy-bundling]
npm ERR! [--global-style] [--omit <dev|optional|peer> [--omit <dev|optional|peer> ...]]
npm ERR! [--include <prod|dev|optional|peer> [--include <prod|dev|optional|peer> ...]]
npm ERR! [--strict-peer-deps] [--foreground-scripts] [--ignore-scripts] [--no-audit]
npm ERR! [--no-bin-links] [--no-fund] [--dry-run]
npm ERR! [-w|--workspace <workspace-name> [-w|--workspace <workspace-name> ...]]
npm ERR! [-ws|--workspaces] [--include-workspace-root] [--install-links]
npm ERR!
npm ERR! aliases: clean-install, ic, install-clean, isntall-clean
npm ERR!
npm ERR! Run "npm help ci" for more info
npm ERR! A complete log of this run can be found in: {NPM}/{TESTDIR}/cache/_logs/{LOG}
npm error code EUSAGE
npm error
npm error \`npm ci\` can only install packages when your package.json and package-lock.json or npm-shrinkwrap.json are in sync. Please update your lock file with \`npm install\` before continuing.
npm error
npm error Invalid: lock file's abbrev@1.0.4 does not satisfy abbrev@1.1.1
npm error
npm error Clean install a project
npm error
npm error Usage:
npm error npm ci
npm error
npm error Options:
npm error [--install-strategy <hoisted|nested|shallow|linked>] [--legacy-bundling]
npm error [--global-style] [--omit <dev|optional|peer> [--omit <dev|optional|peer> ...]]
npm error [--include <prod|dev|optional|peer> [--include <prod|dev|optional|peer> ...]]
npm error [--strict-peer-deps] [--foreground-scripts] [--ignore-scripts] [--no-audit]
npm error [--no-bin-links] [--no-fund] [--dry-run]
npm error [-w|--workspace <workspace-name> [-w|--workspace <workspace-name> ...]]
npm error [-ws|--workspaces] [--include-workspace-root] [--install-links]
npm error
npm error aliases: clean-install, ic, install-clean, isntall-clean
npm error
npm error Run "npm help ci" for more info
npm error A complete log of this run can be found in: {NPM}/{TESTDIR}/cache/_logs/{LOG}
`

exports[`test/index.js TAP basic npm diff > should have expected diff output 1`] = `
Expand Down
14 changes: 4 additions & 10 deletions test/fixtures/mock-logs.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
const { log: { LEVELS } } = require('proc-log')
const { stripVTControlCharacters: stripAnsi } = require('util')

const labels = new Map([
['error', 'ERR!'],
['warn', 'WARN'],
['verbose', 'verb'],
['silly', 'sill'],
].reduce((acc, v) => acc.concat([v, v.slice(0).reverse()]), []))
const logPrefix = new RegExp(`^npm (${LEVELS.map(l => labels.get(l) ?? l).join('|')})\\s`)
const logPrefix = new RegExp(`^npm (${LEVELS.join('|')})\\s`)
const isLog = (str) => logPrefix.test(stripAnsi(str))

// We only strip trailing newlines since some output will
Expand Down Expand Up @@ -63,15 +57,15 @@ module.exports = () => {
// Split on spaces for the heading and level/label. We know that
// none of those have spaces but could be colorized so there's no
// other good way to get each of those including control chars
const [rawHeading, rawLabel] = str.split(' ')
const rawPrefix = `${rawHeading} ${rawLabel} `
const [rawHeading, rawLevel] = str.split(' ')
const rawPrefix = `${rawHeading} ${rawLevel} `
// If message is colorized we can just replaceAll with the string since
// it will be unique due to control chars. Otherwise we create a regex
// that will only match the beginning of each line.
const prefix = stripAnsi(str) !== str ? rawPrefix : new RegExp(`^${rawPrefix}`, 'gm')

// The level needs color stripped always because we use it to filter logs
const level = labels.get(stripAnsi(rawLabel)) ?? stripAnsi(rawLabel)
const level = stripAnsi(rawLevel)

logs.push(str.replaceAll(prefix, `${level} `))
levelLogs.push({ level, message: str.replaceAll(prefix, '') })
Expand Down

0 comments on commit 0708b3b

Please sign in to comment.