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
|
|
@ -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