Skip to content

Commit

Permalink
Merge pull request #64 from targos/fix-falsy-interpolation
Browse files Browse the repository at this point in the history
Fix falsy sprintf substitution
  • Loading branch information
thetutlage authored Feb 8, 2024
2 parents da1dec4 + cc024e7 commit 8b3b576
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 10 deletions.
8 changes: 1 addition & 7 deletions src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,13 +173,7 @@ export class Logger<Config extends LoggerConfig = LoggerConfig> {
return
}

if (values.length) {
;(this.pino[level] as any)(mergingObject, message, ...values)
} else if (message) {
;(this.pino[level] as any)(mergingObject, message)
} else {
;(this.pino[level] as any)(mergingObject)
}
;(this.pino[level] as any)(mergingObject, message, ...values)
}

/**
Expand Down
11 changes: 8 additions & 3 deletions tests/logger.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ test.group('Logger', () => {
)
})

test('handle sprintf subsitutes', ({ assert }) => {
test('handle sprintf substitutes', ({ assert }) => {
const messages: string[] = []

const logger = new Logger({
Expand All @@ -86,6 +86,7 @@ test.group('Logger', () => {
logger.info('hello %s', 'info')
logger.info('hello %s %o', 'info', { url: '/' })
logger.info('hello %s %j', 'info', { url: '/' })
logger.info('total: %d', 0)

assert.deepEqual(
messages.map((m) => {
Expand All @@ -105,6 +106,10 @@ test.group('Logger', () => {
level: 30,
msg: `hello info ${JSON.stringify({ url: '/' })}`,
},
{
level: 30,
msg: 'total: 0',
},
]
)
})
Expand Down Expand Up @@ -326,8 +331,8 @@ test.group('Logger', () => {
assert.deepEqual(logger.bindings(), {})
assert.isFalse(logger.isLevelEnabled('info'))

assert.snapshot(logger.pinoVersion).matchInline('"8.17.2"')
assert.snapshot(logger.version).matchInline('"8.17.2"')
assert.match(logger.pinoVersion, /\d+\.\d+\.\d+/)
assert.strictEqual(logger.pinoVersion, logger.version)

assert.deepEqual(logger.levels, {
labels: {
Expand Down

0 comments on commit 8b3b576

Please sign in to comment.