-
Notifications
You must be signed in to change notification settings - Fork 320
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
feat(promslog): add NewWithWriter() to allow logging other than stderr #686
Conversation
Accepting an `io.Writer` makes it easy to plug-n-play with slog directly while also making the logging output destination configurable. We explicilty make no attempt to manage the underlying writer and leave it to the caller to ensure proper management/cleanup. Example usage to allow writing a JSON file: ```go file, err := os.OpenFile("/tmp/json.log", os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0600) if err != nil { panic(err) } defer file.Close() logger := NewWithWriter(&Config{}, file) logger.Info("I'm writing to a file!", "hello", "world") ... ``` Example output: ```bash ~ cat /tmp/json.log time=2024-09-02T16:43:30.925-04:00 level=INFO source=/home/tjhop/go/src/github.com/prometheus/common/promslog/slog_test.go:162 msg="I'm writing to a file!" hello=world ``` Signed-off-by: TJ Hoplock <[email protected]>
@SuperQ @ArthurSens if you don't mind taking a look when you have time, I'd appreciate it! |
Why not exposing ioWriter publicly instead? |
We could do that! My main thought was that setting the output destination for the logger is a one-and-done situation in log/slog, so it made sense to only allow it to be set at logger creation time. If a user sets the io writer field on a config struct after a logger has already been created, it won't do anything, and so leaving it publicly accessible/settable on the |
I'm not sure we want to have this. We're reasonably opinionated about what and how these things should be used. One major concern, how do we handle log rotations with this file mode? |
I intend to use this to try and dry out some of prometheus'
I'd argue it's the same as current behavior, we're just moving some common functionality out to the lib instead of prometheus helper packages |
Also, while I intend to use it with files, we're obviously accepting an |
Another use -- more easily mimicing the behavior of go-kit/log's We have lots of |
While we do not rotate the query log, we close/reopen it on SIGHUP .. after the rotation you have to reload prometheus. |
Would it make sense to have a
|
Apparently we also need this for Windows. 😞 What about this implementation: #694 |
Ah, I didn't saw this PR.
I explicitly tell people that log rotation isn't a problem the exporter has to solve. Looking at web servers, they don't care about this either. At linux, logrotate should care about that.
Why not just directly initiate slog in tests? If the |
Simple convenience function to return an slog.Logger that writes to io.Discard. Originally suggested by @ArthurSens [here](prometheus#686 (comment)), and requested again by @bboreham [here](prometheus/prometheus#14906 (comment)). As Bryan points out in the comment, there's 147 instances where a discard logger is needed, so a consistent utility function to manage them seems helpful. Signed-off-by: TJ Hoplock <[email protected]>
Simple convenience function to return an slog.Logger that writes to io.Discard. Originally suggested by @ArthurSens [here](#686 (comment)), and requested again by @bboreham [here](prometheus/prometheus#14906 (comment)). As Bryan points out in the comment, there's 147 instances where a discard logger is needed, so a consistent utility function to manage them seems helpful. Signed-off-by: TJ Hoplock <[email protected]>
Accepting an
io.Writer
makes it easy to plug-n-play with slog directly while also making the logging output destination configurable. We explicilty make no attempt to manage the underlying writer and leave it to the caller to ensure proper management/cleanup. Example usage to allow writing a JSON file:Example output: