Skip to content

Commit

Permalink
fix: support falsy value in sprintf substitution
Browse files Browse the repository at this point in the history
  • Loading branch information
targos committed Feb 8, 2024
1 parent da1dec4 commit 73b4bed
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 8 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
7 changes: 6 additions & 1 deletion 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

0 comments on commit 73b4bed

Please sign in to comment.