Skip to content

Commit

Permalink
fix: declare pino-pretty dependency (#326)
Browse files Browse the repository at this point in the history
Co-authored-by: Timon Masberg <contact@timonmasberg.com>
  • Loading branch information
JSPRH and timonmasberg authored Aug 10, 2023
1 parent 88990f0 commit 033e89c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import pino from 'pino';
import { Transform } from 'stream';

import { PinoLogger } from './pino-logger.service';

Expand All @@ -24,15 +25,12 @@ describe('PinoLogger', () => {
const debug = true;
logger = new PinoLogger(debug);

expect(pinoMock).toHaveBeenCalledWith({
level: 'trace',
transport: {
target: 'pino-pretty',
colorize: true,
translateTime: 'SYS:dd.mm.yyyy hh:MM:ss',
ignore: 'pid,hostname',
expect(pinoMock).toHaveBeenCalledWith(
{
level: 'trace',
},
});
expect.any(Transform),
);
});

it('should initialize pino with the correct configuration when debug is false', () => {
Expand Down
41 changes: 26 additions & 15 deletions libs/api/observability/src/lib/services/pino-logger.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import pino from 'pino';
import { OnUnknown } from 'pino-abstract-transport';
import pretty from 'pino-pretty';
import { Transform } from 'stream';

import { KordisLoggerService } from './kordis-logger-service.interface';

Expand All @@ -9,21 +12,29 @@ export class PinoLogger implements KordisLoggerService {
private readonly logger: pino.Logger;

constructor(debug: boolean) {
this.logger = pino(
debug
? {
level: 'trace',
transport: {
target: 'pino-pretty',
colorize: true,
translateTime: 'SYS:dd.mm.yyyy hh:MM:ss',
ignore: 'pid,hostname',
},
}
: {
level: 'info',
},
);
let pinoParams:
| [{ level: string }]
| [{ level: string }, Transform & OnUnknown];
if (debug) {
pinoParams = [
{
level: 'trace',
},
pretty({
colorize: true,
translateTime: 'SYS:dd.mm.yyyy hh:MM:ss',
ignore: 'pid,hostname',
}),
];
} else {
pinoParams = [
{
level: 'info',
},
];
}

this.logger = pino(...pinoParams);
}

log(message: string, context?: string, args?: object): void {
Expand Down

0 comments on commit 033e89c

Please sign in to comment.