removed deprecated createLogger and replaced with getLogger
This commit is contained in:
parent
5d8b102422
commit
c10a524aa8
29 changed files with 93 additions and 136 deletions
|
|
@ -7,7 +7,6 @@
|
|||
// Core logger classes and functions
|
||||
export {
|
||||
Logger,
|
||||
createLogger,
|
||||
getLogger,
|
||||
shutdownLoggers
|
||||
} from './logger';
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@
|
|||
*/
|
||||
|
||||
import { describe, it, expect, beforeEach, afterEach } from 'bun:test';
|
||||
import { Logger, getLogger, createLogger, shutdownLoggers } from '../src';
|
||||
import { Logger, getLogger, shutdownLoggers } from '../src';
|
||||
import { loggerTestHelpers } from './setup';
|
||||
|
||||
describe('Basic Logger Tests', () => {
|
||||
|
|
@ -32,13 +32,6 @@ describe('Basic Logger Tests', () => {
|
|||
anotherTestLoggerInstance.logger.info('Factory test');
|
||||
}).not.toThrow();
|
||||
});
|
||||
|
||||
it('should create Pino logger with createLogger', () => {
|
||||
expect(typeof createLogger).toBe('function');
|
||||
|
||||
// Test that createLogger function exists
|
||||
expect(() => createLogger).not.toThrow();
|
||||
});
|
||||
});
|
||||
|
||||
describe('Logger Methods', () => {
|
||||
|
|
|
|||
|
|
@ -7,7 +7,6 @@
|
|||
import { describe, it, expect, beforeEach, afterEach } from 'bun:test';
|
||||
import {
|
||||
Logger,
|
||||
createLogger,
|
||||
getLogger,
|
||||
shutdownLoggers
|
||||
} from '../src';
|
||||
|
|
@ -101,7 +100,6 @@ describe('Logger Integration Tests', () => {
|
|||
describe('Factory Functions', () => {
|
||||
it('should export factory functions', () => {
|
||||
// Verify that the factory functions are exported and callable
|
||||
expect(typeof createLogger).toBe('function');
|
||||
expect(typeof getLogger).toBe('function');
|
||||
});
|
||||
|
||||
|
|
|
|||
|
|
@ -19,63 +19,7 @@ const originalConsole = {
|
|||
|
||||
// Create a test logger helper
|
||||
export const loggerTestHelpers = {
|
||||
/**
|
||||
* Create a test logger instance that captures logs locally.
|
||||
*/
|
||||
createTestLogger: (serviceName: string = 'test-service', initialContext: any = {}) => {
|
||||
const capturedLogsArray: any[] = [];
|
||||
|
||||
const createLoggerMockInstance = (currentContext: any): Logger => {
|
||||
const buildLogEntry = (level: string, messageOrObject: string | object, metadata?: any) => {
|
||||
const logObject: any = { level, service: serviceName, ...currentContext };
|
||||
|
||||
if (typeof messageOrObject === 'string') {
|
||||
logObject.msg = messageOrObject;
|
||||
if (metadata) {
|
||||
Object.assign(logObject, metadata);
|
||||
}
|
||||
} else { // messageOrObject is an object
|
||||
Object.assign(logObject, messageOrObject); // Merge fields from the message object
|
||||
if (metadata) { // If metadata is also provided (e.g. logger.error({code: 1}, {extra: 'data'}))
|
||||
Object.assign(logObject, metadata);
|
||||
}
|
||||
// Ensure 'msg' field exists, defaulting if necessary
|
||||
if (typeof logObject.msg === 'undefined') {
|
||||
// If the second arg was a string, it might have been intended as the message
|
||||
if (typeof metadata === 'string') {
|
||||
logObject.msg = metadata;
|
||||
} else {
|
||||
logObject.msg = 'Object logged';
|
||||
}
|
||||
}
|
||||
}
|
||||
capturedLogsArray.push(logObject);
|
||||
};
|
||||
|
||||
return {
|
||||
// serviceName, // For inspection if needed, though logs capture it
|
||||
context: currentContext, // For inspection
|
||||
debug: (msgOrObj: string | object, meta?: any) => buildLogEntry('debug', msgOrObj, meta),
|
||||
info: (msgOrObj: string | object, meta?: any) => buildLogEntry('info', msgOrObj, meta),
|
||||
warn: (msgOrObj: string | object, meta?: any) => buildLogEntry('warn', msgOrObj, meta),
|
||||
error: (msgOrObj: string | object, metaOrMsg?: any) => buildLogEntry('error', msgOrObj, metaOrMsg),
|
||||
child: (childContext: any): Logger => {
|
||||
// Children will log to the same capturedLogsArray
|
||||
return createLoggerMockInstance({ ...currentContext, ...childContext });
|
||||
}
|
||||
} as any; // Cast to Logger, assuming it fulfills the interface for testing purposes
|
||||
};
|
||||
|
||||
const loggerInstance = createLoggerMockInstance(initialContext);
|
||||
|
||||
return {
|
||||
logger: loggerInstance,
|
||||
getCapturedLogs: () => [...capturedLogsArray],
|
||||
clearCapturedLogs: () => {
|
||||
capturedLogsArray.length = 0;
|
||||
}
|
||||
};
|
||||
},
|
||||
|
||||
/**
|
||||
* Mock Loki transport
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue