Skip to content

Conversation

@alexandear
Copy link
Collaborator

@alexandear alexandear commented Mar 25, 2025

This PR replaces usages of log to log/slog as discussed in #1278.

Now, the logger outputs to stderr and revive.log simultaneously if DEBUG environment variable is defined and not empty.

Example:

$ DEBUG=1 go run main.go
time=2025-03-25T13:04:29.971+02:00 level=INFO msg="Logger initialized" logFile=revive.log
time=2025-03-25T13:04:29.971+02:00 level=INFO msg="Config loaded" rules="[time-naming unexported-naming context-keys-type filename-format useless-break package-comments unused-parameter var-declaration unreachable-code unexported-return line-length-limit range increment-decrement indent-error-flow empty-lines use-any error-strings enforce-slice-style error-naming bare-return receiver-naming dot-imports context-as-argument errorf empty-block exported var-naming enforce-map-style error-return redefines-builtin-id blank-imports superfluous-else]"

For discarding output, I use slog.NewTextHandler(io.Discard, nil) because slog.DiscardHandler exist only in Go 1.24.

Copy link
Contributor

@ccoVeille ccoVeille left a comment

Choose a reason for hiding this comment

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

LGTM 👍

Only minor feedbacks and a question

debugModeEnabled := os.Getenv("DEBUG") != ""
if !debugModeEnabled {
// by default, suppress all logging output
return slog.New(slog.NewTextHandler(io.Discard, nil)), nil
Copy link
Contributor

Choose a reason for hiding this comment

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

BTW, is there a linter about slog (not necessarily in revive) about replacing slog.NewTextHandler(io.Discard, nil) ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I think no, it's not.

Copy link
Contributor

@ccoVeille ccoVeille Mar 25, 2025

Choose a reason for hiding this comment

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

Maybe we should think about it

https://github.com/search?q=%22slog.NewTextHandler%28io.Discard%2C+nil%29%22&type=code

What would be the best candidate? Revive?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Perhaps it's go-critic

Copy link
Contributor

@ccoVeille ccoVeille Mar 25, 2025

Choose a reason for hiding this comment

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

BTW, is there a linter about slog (not necessarily in revive) about replacing slog.NewTextHandler(io.Discard, nil) for Go 1.24+ ?

Perhaps it's go-critic

What do you think about this Oleg ?

@cristaloleg

Choose a reason for hiding this comment

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

Yep, this can be easily done via ruleguard rule. Feel free to submit an issue (or even a PR).

Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Contributor

Choose a reason for hiding this comment

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

I consider sloglint as a possible candidate also

https://github.com/go-simpler/sloglint

@tmzane do you think there could be an interest in sloglint to report people using

slog.NewTextHandler(io.Discard, nil) for Go 1.24+, while slog.DiscardHandler is available

Apparently, it's massively used

https://github.com/search?q=%22slog.NewTextHandler%28io.Discard%2C+nil%29%22&type=code

If you have interest for this, I will open an issue in your project
If you don't, I will open an issue in go-critric and will code it I think.

Copy link
Contributor

Choose a reason for hiding this comment

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

Hi, I don't mind adding it to sloglint as in slog-focused linter, seems like a useful and easy-to-implement check. Feel free to open an issue, thanks

Copy link
Contributor

Choose a reason for hiding this comment

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

@denisvmedia
Copy link
Collaborator

Thank you!

@denisvmedia denisvmedia merged commit 561c949 into mgechev:master Mar 25, 2025
8 checks passed
@alexandear alexandear deleted the change/log-slog branch March 25, 2025 17:00
mfederowicz pushed a commit to mfederowicz/revive that referenced this pull request Apr 18, 2025
* change: replace log with log/slog for logging
* add TODO about slog.DiscardHandler
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.

5 participants