Skip to content

Commit

Permalink
fix: force add logger (#793)
Browse files Browse the repository at this point in the history
  • Loading branch information
czy88840616 authored Jan 6, 2021
1 parent 9450c69 commit f095543
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 18 deletions.
17 changes: 8 additions & 9 deletions packages/logger/src/container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,16 @@ export class MidwayLoggerContainer extends Map<string, ILogger> {
}

addLogger(name: string, logger: ILogger, errorWhenReplace = true) {
if (!this.has(name)) {
if (logger['on']) {
(logger as any).on('close', () => this.delete(name));
if (!errorWhenReplace || !this.has(name)) {
// 同一个实例就不需要再添加了
if (this.get(name) !== logger) {
if (logger['on']) {
(logger as any).on('close', () => this.delete(name));
}
this.set(name, logger);
}
this.set(name, logger);
} else {
if (errorWhenReplace) {
throw new Error(`logger id ${name} has duplicate`);
} else {
return this.addLogger(name, logger, false);
}
throw new Error(`logger id ${name} has duplicate`);
}
return this.get(name);
}
Expand Down
32 changes: 23 additions & 9 deletions packages/logger/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
} from './util';
import { EggLogger } from 'egg-logger';
import { readFileSync } from "fs";
import { ILogger } from '../dist';

describe('/test/index.test.ts', () => {
it('should test create logger', async () => {
Expand Down Expand Up @@ -343,31 +344,44 @@ describe('/test/index.test.ts', () => {
const logger1 = createConsoleLogger('consoleLogger');
createConsoleLogger('anotherConsoleLogger');
const logger3 = createConsoleLogger('consoleLogger');
expect(logger1).toBe(logger3);
expect(loggers.size).toBe(2);
expect(logger1).toEqual(logger3);
expect(loggers.size).toEqual(2);
clearAllLoggers();
expect(loggers.size).toBe(0);
expect(loggers.size).toEqual(0);
});

it('should test container with add logger', function () {
if (loggers.size > 0) {
clearAllLoggers();
}
const originLogger = createConsoleLogger('consoleLogger');
expect(loggers.size).toBe(1);
const originLogger: any = createConsoleLogger('consoleLogger');
expect(loggers.size).toEqual(1);
const logger = new MidwayBaseLogger({
disableError: true,
disableFile: true
});
// 重复添加会报错
expect(() => {
loggers.addLogger('consoleLogger', logger);
}).toThrow();
expect(loggers.size).toEqual(1);
let consoleLogger: ILogger = loggers.getLogger('consoleLogger');
expect(originLogger).toEqual(consoleLogger);

// 允许重复添加,且直接返回原对象
loggers.addLogger('consoleLogger', originLogger, false);
expect(loggers.size).toEqual(1);
consoleLogger = loggers.getLogger('consoleLogger');
expect(originLogger).toEqual(consoleLogger);

// 允许重复添加,且替换原来的对象
loggers.addLogger('consoleLogger', logger, false);
expect(loggers.size).toEqual(1);
consoleLogger = loggers.getLogger('consoleLogger');
expect(logger).toEqual(consoleLogger);

expect(loggers.size).toBe(1);
const consoleLogger: any = loggers.getLogger('consoleLogger');
expect(originLogger).toBe(consoleLogger);
loggers.close('consoleLogger');
expect(loggers.size).toBe(0);
expect(loggers.size).toEqual(0);
});

it('should create container with options and add logger', async () => {
Expand Down

0 comments on commit f095543

Please sign in to comment.