|
| 1 | +import { LoggerFactory } from '../src/factory'; |
| 2 | +import { LoggerOptions } from '../src/interface'; |
| 3 | +import { MidwayLogger } from '../src/logger'; |
| 4 | + |
| 5 | +describe('LoggerFactory', () => { |
| 6 | + let factory: LoggerFactory; |
| 7 | + let loggerOptions: LoggerOptions; |
| 8 | + |
| 9 | + beforeEach(() => { |
| 10 | + factory = new LoggerFactory(); |
| 11 | + loggerOptions = { |
| 12 | + level: 'info', |
| 13 | + // Add other logger options as needed |
| 14 | + }; |
| 15 | + }); |
| 16 | + |
| 17 | + test('should create a logger', () => { |
| 18 | + const logger = factory.createLogger('test', loggerOptions); |
| 19 | + expect(logger).toBeInstanceOf(MidwayLogger); |
| 20 | + }); |
| 21 | + |
| 22 | + test('should add a logger', () => { |
| 23 | + const logger = new MidwayLogger(loggerOptions); |
| 24 | + const addedLogger = factory.addLogger('test', logger); |
| 25 | + expect(addedLogger).toEqual(logger); |
| 26 | + }); |
| 27 | + |
| 28 | + test('should get a logger', () => { |
| 29 | + factory.createLogger('test', loggerOptions); |
| 30 | + const logger = factory.getLogger('test'); |
| 31 | + expect(logger).toBeInstanceOf(MidwayLogger); |
| 32 | + }); |
| 33 | + |
| 34 | + test('should remove a logger', () => { |
| 35 | + factory.createLogger('test', loggerOptions); |
| 36 | + factory.removeLogger('test'); |
| 37 | + const logger = factory.getLogger('test'); |
| 38 | + expect(logger).toBeUndefined(); |
| 39 | + }); |
| 40 | + |
| 41 | + test('should close all loggers', () => { |
| 42 | + factory.createLogger('test1', loggerOptions); |
| 43 | + factory.createLogger('test2', loggerOptions); |
| 44 | + factory.close(); |
| 45 | + const logger1 = factory.getLogger('test1'); |
| 46 | + const logger2 = factory.getLogger('test2'); |
| 47 | + expect(logger1).toBeUndefined(); |
| 48 | + expect(logger2).toBeUndefined(); |
| 49 | + }); |
| 50 | + |
| 51 | + |
| 52 | + test('should get default midway logger config', () => { |
| 53 | + const appInfo = { |
| 54 | + pkg: {}, |
| 55 | + name: 'testApp', |
| 56 | + baseDir: '/base/dir', |
| 57 | + appDir: '/app/dir', |
| 58 | + HOME: '/home/dir', |
| 59 | + root: '/root/dir', |
| 60 | + env: 'local', |
| 61 | + }; |
| 62 | + |
| 63 | + const config = factory.getDefaultMidwayLoggerConfig(appInfo); |
| 64 | + |
| 65 | + expect(config).toBeDefined(); |
| 66 | + expect(config.midwayLogger.default.dir).toEqual('/root/dir/logs/testApp'); |
| 67 | + expect(config.midwayLogger.default.fileLogName).toEqual('midway-app.log'); |
| 68 | + expect(config.midwayLogger.default.errorLogName).toEqual('common-error.log'); |
| 69 | + expect(config.midwayLogger.default.transports.console['autoColors']).toBeTruthy(); |
| 70 | + expect(config.midwayLogger.default.transports.file.bufferWrite).toBeFalsy(); |
| 71 | + expect(config.midwayLogger.default.transports.error.bufferWrite).toBeFalsy(); |
| 72 | + expect(config.midwayLogger.clients.coreLogger.fileLogName).toEqual('midway-core.log'); |
| 73 | + }); |
| 74 | +}); |
0 commit comments