Skip to content
This repository has been archived by the owner on Mar 7, 2022. It is now read-only.

Commit

Permalink
feat(): add native pino levels for injection to fastify adapter
Browse files Browse the repository at this point in the history
Closes #54
  • Loading branch information
kostyazgara committed Jan 17, 2021
1 parent 8b54102 commit b4a54dc
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 6 deletions.
33 changes: 29 additions & 4 deletions lib/loggers/pino.logger.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Logger, Level } from 'pino';
import type { Logger, Level, Bindings } from 'pino';
import {
LoggerService,
OnApplicationShutdown,
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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');
}
Expand Down
28 changes: 26 additions & 2 deletions test/loggers/pino-logger.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
Expand All @@ -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;
Expand Down Expand Up @@ -173,7 +189,8 @@ describe('Pino logger', () => {
testEvent,
);
expect(stubFinalLoggerError).not.called;
expect(stubFinalLoggerInfo).calledOnceWithExactly(
expect(stubFinalLoggerInfo).calledTwice;
expect(stubFinalLoggerInfo.secondCall).calledWithExactly(
`${testEvent} caught`,
);
});
Expand All @@ -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;
});
});
});
});

0 comments on commit b4a54dc

Please sign in to comment.