From b4a54dc2c27c5608a8e0402fbeec1531ac720322 Mon Sep 17 00:00:00 2001 From: Kostya Zgara Date: Sun, 17 Jan 2021 12:39:26 +0200 Subject: [PATCH] feat(): add native pino levels for injection to fastify adapter Closes #54 --- lib/loggers/pino.logger.ts | 33 ++++++++++++++++++++++++++++---- test/loggers/pino-logger.spec.ts | 28 +++++++++++++++++++++++++-- 2 files changed, 55 insertions(+), 6 deletions(-) diff --git a/lib/loggers/pino.logger.ts b/lib/loggers/pino.logger.ts index 95536855..ceb75cdf 100644 --- a/lib/loggers/pino.logger.ts +++ b/lib/loggers/pino.logger.ts @@ -1,4 +1,4 @@ -import type { Logger, Level } from 'pino'; +import type { Logger, Level, Bindings } from 'pino'; import { LoggerService, OnApplicationShutdown, @@ -32,6 +32,29 @@ export class PinoLogger implements LoggerService, OnApplicationShutdown { this._logger = pino(options); } } + // native pino levels implementations + public child(bindings: Bindings): Logger { + return this._logger.child(bindings); + } + public fatal(_obj: unknown, _msg?: string, ..._args: unknown[]): void; + public fatal(_msg: string, ..._args: unknown[]): void; + // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types + public fatal(msgOrObject: any, ...args: unknown[]): void { + this._logger.fatal(msgOrObject, ...args); + } + public info(_obj: unknown, _msg?: string, ..._args: unknown[]): void; + public info(_msg: string, ..._args: unknown[]): void; + // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types + public info(msgOrObject: any, ...args: unknown[]): void { + this._logger.info(msgOrObject, ...args); + } + public trace(_obj: unknown, _msg?: string, ..._args: unknown[]): void; + public trace(_msg: string, ..._args: unknown[]): void; + // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types + public trace(msgOrObject: any, ...args: unknown[]): void { + this._logger.trace(msgOrObject, ...args); + } + // Nestjs logger service implementation public error(msgOrObject: unknown, trace?: string, context?: string): void { this.callFunction('error', msgOrObject, context, trace); } @@ -93,10 +116,12 @@ export class PinoLogger implements LoggerService, OnApplicationShutdown { private finalHandler( err: Error | null, finalLogger: Logger, - evt: string, + evt?: string, ): void { - finalLogger.info(`${evt} caught`); - + finalLogger.info('Final flushing logs to stdout...'); + if (evt) { + finalLogger.info(`${evt} caught`); + } if (err) { finalLogger.error(err, 'error caused exit'); } diff --git a/test/loggers/pino-logger.spec.ts b/test/loggers/pino-logger.spec.ts index a02937f9..e1f27b9c 100644 --- a/test/loggers/pino-logger.spec.ts +++ b/test/loggers/pino-logger.spec.ts @@ -7,7 +7,16 @@ chai.use(sinonChai); const expect = chai.expect; describe('Pino logger', () => { - const levels = ['error', 'warn', 'log', 'debug', 'verbose']; + const levels = [ + 'error', + 'warn', + 'log', + 'debug', + 'verbose', + 'fatal', + 'trace', + 'info', + ]; afterEach(() => { sinon.restore(); }); @@ -16,6 +25,13 @@ describe('Pino logger', () => { expect(pino).property('_logger').is.not.undefined; expect(pino).not.property('_finalLogger'); }); + it('should return child logger', () => { + const pino = new PinoLogger(); + const stubPinoChild = sinon.stub((pino as any)._logger, 'child'); + const testOptions = { levels: 'info' }; + pino.child(testOptions); + expect(stubPinoChild).calledOnceWithExactly(testOptions); + }); describe('logging', () => { let mockPinoLogger: sinon.SinonMock; let pinoLogger: PinoLogger; @@ -173,7 +189,8 @@ describe('Pino logger', () => { testEvent, ); expect(stubFinalLoggerError).not.called; - expect(stubFinalLoggerInfo).calledOnceWithExactly( + expect(stubFinalLoggerInfo).calledTwice; + expect(stubFinalLoggerInfo.secondCall).calledWithExactly( `${testEvent} caught`, ); }); @@ -188,6 +205,13 @@ describe('Pino logger', () => { ); expect(stubFinalLoggerError).calledOnceWith(testError); }); + it('if no error and no event, should just inform about final handler is working', () => { + (pino as any).finalHandler(null, { + info: stubFinalLoggerInfo, + error: stubFinalLoggerError, + }); + expect(stubFinalLoggerInfo).calledOnce; + }); }); }); });