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