Skip to content

Commit 43540e5

Browse files
authored
chore: add interrupt for empty root (#122)
* chore: add interrupt for empty root * test: add case
1 parent b5ac93b commit 43540e5

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed

src/factory.ts

+7
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,13 @@ export class LoggerFactory extends Map<string, ILogger> {
9191
}) {
9292
const isDevelopment = isDevelopmentEnvironment(appInfo.env);
9393
const logRoot = process.env['MIDWAY_LOGGER_WRITEABLE_DIR'] ?? appInfo.root;
94+
95+
if (!logRoot) {
96+
throw new Error(
97+
'Midway Logger requires a root path during initialization, but it was provided empty. Please set it manually in the "logger.default.dir" configuration.'
98+
);
99+
}
100+
94101
return {
95102
midwayLogger: {
96103
default: {

test/factory.test.ts

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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

Comments
 (0)