Skip to content

Commit

Permalink
chore: upgrade to pino 6.13.0
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Jul 23, 2021
1 parent a8fac7b commit 6c49214
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 13 deletions.
16 changes: 11 additions & 5 deletions adonis-typings/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,17 @@ declare module '@ioc:Adonis/Core/Logger' {

isLevelEnabled(level: string): boolean
bindings(): Bindings
child(bindings: {
level?: Level | string
serializers?: { [key: string]: SerializerFn }
[key: string]: any
}): LoggerContract
child(
bindings: {
level?: Level | string
serializers?: { [key: string]: SerializerFn }
[key: string]: any
},
options?: {
redact?: string[] | redactOptions
serializers?: { [key: string]: SerializerFn }
}
): LoggerContract
}

const Logger: LoggerContract
Expand Down
19 changes: 13 additions & 6 deletions src/Logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,15 +185,22 @@ export class Logger implements LoggerContract {
/**
* Returns a child logger instance
*/
public child(bindings: {
level?: Pino.Level | string
serializers?: { [key: string]: Pino.SerializerFn }
[key: string]: any
}) {
public child(
bindings: {
level?: Pino.Level | string
serializers?: { [key: string]: Pino.SerializerFn }
[key: string]: any
},
options?: {
redact?: string[] | Pino.redactOptions
serializers?: { [key: string]: Pino.SerializerFn }
}
) {
if (!this.config.enabled) {
return this
}
return new Logger(this.config, this.pino.child(bindings))

return new Logger(this.config, (this.pino.child as any)(bindings, options))
}

/**
Expand Down
62 changes: 60 additions & 2 deletions test/logger.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,64 @@ test.group('Logger', () => {
)
})

test('use custom redact options with a child logger', (assert) => {
const messages: string[] = []

const logger = new Logger({
name: 'adonis-logger',
level: 'info',
messageKey: 'msg',
enabled: true,
stream: getFakeStream((message) => {
messages.push(message.trim())
return true
}),
})

const child = logger.child(
{},
{
redact: ['password'],
}
)

child.trace({ password: 'secret' }, 'hello trace')
child.debug({ password: 'secret' }, 'hello debug')
child.info({ password: 'secret' }, 'hello info')
child.warn({ password: 'secret' }, 'hello warn')
child.error({ password: 'secret' }, 'hello error')
child.fatal({ password: 'secret' }, 'hello fatal')

assert.deepEqual(
messages.map((m) => {
const parsed = JSON.parse(m)
return { level: parsed.level, msg: parsed.msg, password: parsed.password }
}),
[
{
level: 30,
msg: 'hello info',
password: '[Redacted]',
},
{
level: 40,
msg: 'hello warn',
password: '[Redacted]',
},
{
level: 50,
msg: 'hello error',
password: '[Redacted]',
},
{
level: 60,
msg: 'hello fatal',
password: '[Redacted]',
},
]
)
})

test('log using fake logger', (assert) => {
const logger = new FakeLogger({
name: 'adonis-logger',
Expand Down Expand Up @@ -330,7 +388,7 @@ test.group('Logger', () => {
assert.deepEqual(logger.child({}), logger)
assert.deepEqual(logger.bindings(), {})
assert.isFalse(logger.isLevelEnabled('info'))
assert.equal(logger.pinoVersion, '6.12.0')
assert.equal(logger.pinoVersion, '6.13.0')
assert.deepEqual(logger.levels, {
labels: {
10: 'trace',
Expand Down Expand Up @@ -521,7 +579,7 @@ test.group('Logger', () => {
assert.deepEqual(logger.child({}), logger)
assert.deepEqual(logger.bindings(), {})
assert.isFalse(logger.isLevelEnabled('info'))
assert.equal(logger.pinoVersion, '6.12.0')
assert.equal(logger.pinoVersion, '6.13.0')
assert.deepEqual(logger.levels, {
labels: {
10: 'trace',
Expand Down

0 comments on commit 6c49214

Please sign in to comment.