Skip to content
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

Closed
pscheid92 opened this issue Oct 18, 2022 · 5 comments · Fixed by #45
Closed

Allow to configure writers different from zerolog.ConsoleWriter #41

pscheid92 opened this issue Oct 18, 2022 · 5 comments · Fixed by #45

Comments

@pscheid92
Copy link

Use Case
I want structured logging in JSON format (like the default zerolog) instead of the human-friendly ConsoleWriter format.


Analysis
The current implementation hard-codes a ConsoleWriter. Other configurations are overwritten.

logger/logger.go

Lines 60 to 69 in 1a33159

l := zerolog.New(cfg.output).
Output(
zerolog.ConsoleWriter{
Out: cfg.output,
NoColor: !isTerm,
},
).
With().
Timestamp().
Logger()


Ideas

  1. 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.

  2. Allow configuration by gin.Mode
    We could allow different configurations according to the selected mode. For instance one could set ConsoleWriter for gin. DebugMode but the default JSON-format writer for gin.ReleaseMode. Maybe someone wants a certain writer for test in gin.TestMode.

  3. Provide defaults for the standard gin.Modes
    If we implemented (2) we could provide defaults for the different standard gin.Modes.

@a180285
Copy link
Contributor

a180285 commented Dec 29, 2022

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

@pscheid92
Copy link
Author

Hey @a180285, excuse my late response and thank you very much for your PR! 👍
This indeed works as a workaround! 🥳

To me, it feels like forcing the API to do what we want, though:

  1. Create a logger with default output zerolog.New(cfg.output) (default cfg.output = gin.DefaultWriter)
  2. Overwrite the output to a zerolog.ConsolteWriter
  3. Overwrite the output to gin.DefaultWriter (done by your code snippet above)

It doesn't seem too elegant to me 😅 .

I would have expected logger.WithWriter() to be a shorter solution, but it doesn't work.
Step 2 overwrites it again, which looks like a bug to me.

We could try to find a more elegant and profound solution.
WDYT 🤔

@pscheid92
Copy link
Author

pscheid92 commented Jan 17, 2023

@appleboy What do you think? Would you be open to the aforementioned changes?
If so I would try to come up with a more detailed proposal.

@david-mkl
Copy link

I would also expect to be able to use WithWriter in order to set the writer later, but that doesn't seem to work as I would expect.

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 gin.DefaultWriter. Calling WithWriter seems to have no effect.

appleboy pushed a commit that referenced this issue Feb 1, 2023
@Tommy-42
Copy link

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
which is exactly doing what I want.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants