-
Notifications
You must be signed in to change notification settings - Fork 71
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
Shouldn't throw exception when it fails to log to logstash? #35
Comments
Could you provide more details like which winston version you are using? |
I was using the latest version of winston / winston-logstash as of Aug 16.. I've removed winston-logstash already so I can't remember exactly which version it was.. Sorry! |
I seem to be having the same issue. I'm using the winston-logstash@0.2.11 |
+1 |
1 similar comment
+1 |
If you want to handle logstash errors, you can do so like this:
|
Thanks for the info. The reason why I reported this issue is not that I couldn't handle the error, but that winston-logstash should not cause application to crash under any circumstance; unhanded logging error / log file missing / log file full / can't write to log, etc, etc.. |
thank you @joshje , I fixed it with that in my project |
I agree, the transport should not crash the application in any scenario. @jaakkos using the proposed workaround suggested by @joshje no longer works. is there a solution? (except hammering the server with retries indefinitely) |
Hmmm, I think it depends on what kind of application it is and in what kind of environment. For example, if you are running an application handling money transactions, I would prefer it wouldn't run if the logging weren't working, and I feel it's better to close the system than keep it running. Another hand, if it's some hobby project which does something not essential and it's known that logging might sometimes fail, and it's accepted, then it's just fine. I think I'll approach the issues with the following steps:
Any thoughts? |
you have a valid point, but still, the developer should have the ability to at least choose if to allow crashing of the app, or not. |
@nitzan-blink btw which Winston version do you use? I am not sure if |
I'm using Winston 3.8.2. |
It seems that when using Winston 3.x the ```on('error')´´´ works but with the 2.X the stream pipping seems to be broken. Not sure why and when might be related to changes to Winston 2.X. @nitzan-blink if you check the PRs test-bench/winston-3x/test/on_error_test.js |
@jaakkos sorry but I'll have to limit our discussion to winston 3.x since I only use it. my test env looks like:
and
when running with no logstash instance on "127.0.0.1", the app crashes, and I get this on console:
|
You should add the const logger = createLogger({ . . . your configuration and passing the LogstashTransport . . .})
logger.on('error', (error) => {
// handle errors in the logger here
console.log('Error from logger:', error)
});
logger.info('foo', 'bar'): Then it should not throw the exception because there are listeners for the error event. |
@jaakkos when used like that, the app doesn't crash.
|
That workaround was for Winston >=2.x and with the Winston 3.x the same logic doesn't work because the Winston implementation is different. Did you migrate from 2.x to 3.x or what was your path in this case? There are quite many changes when migrating to 3.x, have you checked the https://github.com/winstonjs/winston/blob/222c86341df7cb8065d6fa0f78de50a445db2e4b/UPGRADE-3.0.md#exceptions--exception-handling
My thoughts; It says on the documentation
I am not sure what handled exceptions means in this context. How I would understand it is; if some exception is thrown in the application code, but there is a catch block somewhere to catch the exception, the logger will write the exception to log and try to continue. How this will work will be application-specific logic combined with Winston. There isn't any error case if logging itself is not working. For errors happening inside the Winston itself and this case, in the transport, you should listen to the error from the logger with Did you catch my idea of the separation of duties? disclaimer 😉 |
I didn't migrate from v2, I just started using in v3.
tried looking closer into what you wrote, and it seems that this
is a typo:
so that flag apparently is not related to inner winston exceptions, but to broader exception handling with/without winston, and in any case irrelevant for the issue I'm facing (please correct me if I'm wrong, and you understood differently).
seems like this is the only solution we currently have.. |
If you check the implementation of that feature in the Winston, it's attaching a process exit hook which prevents the exit; I feel that I wouldn't use that approach in any production code because it could leave the application in an unpredicted state which is in my experience always a bad situation to be. You always have multiple a solution, as this is open source project 😄
While this is a relatively complex transport with an internal state and IO, I think the current solution is the yet best approach to network failure. Transport is transparent about the errors and lets the user decide what to do in the case of an error. Suppose the developer chooses not to address them, it fallback to a safe default, an exception that stops the process. I think this way; it's simple but not always easy. This is my rationale for the functionality. |
I'll close the issue and write that rationale in the README. |
My application crashes if winston-logstash fails to connect to the logstash.
In my own opinion, a logging library shouldn't cause the application to crash (don't throw exception) if it fails to log... but I am not sure if the author of this library agrees with that or not?
The text was updated successfully, but these errors were encountered: