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

dot record console.log('xx') #1042

Closed
junbao520 opened this issue Sep 3, 2020 · 2 comments · Fixed by #1062 or #1247
Closed

dot record console.log('xx') #1042

junbao520 opened this issue Sep 3, 2020 · 2 comments · Fixed by #1062 or #1247
Labels
enhancement New feature or request
Milestone

Comments

@junbao520
Copy link

log4js.configure({
appenders: {
cheeseLogs: { type: 'file', filename: 'cheese.log' },
console: { type: 'console' }
},
categories: {
cheese: { appenders: ['cheeseLogs'], level: 'error' },
another: { appenders: ['console'], level: 'trace' },
default: { appenders: ['console', 'cheeseLogs'], level: 'trace' }
}
});

this code dot record console.log()

@rnd-debug
Copy link
Contributor

@junbao520 Please provide an example of your issue.
The following code works as expected:

const log4js = require('log4js')

log4js.configure({
appenders: {
cheeseLogs: { type: 'file', filename: 'cheese.log' },
console: { type: 'console' }
},
categories: {
cheese: { appenders: ['cheeseLogs'], level: 'error' },
another: { appenders: ['console'], level: 'trace' },
default: { appenders: ['console', 'cheeseLogs'], level: 'trace' }
}
});

const logger = log4js.getLogger();
logger.trace("this message is recorded to cheese.log file and appears in the console");
console.log("this message goes to console only, as it is not using log4js");
const loggerAnother = log4js.getLogger("another");
loggerAnother.trace("goes only to console");
const loggerCheese = log4js.getLogger("cheese");
loggerCheese.error("goes only to cheese file");
loggerCheese.trace("not recorded, because cheese level is 'error'");

@abernh
Copy link
Contributor

abernh commented Feb 22, 2021

I saw the same issue and it is a misunderstanding:

TLDR;

logger.log is not a log-level-short method like logger.info or logger.error but expects a valid log-level as first parameter (or will silently default to log-level Info).

In Detail

I landed here because my logger.log calls seem to drop the first argument that was given, logging only 2nd and further arguments.
As I had simply replaced console.log with the log4js logger, I had overlooked the fact that there is no log log-level. The .log method has a different format.

#lib/logger.js:70
  log(level, ...args) { ...

Using logger.log('some message'); will not log that message but treats the log as log-level Info and drops silently that first argument.

const log4js = require('log4js')

log4js.configure({
appenders: {
console: { type: 'console' }
},
categories: {
default: { appenders: ['console'], level: 'trace' }
}
});

const logger = log4js.getLogger();
logger.trace("this message appears in the console");
logger.log("this message appears as empty info log in the console");

Suggestion

To avoid such a misunderstanding it might be better not to auto-default to the Info level without an additional warning about the given invalid log-level.

abernh added a commit to abernh/log4js-node that referenced this issue Feb 23, 2021
a warning is logged if the `log` method is used with an unknown log-level
this happens whenever people confuse the `log` method with yet another log-level-short method (like in the browser console.log)
adjusted `newLevel-test` accordingly

rel: log4js-node#1042

Signed-off-by: abernh <a.bernhard@23go.de>
@lamweili lamweili linked a pull request Jan 7, 2022 that will close this issue
@lamweili lamweili added this to the 6.4.0 milestone Jan 10, 2022
@lamweili lamweili added the enhancement New feature or request label Apr 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
4 participants