Skip to content

Commit

Permalink
fix: use name of level instead of label for logging prefixes
Browse files Browse the repository at this point in the history
  • Loading branch information
lukekarrys committed Apr 24, 2024
1 parent 7f4e667 commit b48958a
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 15 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
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 b48958a

Please sign in to comment.