diff --git a/adonis-typings/logger.ts b/adonis-typings/logger.ts index da70ae3..43484f6 100644 --- a/adonis-typings/logger.ts +++ b/adonis-typings/logger.ts @@ -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 diff --git a/src/Logger.ts b/src/Logger.ts index eec916f..199ebf1 100644 --- a/src/Logger.ts +++ b/src/Logger.ts @@ -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)) } /** diff --git a/test/logger.spec.ts b/test/logger.spec.ts index 271bbd1..373d232 100644 --- a/test/logger.spec.ts +++ b/test/logger.spec.ts @@ -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', @@ -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', @@ -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',