Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

自定义日志级别时,TS使用自定级别记录日志报错 #1112

Closed
lianjl256 opened this issue Dec 30, 2021 · 2 comments · Fixed by #1117
Closed

自定义日志级别时,TS使用自定级别记录日志报错 #1112

lianjl256 opened this issue Dec 30, 2021 · 2 comments · Fixed by #1117
Milestone

Comments

@lianjl256
Copy link

当定义了一个日志级别时:
例如:
log4js.levels.addLevels({
KEY: {
value: 30001,
colour: 'yellow'
}
})
再使用新的级别记录日志:
logger._log('KEY', ‘测试关键日志’);
会报错:
categoryAppenders.forEach(appender => {
^
TypeError: eventLevel.isGreaterThanOrEqualTo is not a function

导致这个原因是在log4js.d.ts这样定义_log方法的:
_log(level: string, data: any): void;

应该改成:
_log(level: Level, data: any): void;
然后使用以下方法记录相应的日志
logger._log(log4js.levels.getLevel('KEY'), "测试关键日志’")

@lamweili lamweili linked a pull request Feb 1, 2022 that will close this issue
@lamweili lamweili added this to the 6.4.0 milestone Feb 1, 2022
@lamweili
Copy link
Contributor

lamweili commented Feb 1, 2022

Fixed in log4js@6.4.0.

@lamweili lamweili closed this as completed Feb 1, 2022
@lamweili
Copy link
Contributor

lamweili commented Feb 1, 2022

In any case, you should be using log and not _log.
It does additional checks for isLevelEnabled before proceeding further to _log (non-public).

log(level: Level | string, ...args: any[]): void;

log(level, ...args) {
let logLevel = levels.getLevel(level);
if (!logLevel) {
this._log(levels.WARN, 'log4js:logger.log: invalid value for log-level as first parameter given: ', level);
logLevel = levels.INFO;
}
if (this.isLevelEnabled(logLevel)) {
this._log(logLevel, args);
}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants