logging
This commit is contained in:
11
lib/services/logging/logger-service.ts
Normal file
11
lib/services/logging/logger-service.ts
Normal 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;
|
||||
}
|
||||
13
lib/services/logging/noop-logger-service.ts
Normal file
13
lib/services/logging/noop-logger-service.ts
Normal 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;
|
||||
}
|
||||
}
|
||||
58
lib/services/logging/pino-logger-service.ts
Normal file
58
lib/services/logging/pino-logger-service.ts
Normal 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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user