Skip to content

Commit 1b01daa

Browse files
authored
fix: respect no color for inspected values (#133)
1 parent 3ced69f commit 1b01daa

4 files changed

Lines changed: 37 additions & 5 deletions

File tree

packages/sev-logger/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# @transloadit/sev-logger
22

3+
## 1.0.3
4+
5+
### Patch Changes
6+
7+
- Respect `NO_COLOR=1` when formatting inspected object values.
8+
39
## 1.0.2
410

511
### Patch Changes

packages/sev-logger/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@transloadit/sev-logger",
3-
"version": "1.0.2",
3+
"version": "1.0.3",
44
"repository": {
55
"type": "git",
66
"url": "git://github.com/transloadit/monolib.git",

packages/sev-logger/src/SevLogger.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,28 @@ describe('SevLogger', () => {
360360
assert.match(output, /notice smoke ok/)
361361
assert.match(output, /SevLogger\.test\.(js|ts):\d+/)
362362
})
363+
364+
it('respects NO_COLOR for inspected objects', () => {
365+
const previousNoColor = process.env.NO_COLOR
366+
process.env.NO_COLOR = '1'
367+
try {
368+
const logger = new SevLogger({
369+
level: LEVEL.TRACE,
370+
addCallsite: false,
371+
})
372+
373+
assert.strictEqual(
374+
logger.formatter(LEVEL.NOTICE, { foo: 'bar' }),
375+
"[ NOTICE] { foo: 'bar' }",
376+
)
377+
} finally {
378+
if (previousNoColor === undefined) {
379+
delete process.env.NO_COLOR
380+
} else {
381+
process.env.NO_COLOR = previousNoColor
382+
}
383+
}
384+
})
363385
})
364386

365387
describe('announceMotd', () => {

packages/sev-logger/src/SevLogger.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,10 @@ export class SevLogger {
412412
this.reset(params)
413413
}
414414

415+
#inspect(value: unknown): string {
416+
return inspect(value, false, null, process.env.NO_COLOR !== '1' && !this.filepath)
417+
}
418+
415419
colorByName(name: string) {
416420
const absCrc = SevLogger.#crc32(name)
417421

@@ -923,7 +927,7 @@ export class SevLogger {
923927
const base = workingMessages.shift()
924928
if (typeof base !== 'string') {
925929
// Should not happen due to initial check, but safeguard
926-
subject = inspect(base, false, null, true)
930+
subject = this.#inspect(base)
927931
} else {
928932
// Perform replacement - Type errors inside callback WILL throw
929933
subject = base.replace(
@@ -947,7 +951,7 @@ export class SevLogger {
947951
if (typeof arg === 'string' || typeof arg === 'number') {
948952
return match.replace('%s', fmtColorFunc(String(arg)))
949953
}
950-
return match.replace('%s', inspect(arg, false, null, true)) // Use inspect for non-string/num %s
954+
return match.replace('%s', this.#inspect(arg)) // Use inspect for non-string/num %s
951955
}
952956
if (match.includes('%r')) {
953957
if (typeof arg !== 'string') {
@@ -982,15 +986,15 @@ export class SevLogger {
982986
console.error(`SevLogger Formatting Warning: ${errorMsg}`, originalMessages)
983987
// Fallback: Use original messages inspected
984988
subject = originalMessages
985-
.map((msg) => (typeof msg === 'string' ? msg : inspect(msg, false, null, true)))
989+
.map((msg) => (typeof msg === 'string' ? msg : this.#inspect(msg)))
986990
.join(' ')
987991
subject = subject.replace(/%%/g, '%') // Still handle escapes in the fallback
988992
}
989993
}
990994
} else {
991995
// No format specifiers or mismatch count, inspect all messages
992996
subject = redactedMessages
993-
.map((msg) => (typeof msg === 'string' ? msg : inspect(msg, false, null, true)))
997+
.map((msg) => (typeof msg === 'string' ? msg : this.#inspect(msg)))
994998
.join(' ')
995999
subject = subject.replace(/%%/g, '%')
9961000
}

0 commit comments

Comments
 (0)