-
Notifications
You must be signed in to change notification settings - Fork 97
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
Error formats incorrectly #100
Comments
Same issue here. Using t he exact code from the docs:
you get:
|
Yeah same here. It seems that the stack is JSON.strigified and thus escapes newline characters. In winston I tried using a custom format instead (with The other issue is that logging without a message as the first argument supresses both the error's message and the stack. So you(in winston) with |
yeah, same complaint, doing this with splat is nicer :/ log.error('%O', new Error('my error')); |
Ran into the same issue just now. With winston 3.3.3, the following does not work as expected: const winston = require('winston')
const logger = winston.createLogger({
transports: [
new winston.transports.Console({
format: winston.format.errors(),
})
]
})
logger.error(new Error('I am Error')) // Prints `{"level":"error"}` Looks like the error properties get dropped when doing There's a workaround: Apply the errors format before passing the log message to the transports: const winston = require('winston')
const logger = winston.createLogger({
format: winston.format.errors(),
transports: [
new winston.transports.Console()
]
})
logger.error(new Error('I am Error')) // Prints `I am Error` |
Same when running the example : const {format} = require('logform');
const {errors} = format;
const errorsFormat = errors({stack: true});
const info = errorsFormat.transform(new Error('Oh no!'));
console.log(info);
// Error: Oh no!
// at repl:1:13
// at ContextifyScript.Script.runInThisContext (vm.js:50:33)
// at REPLServer.defaultEval (repl.js:240:29)
// at bound (domain.js:301:14)
// at REPLServer.runBound [as eval] (domain.js:314:12)
// at REPLServer.onLine (repl.js:468:10)
// at emitOne (events.js:121:20)
// at REPLServer.emit (events.js:211:7)
// at REPLServer.Interface._onLine (readline.js:282:10)
// at REPLServer.Interface._line (readline.js:631:8) Error:
Node v14.17.4 |
This has to be addressed; as there is currently no solution at all to correctly handle I digged into it; by first copy pasting the const errFormat = Logform.format((einfo, { stack, cause }) => {
console.log('IM HERE')
console.log(einfo)
if (einfo instanceof Error) {
const info = Object.assign({}, einfo, {
level: einfo.level,
[LEVEL]: einfo[LEVEL] || einfo.level,
message: einfo.message,
[MESSAGE]: einfo[MESSAGE] || einfo.message,
})
if (stack) info.stack = einfo.stack
if (cause) info.cause = einfo.cause
return info
}
if (!(einfo.message instanceof Error)) return einfo
// Assign all enumerable properties and the
// message property from the error provided.
const err = einfo.message
Object.assign(einfo, err)
einfo.message = err.message
einfo[MESSAGE] = err.message
// Assign the stack and/or cause if requested.
if (stack) einfo.stack = err.stack
if (cause) einfo.cause = err.cause
return einfo
}) This is a total nonsense; as (thanks to @opitzmic )
As a consequence, Since it's done at the transport level; there is basically nothing we can do inside formatters that could workaround this. |
Any updates? |
Open to PRs that the community feels solves things in the right way here (whether at the Winston, Winston-transport, or logform level). I don’t have much time to investigate this but if someone made a well-tested / backwards-compatible PR that articulated why it addressed these issues, it’d be easy for me to approve and do a release. (Or if there’s no backwards compatible way to do this - could fix via a major version bump to winston…) |
An update to documentation/example code might also be an improvement. |
I think I saw a comment on here briefly (since deleted) asking for updates... any contributions that folks feel improves the behavior here (even if that's just better docs/examples) are absolutely welcomed and will be reviewed. We appreciate the Winston userbase and value any community contributions. |
The error formatter example describes that it will log:
Whereas it really logs:
{ level: undefined, message: 'Oh no!', stack: 'Error: Oh no!\n at Object.<anonymous> (C:\\Users\\Fennec\\Documents\\errlog.js:6:37)\n at Module._compile (internal/modules/cjs/loader.js:688:30)\n at Object.Module._extensions..js (internal/modules/cjs/loader.js:699:10)\n at Module.load (internal/modules/cjs/loader.js:598:32)\n at tryModuleLoad (internal/modules/cjs/loader.js:537:12)\n at Function.Module._load (internal/modules/cjs/loader.js:529:3)\n at Function.Module.runMain (internal/modules/cjs/loader.js:741:12)\n at startup (internal/bootstrap/node.js:285:19)\n at bootstrapNodeJSCore (internal/bootstrap/node.js:739:3)', [Symbol(level)]: undefined, [Symbol(message)]: 'Oh no!' }
The text was updated successfully, but these errors were encountered: