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

Updates to slog to make it the default feature-full logger #127

Merged
merged 8 commits into from
Mar 7, 2024

Conversation

samlaf
Copy link
Collaborator

@samlaf samlaf commented Feb 24, 2024

Started looking into slog:

and agree with @dmanc at this point that we should probably start adopting slog as our standard logger everywhere. Been playing with it a bit and our current interface is super limiting wrt what can be done with slog (for eg can't pass a context to logging functions right now, which can be used to also log traceIds). The blog above mentions this
image

This draft PR is a wip for adding features to the slogger itself. But we'll eventually need to find a way to adapt our main interface, probably in a next PR. This will require more discussions.

Features added thus far:

  • Only print required log level (production logger will only print info and above logs, whereas the development one will also print debug logs) 4c03592 Found a much easier way to do this: df441e7
  • Separate constructor into two, one for TextHandler and one for JsonHandler (I'm personally becoming a big fan of golang's key-value text format) 52c50b7
  • Print proper source file information (see image below*) 52c50b7
  • make the slog logger an embedded struct (instead of named field) so that we have access to its method. Also return the SLogger struct instead of Logger interface from constructors so that users can use those methods (.WithGroup for eg) d5b616a
  • because of our log interface and structs that wrap the underlying logger, the srouce information getting printed was always the wrapper location, instead of its caller, which is useless:
    image

@samlaf samlaf requested a review from shrimalmadhur February 24, 2024 16:36
…d code for proper source info logging in dev mode
@samlaf samlaf requested a review from dmanc February 24, 2024 18:00
@samlaf samlaf marked this pull request as draft February 24, 2024 18:03
@samlaf samlaf changed the title added level handler to slog Updates to slog to make it the default feature-full logger Feb 24, 2024
Copy link

@dmanc dmanc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another thing that could be nice is to have the option to write logs to a file in addition to stdout.

logging/slog_logger.go Outdated Show resolved Hide resolved
@samlaf
Copy link
Collaborator Author

samlaf commented Feb 26, 2024

Another thing that could be nice is to have the option to write logs to a file in addition to stdout.

Check bded386
Made the io.Writer an argument to the constructors. So to write to both stdout and a file, you can just pass a io.MultiWriter(os.Stdout, file).

// but can also use a io.MultiWriter(os.Stdout, file) to write to multiple outputs)
// logLevel is the minimum level to log
// logSource if true, adds source information to the log
func NewSlogTextLogger(outputWriter io.Writer, logLevel slog.Level, logSource bool) *SLogger {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel this should expect a log config which should have: env, log level etc - whatever is needed.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shall we take the entire slog.HandlerOptions as input then?
So NewSlogTextLogger(outputWriter io.Writer, handerOptions slog.HandlerOptions)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that makes sense or something like

type SLoggerConfig struct {
 slog.HandlerOptions
 OutputWriter io.Writer
}

@shrimalmadhur wdyt?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See 2bfeb3f

ian-shim
ian-shim previously approved these changes Mar 5, 2024
logging/slog_logger.go Outdated Show resolved Hide resolved
logging/slog_logger.go Show resolved Hide resolved
@samlaf samlaf marked this pull request as ready for review March 5, 2024 23:18
// logSource if true, adds source information to the log
func NewSlogTextLogger(outputWriter io.Writer, logLevel slog.Level, logSource bool) *SLogger {
handler := slog.NewTextHandler(outputWriter, &slog.HandlerOptions{
Level: logLevel, // prints debug and above
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: // prints this level and above

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// but can also use a io.MultiWriter(os.Stdout, file) to write to multiple outputs)
// logLevel is the minimum level to log
// logSource if true, adds source information to the log
func NewSlogTextLogger(outputWriter io.Writer, logLevel slog.Level, logSource bool) *SLogger {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that makes sense or something like

type SLoggerConfig struct {
 slog.HandlerOptions
 OutputWriter io.Writer
}

@shrimalmadhur wdyt?

@ian-shim ian-shim merged commit 01719ce into master Mar 7, 2024
3 checks passed
@ian-shim ian-shim deleted the feat-slog-level-handler branch March 7, 2024 00:00
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 this pull request may close these issues.

4 participants