-
Notifications
You must be signed in to change notification settings - Fork 18k
proposal: log/slog: return Level from Logger or TextHandler #60936
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
Comments
I believe the intention is to use Enabled to test if it's enabled at a given level or above |
doesn't that remove agency from the end user? I guess you could do a loop and test Enabled for each value, but Level is an |
what's the use case for it? if logger.Enabled(debug) { |
If I have a certain
I originally did something with |
I don't see why your usecase can't work with Enabled,
and with Enabled, you get more consistent output it your users pass in intermediate levels like debug+2 |
FWIW, I would find it quite surprising if changing the log level caused the content of one particular message to change, rather than just deciding whether or not each message is included at all. Of course I don't know the details of your application so perhaps yours is a valid exception for an unusual situation, but it does seem like an odd behavior to encourage by exposing it directly in the standard library. The current API design encourages robust implementation of the common case of using the levels only to filter out entire log messages below a certain watermark. If this were accepted, perhaps it would warrant a note in its doc comment explaining that |
CC @jba |
@4cq2 I expect I would design that such that the top-line information like the method and URL gets written out as an "info" message, and then the full request header and body gets separately written out as another "trace" message, so that if I'm using info-level logging I see only the first message but can enable trace to see the other. With that said, I'm not meaning to suggest that what you want to do is wrong, only that I would find it surprising when I bring my experience with other software. Specifically, a common thing I'll do when debugging is to first log at a relatively high level to get a broad overview of what's going on, and then gradually get more granular until I find the information I need. In this process I rely on being able to find the same log lines I saw on previous runs as an anchor to reduce the number of more-verbose log lines I need to read through. If the messages I saw before were suddenly different when I selected a more granular level, I expect I'd be skeptical about whether I'm still observing the same behavior I was observing on the previous run. This is a subjective thing and I expect others work with application logs differently, so I'm not meaning that my perspective is universal. If others have different expectations than I do then I've no problem with that. |
@apparentlymart thats a good idea. I think I was avoiding it, because it would result in something like this:
instead of just a single Log call. I realized that the https://godocs.io/log#pkg-constants which means the other (32-7) or (64-7) bits are available (for now) to sneak some state in. so I came up with the below ugly code, which might do the job. I guess this issue can probably be closed, I will leave it up for another day in case someone thinks its worth pursuing. package main
import "log"
type hello struct{}
func (hello) String() string {
switch log.Flags() >> 9 {
case 1:
return "basic stuff"
case 2:
return "extra info"
case 3:
return "moooooooooooooooooooooooooooooooooooore info"
}
return ""
}
func main() {
log.SetFlags(1<<9)
log.Print(hello{})
log.SetFlags(2<<9)
log.Print(hello{})
log.SetFlags(3<<9)
log.Print(hello{})
} |
@4cq2, I don't have much to add to the other answers here, but I can say that we deliberately do not have something like If you control the application, you can wrap your handlers in a custom handler that knows its level, like the example LevelHandler. Just add a |
slog.HandlerOptions
has a fieldLevel
which sets the minimum level that will be logged. this value is stuffed inside aslog.TextHandler
, which is in turn stuffed inside aslog.Logger
:https://pkg.go.dev/log/slog#example-HandlerOptions-CustomLevels
however it seems the
Level
cannot currently be returned fromslog.TextHandler
:https://pkg.go.dev/log/slog#TextHandler
or
slog.Logger
:https://pkg.go.dev/log/slog#Logger
is this an oversight, or is the ability to return the current level from these types intentionally not available for some reason? or did I overlook some method to get that information?
The text was updated successfully, but these errors were encountered: