-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Can not log variables that are null
or undefined
#1290
Comments
Sorry, does this also happen with the latest rc ( |
@DABH instincts are correct – this appears to be a duplicate of #1224, which was fixed in ... please let us know if the problem persists after upgrading. |
Updating winston to So there was en error with rc1, but it's fixed now, thanks! This is also made apparent by the fact that (Sorry @indexzero, didn't find that issue before posting!) My errorIn case anyone else comes across the same error. Rookie mistake, I know, but see "Something odd" below for why I didn't catch this. The error I had in my code was calling: Object.keys(info.message); when Something oddThis however, did not log any errors, which is odd, and made the mistake hard to spot and fix. My code is definitively wrong, but shouldn't this log an error, at least to the console? This might also be something to do with how Specifically, when calling const myFormat = format.printf((info) => {
console.log(info.message);
let t = Object.keys(info.message);
return info.message;
} And outputs to console:
While this code produces an error (as one would expect) and stops execution: const myFormat = format.printf((info) => {
console.log(info.message);
let t = Object.keys(null);
return info.message;
} With this output to console:
SetupFor completeness, here's my logger setup ( const winston = require('winston');
const format = winston.format;
const myFormat = format.printf((info) => {
const timestamp = (new Date()).toLocaleTimeString();
let parsedMessage;
if (/*info.message && <ADDED NOW>*/typeof info.message !== 'string' && Object.keys(info.message).length > 1) {
parsedMessage = `\n${JSON.stringify({ ...info.message }, null, 2)}`;
} else if (typeof info.message !== 'string') {
parsedMessage = `${JSON.stringify(info.message)}`;
} else {
parsedMessage = info.message;
}
let logMessage = `${timestamp} ${info.level}: ${parsedMessage}`;
return logMessage;
});
const logger = winston.createLogger({
level: 'info',
format: format.combine(format.colorize(), myFormat),
transports: [
new winston.transports.Console({
level: 'silly'
}),
new winston.transports.File({
filename: 'error.log',
level: 'error'
}),
new winston.transports.File({
filename: 'combined.log'
})
]
});
global.logger = logger; |
@dagerikhl thanks for the detailed repro – what you've described is #1261, which is one of the last "show stopper" important bugs for us to fix before the May 29th release for |
Wow, I'm really nailing it with the searches! Thanks for clearing that up, and thanks for the help. If I think of some way to handle it, I'll be sure to comment. |
Hello! I'm having an issue with logging variables if I expect them to sometimes be
null
orundefined
.I am using a custom formatter, but disabling this does not fix the problem.
Error when
console.log()
-ing the error:And from my logger logging its own error,:
Steps to reproduce
logger.debug(null);
orlet myVar = undefined; logger.info(myVar);
.Expected behaviour
Actual behaviour
Cannot read property 'message' of null/undefined
error.System info
The text was updated successfully, but these errors were encountered: