-
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
Error logging method does not logs Error instance correctly #1498
Comments
It doesn't depends on |
Probably related issue #1422 |
Had same issue, I had to implement
|
@gtsec actually I did almost the same just used new method name |
This might be caused by |
In a sample repo where I'm testing out migrating const errorFormatter = format((info, opts) => {
let formatted = {};
if (info instanceof Error) {
// An error object is not 100% like a normal object, so
// we have to jump through hoops to get needed info out
// of error objects for logging.
formatted = Object.assign(formatted, info);
formatted.message = info.stack;
} else {
formatted = info;
}
return formatted;
}); However for a consistent experience it would be beneficial if this handling was done inside of the logger's error method, rather than relying on custom formatters (inconsistent from project-to-project) or re-implementing/overwriting the library's methods (super unsafe & hacky). |
Actually, I just now noticed that the default winston format object has a handler for errors, so you just need to attach it as a formatter. |
Example? |
So, turns out I was incorrect about the error formatter. It's not in Winston yet, but it is in [email protected], and you have to use it in combination with the So, yeah, lots of caveats to get it going. Would be better if you could just use it standalone, but it's better than nothing. const winston = require('winston');
const { format } = require('logform');
const logger = winston.createLogger({
format: format.combine(
format.errors({ stack: true }),
format.metadata(),
format.json(),
),
transports: [ new winston.transports.Console() ],
});
logger.error(new Error(FakeError)); |
Slick workaround @jeremy-j-ackso |
Suggesting this be closed due to #1576. Can use the formatters. const winston = require('winston');
const { format } = winston;
const logger = winston.createLogger({
format: format.combine(
format.errors({ stack: true }),
format.metadata(),
format.json(),
),
transports: [ new winston.transports.Console() ],
});
logger.error(new Error('FakeError')); |
Yep, Errors should finally be loggable in a nice way for people who want to turn on that functionality! |
While this approach mostly works, it doesn't seem to provide an easy way to log errors in a text format like simple(). It seems like a very common use case, and it would be wonderful if there was a standard, hassle free, and opinionated way of doing it. |
I appreciate the desire for a one-size-fits-all-do-everything-for-me-make-it-magic-dont-make-me-think format. It's an easy trap to fall into, which is how winston@2 and prior release lines had such awful performance compared to other logging libraries. It simply tried to do too much by default to meet the feature demands of a large set of users with varying opinions and tastes. As a project winston@3 embodies a new opinion: push complex formatting into decoupled, single purpose formats and make combining those easy for users. I will admit there is still work to do on documentation, along with high-level explainations and debugging of formats. All that said the errors format suggested reflects the opinion of the project: Can this be implemented as a custom format? |
@DABH why did you closed this issue? Using of formatter suggested by @jeremy-j-ackso does not works without
|
Tested on |
Yeah, I agree with @nosuchip - there doesn't seem to be a good solution if you want to use the
|
So there is still not a solution to this issue? |
@cogscape unfortunately no. Just tested latest winston and no one solution found over internet gave in output error message and stack. Except custom method, of course. |
For TypeScript I am using following snippet to avoid typing warnings:
It works with any other formatters. Reason: attempt to implement current formatter always ends with fail as |
I can't log any meaningful error:
It only logs this sad bit: just using simple is almost better as it produces double the information: What I need:
is there a way to get via winston the information that node print automatically when it catches unhandled error? |
Any updates? |
I noticed that the reason is |
so basically, if I want the stack trace I have to either
|
It's 2021 and still didn't work for Error instances. I'm stopped using this. |
This is an up, as I think this should be taking account. |
This is a very critical issue for our app, we use winston and when errors occur there is no way to see the stack without tinkering the |
I have the same problem |
Hey, guys! |
I expect this to work out of the box without a formatter. |
+1,, would really like to see a fix merged for this issue. Error handling with stack trace option seems relatively worthless if you can't create a log that contains an error message and stack trace. |
My fix for now, pretty ridiculous: |
logger.error("Hi") |
I think this is a duplicate of #1338 where a workaround is posted. To future commenters, please post here only if you are distinguishing the two. |
See my comment here : winstonjs/logform#100 This is an internal bug inside base Transport class, that needs to be adressed. |
Just in case I'm using metadata to log errors, not ideal but it works. |
in 2023 the most popular node.js logging library with 10 million weekly downloads can't log Error instances by default? I'm not even getting a message, just "undefined", nice |
It is open source... You could contribute! |
Here's my workaround : import { Logger } from 'winston'
export const logError = (logger: Logger) => (err: unknown) => {
if (!(err instanceof Error)) {
logger.error(JSON.stringify(err))
return
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { name, message, cause, stack, ...metadata } = err
let _message = stack != null ? stack : `${name}: ${message}`
if (metadata != null) {
_message = `${_message}
${JSON.stringify(metadata)}`
}
logger.error(_message)
} catch (err: unknown) {
logError(logger)(err)
} |
Any update on this? |
Please tell us about your environment:
winston
version? 3.1.0winston@2
winston@3
node -v
outputs: v8.11.3Operating System? Linux
Language? all
What is the problem?
logger.error
called withError
instance doesn't usemessage
andstack
fields, instead logs testerror
.Code example:
What do you expect to happen instead?
At least actual error message logged. Ideally - with stack.
The text was updated successfully, but these errors were encountered: