-
Notifications
You must be signed in to change notification settings - Fork 575
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
Is it possible to re-use a logger writers for a new logger\add a new output to an existing logger? #361
Comments
You have both stdout and stderr directed to your terminal with logs going to both at the same time so it gets mixed up. |
Indeed they all go the terminal, it was expected to get multiple times the same log event (INFO 1 and INFO 2) but what I am not understanding is that I expected the output to be:
Notice how the posted output in the first comment encapsulate the os.Stdout\os.Stderr INFO 2 log event in a log event with no level and only message Am I misunderstanding your explanation maybe? Thanks 🙏 I've updated the first comment with details. I am incline to confirm that re-using a logger output causes the logger to wrongly handle the logging events. Here I create a https://play.golang.org/p/I2afld8Mv-n func main() {
consoleWriter := zerolog.ConsoleWriter{Out: os.Stderr}
consoleWriter.NoColor = true
logger := zerolog.New(os.Stdout)
logger.Info().Msg("INFO 1")
multi := zerolog.MultiLevelWriter(logger, consoleWriter)
logger = logger.Output(multi)
logger.Info().Msg("INFO 2")
}
|
You are using a logger as a Note that you are not "re-using" a logger. Loggers aren't pointers so each logger is a copy. |
Thanks for the clarification on the internals of zerolog, it made it more clear. So the questions I'm left with are:
In both cases we don't have access to the writers used to create the loggers. For instance:
|
A writer will get |
I'm sorry but I'm getting confused, is there a way to create a new logger from an existing one and append a new writer or there isn't? Please see my questions above:
|
You can set a writer. This new writer can be a multi writer. You need to store the other writers if you want to append it, as a logger does not expose its internal writer. |
Ah that's it! So a logger writers can't be re-used elsewhere from where they have been defined if not stored somewhere. Thanks for the clarification! I believe it might be a niche but it would be neat if there was a method to expose a logger writers to reuse them or have |
I'm currently struggling with this issue as well. In my use case, I'm trying to compile all the error logs from a request/event into an email and then send it to an administrator. This makes it easy to quickly troubleshoot an urgent issue without having to query a large error log database using only the trace ID. I'm trying to solve that by adding a bytes.Buffer to the logger to record the series of error logs. Not sure if there is a better way to do it. |
@haveachin I think your use case might be addressed with #583 as well. |
Thanks to the explanation in #361 (comment) the following is now not relevant. Please skip to #361 (comment)
Down the line of execution I'd like to append a new log output to an existing logger. I've tried to play around with the following code and while the log messages are correctly redirected to all the writers a weird formatting happens.
Namely the new log events are logged with no level and with message the whole log message repeated as shown below.
I'm incline to think that I am missing something subtle in the zerolog package.
Example snippet https://play.golang.org/p/F7ofHBDVsLU:
And its output:
Expected output:
The text was updated successfully, but these errors were encountered: