fix(logger): 移除自定义 factory 的缓存, 由使用方管理 (#221)
* fix(logger): 移除自定义 factory 的缓存, 由使用方管理 * test
This commit is contained in:
@@ -50,11 +50,14 @@ export class LoggerManager {
|
|||||||
return this.defaultLogger;
|
return this.defaultLogger;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 如果有自定义 factory, 每次都调用(不缓存), 由使用方自行管理
|
||||||
|
if (this._loggerFactory) {
|
||||||
|
return this._loggerFactory(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 默认 ConsoleLogger 仍然缓存(保持向后兼容)
|
||||||
if (!this._loggers.has(name)) {
|
if (!this._loggers.has(name)) {
|
||||||
const logger = this._loggerFactory
|
this._loggers.set(name, new ConsoleLogger({ prefix: name, level: this._defaultLevel }));
|
||||||
? this._loggerFactory(name)
|
|
||||||
: new ConsoleLogger({ prefix: name, level: this._defaultLevel });
|
|
||||||
this._loggers.set(name, logger);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return this._loggers.get(name)!;
|
return this._loggers.get(name)!;
|
||||||
@@ -132,18 +135,17 @@ export class LoggerManager {
|
|||||||
/**
|
/**
|
||||||
* 设置日志器工厂方法
|
* 设置日志器工厂方法
|
||||||
* @param factory 日志器工厂方法
|
* @param factory 日志器工厂方法
|
||||||
|
*
|
||||||
|
* 注意: 应该在导入 ECS 模块之前调用此方法。
|
||||||
|
* 设置后, 每次调用 getLogger() 都会通过 factory 创建新的 logger 实例, 由用户侧管理
|
||||||
*/
|
*/
|
||||||
public setLoggerFactory(factory: (name?: string) => ILogger): void {
|
public setLoggerFactory(factory: (name?: string) => ILogger): void {
|
||||||
if (this._defaultLogger || this._loggers.size > 0) {
|
|
||||||
console.warn(
|
|
||||||
'[LoggerManager] setLoggerFactory 应该在导入 ECS 模块之前调用。' +
|
|
||||||
'已创建的 logger 引用不会被更新。'
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
this._loggerFactory = factory;
|
this._loggerFactory = factory;
|
||||||
// 清空已创建的 logger, 下次获取时使用新工厂方法
|
|
||||||
delete (this as any)._defaultLogger;
|
// 清空默认 logger,下次获取时使用新工厂方法
|
||||||
|
delete this._defaultLogger;
|
||||||
|
|
||||||
|
// 清空缓存
|
||||||
this._loggers.clear();
|
this._loggers.clear();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,12 @@
|
|||||||
import { ConsoleLogger, LogLevel, createLogger, LoggerManager, setLoggerColors, resetLoggerColors, Colors } from '../../src/Utils/Logger';
|
import {
|
||||||
|
ConsoleLogger,
|
||||||
|
LogLevel,
|
||||||
|
createLogger,
|
||||||
|
LoggerManager,
|
||||||
|
setLoggerColors,
|
||||||
|
resetLoggerColors,
|
||||||
|
Colors
|
||||||
|
} from '../../src/Utils/Logger';
|
||||||
|
|
||||||
describe('Logger', () => {
|
describe('Logger', () => {
|
||||||
describe('ConsoleLogger', () => {
|
describe('ConsoleLogger', () => {
|
||||||
@@ -21,9 +29,7 @@ describe('Logger', () => {
|
|||||||
|
|
||||||
logger.info('测试消息');
|
logger.info('测试消息');
|
||||||
|
|
||||||
expect(consoleSpy).toHaveBeenCalledWith(
|
expect(consoleSpy).toHaveBeenCalledWith('\x1b[32m[INFO] 测试消息\x1b[0m');
|
||||||
'\x1b[32m[INFO] 测试消息\x1b[0m'
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('应该在颜色禁用时输出纯文本', () => {
|
it('应该在颜色禁用时输出纯文本', () => {
|
||||||
@@ -90,9 +96,7 @@ describe('Logger', () => {
|
|||||||
|
|
||||||
logger.info('测试消息');
|
logger.info('测试消息');
|
||||||
|
|
||||||
expect(consoleSpy).toHaveBeenCalledWith(
|
expect(consoleSpy).toHaveBeenCalledWith('\x1b[36m[INFO] 测试消息\x1b[0m');
|
||||||
'\x1b[36m[INFO] 测试消息\x1b[0m'
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('应该支持运行时修改颜色配置', () => {
|
it('应该支持运行时修改颜色配置', () => {
|
||||||
@@ -108,9 +112,7 @@ describe('Logger', () => {
|
|||||||
|
|
||||||
logger.info('测试消息');
|
logger.info('测试消息');
|
||||||
|
|
||||||
expect(consoleSpy).toHaveBeenCalledWith(
|
expect(consoleSpy).toHaveBeenCalledWith('\x1b[94m[INFO] 测试消息\x1b[0m');
|
||||||
'\x1b[94m[INFO] 测试消息\x1b[0m'
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -229,28 +231,6 @@ describe('Logger', () => {
|
|||||||
manager.getLogger();
|
manager.getLogger();
|
||||||
expect(factorySpy).toHaveBeenCalled();
|
expect(factorySpy).toHaveBeenCalled();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('应该在已创建日志器后设置工厂时发出警告', () => {
|
|
||||||
const warnSpy = jest.spyOn(console, 'warn').mockImplementation();
|
|
||||||
|
|
||||||
// 先创建一个日志器
|
|
||||||
manager.getLogger('ExistingLogger');
|
|
||||||
|
|
||||||
// 再设置工厂
|
|
||||||
manager.setLoggerFactory(() => ({
|
|
||||||
debug: jest.fn(),
|
|
||||||
info: jest.fn(),
|
|
||||||
warn: jest.fn(),
|
|
||||||
error: jest.fn(),
|
|
||||||
fatal: jest.fn()
|
|
||||||
}));
|
|
||||||
|
|
||||||
expect(warnSpy).toHaveBeenCalledWith(
|
|
||||||
expect.stringContaining('setLoggerFactory 应该在导入 ECS 模块之前调用')
|
|
||||||
);
|
|
||||||
|
|
||||||
warnSpy.mockRestore();
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('全局颜色配置', () => {
|
describe('全局颜色配置', () => {
|
||||||
|
|||||||
Reference in New Issue
Block a user