removed deprecated createLogger and replaced with getLogger

This commit is contained in:
Bojan Kucera 2025-06-08 12:31:29 -04:00
parent 5d8b102422
commit c10a524aa8
29 changed files with 93 additions and 136 deletions

View file

@ -7,7 +7,6 @@
// Core logger classes and functions
export {
Logger,
createLogger,
getLogger,
shutdownLoggers
} from './logger';

View file

@ -29,10 +29,10 @@ function createTransports(serviceName: string): any {
options: {
colorize: true,
translateTime: 'yyyy-mm-dd HH:MM:ss.l',
messageFormat: '[{service}] {msg}',
messageFormat: '[{service}{childName}] {msg}',
singleLine: true,
hideObject: false,
ignore: 'pid,hostname,service,environment,version',
ignore: 'pid,hostname,service,environment,version,childName',
errorLikeObjectKeys: ['err', 'error'],
errorProps: 'message,stack,name,code',
}
@ -61,7 +61,8 @@ function createTransports(serviceName: string): any {
labels: {
service: serviceName,
environment: lokiConfig.LOKI_ENVIRONMENT_LABEL
}
},
ignore: 'childName',
}
});
}
@ -102,10 +103,13 @@ function getPinoLogger(serviceName: string): pino.Logger {
export class Logger {
private pino: pino.Logger;
private context: LogContext;
private serviceName: string;
private childName?: string;
constructor(serviceName: string, context: LogContext = {}) {
this.pino = getPinoLogger(serviceName);
this.context = context;
this.serviceName = serviceName;
}
/**
@ -198,12 +202,31 @@ export class Logger {
/**
* Create child logger with additional context
*/
child(context: LogContext): Logger {
child(serviceName: string, context?: LogContext): Logger {
// Create child logger that shares the same pino instance with additional context
const childLogger = Object.create(Logger.prototype);
childLogger.serviceName = this.serviceName;
childLogger.childName = serviceName;
childLogger.context = { ...this.context, ...context };
childLogger.pino = this.pino.child(context); // Let pino handle level inheritance naturally
const childBindings = {
service: this.serviceName,
childName: ' -> ' + serviceName,
...(context || childLogger.context)
};
childLogger.pino = this.pino.child(childBindings);
return childLogger;
// }
// childLogger.pino = this.pino.child(context || childLogger.context); // Let pino handle level inheritance naturally
// return childLogger;
}
// Getters for service and context
getServiceName(): string {
return this.serviceName;
}
getChildName(): string | undefined {
return this.childName;
}
}
@ -214,13 +237,6 @@ export function getLogger(serviceName: string, context?: LogContext): Logger {
return new Logger(serviceName, context);
}
/**
* Keep backward compatibility
*/
export function createLogger(serviceName: string): pino.Logger {
return getPinoLogger(serviceName);
}
/**
* Gracefully shutdown all logger instances
* This should be called during application shutdown to ensure all logs are flushed