This commit is contained in:
2026-01-25 13:42:28 +01:00
parent 4dbf566f0c
commit c074a5d935
13 changed files with 383 additions and 12 deletions

View File

@@ -0,0 +1,11 @@
export type LogLevel = 'trace' | 'debug' | 'info' | 'warn' | 'error' | 'fatal';
export interface LoggerService {
trace(msg: string, ...args: any[]): void;
debug(msg: string, ...args: any[]): void;
info(msg: string, ...args: any[]): void;
warn(msg: string, ...args: any[]): void;
error(msg: string, ...args: any[]): void;
fatal(msg: string, ...args: any[]): void;
child(bindings: Record<string, any>): LoggerService;
}

View File

@@ -0,0 +1,13 @@
import type { LoggerService } from './logger-service';
export class NoopLoggerService implements LoggerService {
trace() {}
debug() {}
info() {}
warn() {}
error() {}
fatal() {}
child() {
return this;
}
}

View File

@@ -0,0 +1,58 @@
import pino, { Logger as PinoLogger } from 'pino';
import type { LoggerService } from './logger-service';
export class PinoLoggerService implements LoggerService {
private readonly logger: PinoLogger;
constructor(name?: string, parent?: PinoLogger) {
if (parent) {
this.logger = parent.child({ name });
} else {
this.logger = pino({
name: name || 'app',
level: process.env.LOG_LEVEL || 'info',
transport:
process.env.NODE_ENV !== 'production'
? {
target: 'pino-pretty',
options: {
colorize: true,
},
}
: undefined,
});
}
}
trace(msg: string, ...args: any[]) {
this.logger.trace(msg, ...args);
}
debug(msg: string, ...args: any[]) {
this.logger.debug(msg, ...args);
}
info(msg: string, ...args: any[]) {
this.logger.info(msg, ...args);
}
warn(msg: string, ...args: any[]) {
this.logger.warn(msg, ...args);
}
error(msg: string, ...args: any[]) {
this.logger.error(msg, ...args);
}
fatal(msg: string, ...args: any[]) {
this.logger.fatal(msg, ...args);
}
child(bindings: Record<string, any>): LoggerService {
const childPino = this.logger.child(bindings);
const service = new PinoLoggerService();
// @ts-ignore - accessing private member for child creation
service.logger = childPino;
return service;
}
}