-
Notifications
You must be signed in to change notification settings - Fork 34
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
Allow to configure writers different from zerolog.ConsoleWriter #41
Comments
Hi @pscheid92 , I found that it's possble to overwrite the logger by logger.WithLogger(func(_ *gin.Context, l zerolog.Logger) zerolog.Logger {
return l.Output(gin.DefaultWriter).With().Logger()
}), For more detail, you can check my pull request #45 |
Hey @a180285, excuse my late response and thank you very much for your PR! 👍 To me, it feels like forcing the API to do what we want, though:
It doesn't seem too elegant to me 😅 . I would have expected We could try to find a more elegant and profound solution. |
@appleboy What do you think? Would you be open to the aforementioned changes? |
I would also expect to be able to use For example, using the above workaround wrapped in a function func MyDefaultLogger() logger.Option {
return logger.WithLogger(func(_ *gin.Context, l zerolog.Logger) zerolog.Logger {
return l.Output(gin.DefaultWriter).With().Logger()
})
} I would then expect to be able to do buf := &bytes.Buffer{}
logger.SetLogger(MyDefaultLogger(), logger.WithWriter(buf)) The buffer is empty however and logs continue to be written to |
for those interested in having json structured logging instead of having the colorful log. I found this article that explain everything : https://learninggolang.com/it5-gin-structured-logging.html |
Use Case
I want structured logging in JSON format (like the default
zerolog
) instead of the human-friendlyConsoleWriter
format.Analysis
The current implementation hard-codes a
ConsoleWriter
. Other configurations are overwritten.logger/logger.go
Lines 60 to 69 in 1a33159
Ideas
Make the writer configurable
We could allow a user to overwrite the writer instead of hard-coding it.
ConsoleWriter
still could be a good fallback.Allow configuration by
gin.Mode
We could allow different configurations according to the selected mode. For instance one could set
ConsoleWriter
forgin. DebugMode
but the default JSON-format writer forgin.ReleaseMode
. Maybe someone wants a certain writer for test ingin.TestMode
.Provide defaults for the standard
gin.Mode
sIf we implemented (2) we could provide defaults for the different standard
gin.Mode
s.The text was updated successfully, but these errors were encountered: