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

log/slog: structured, leveled logging #56345

Open
jba opened this issue Oct 20, 2022 · 767 comments
Open

log/slog: structured, leveled logging #56345

jba opened this issue Oct 20, 2022 · 767 comments

Comments

@jba
Copy link
Contributor

jba commented Oct 20, 2022

We propose a new package providing structured logging with levels. Structured logging adds key-value pairs to a human-readable output message to enable fast, accurate processing of large amounts of log data.

See the design doc for details.

@jba jba added the Proposal label Oct 20, 2022
@gopherbot gopherbot added this to the Proposal milestone Oct 20, 2022
@jba

This comment has been hidden.

@fsouza

This comment has been hidden.

@jba

This comment has been hidden.

@mpx
Copy link
Contributor

mpx commented Oct 22, 2022

This is a huge API surface without any real production testing (AIUI). Perhaps it might be better to land it under golang.org/x for some time? Eg, like context, xerrors changes.

@seankhliao
Copy link
Member

It's available under golang.org/x/exp/slog

@mpx

This comment has been hidden.

@deefdragon
Copy link

I love most of what this does, but I don't support its addition as it stands. Specifically, I have issues with the option to use inline key-value pairs in the log calls. I believe the attributes system alone is fine. Logging does not need the breakage that key-value args like that allow.

The complexity in the documentation around Log should be a warning sign.

...
The attribute arguments are processed as follows:

  • If an argument is an Attr, it is used as is.
  • If an argument is a string and this is not the last argument, the following argument is treated as the value and the two are combined into an Attr.
  • Otherwise, the argument is treated as a value with key "!BADKEY".

The suggestion was that potential problems with key-value misalignment will all be solved by vet checks. As I mentioned in this thread of the discussion, relying on vet should be viewed a warning as to potential problems with the design, not a part of the design itself. Vet should not be a catch-all, and we should do what we can to avoid requiring vet warnings to safely develop go.

An accidentally deleted/missed or added/extra argument in key value logging would offset the keys and values after it. That could easily bog down a logging system trying to index all the new "keys" it is getting. It could also lead to data being exposed in a way that it should not be.

I acknowledge that neither of these examples are all that likely in a well defined system, or somewhere with good practices around code reviewing etc.. But they are possible.

@hherman1
Copy link

@deefdragon Doesn't this concern apply to Printf as well? Is the difference the dependency on these logs by external systems..?

@prochac
Copy link

prochac commented Oct 24, 2022

Based on that the Go standard library is very often being recommended as source of idiomatic code, and this package aspires to be merged as part of it, I would like you explain to me the involvement of context package.

If some object uses logger, isn't it its dependency? Shouldn't we make the dependencies explicit? Isn't this logger smuggling bad practice? If passing the logger by context is idiomatic, is *sql.DB too?

Why has the logger stored context? It violates the most famous sentence from the documentation for context

Contexts should not be stored inside a struct type, but instead passed to each function that needs it.

Logger in context containing its own context inside ...

Frankly, I'm a bit confused.

@deefdragon
Copy link

@hherman1 The same concern does apply to printf, tho it's not as bad compared to logging. With printf, most statements are consumed as a single chunk, and only consumed locally by the programmer. Being misaligned is easy enough for a human to notice, parse, and correct.

In the case of Sprintf, where it might not be consumed by the programmer, and instead be used as an argument to something, the "testing" during development that is making sure the program starts would likely catch most misalignment.

Being off by one in a log is much harder to catch as it has no real impact in the program's execution. You only notice there is an issue when you have to go through your logs.

@mminklet
Copy link

mminklet commented Oct 25, 2022

I think I share some of @prochac 's concerns regarding context. Maybe I'm being a bit of a luddite, but recommending that the logger is passed around inside context rather than via explicit dependency injection, smells a bit funny to me. Context, from what I have always followed, is for request-scoped information, rather than dependencies. And the more clarity surfacing dependencies the better. IE just assuming the right logger is in context, and getting the default one because it's still getting some logger

@v3n
Copy link

v3n commented Oct 25, 2022

Maybe I'm being a bit of a luddite, but recommending that the logger is passed around inside context rather than via explicit dependency injection, smells a bit funny to me. Context, from what I have always followed, is for request-scoped information, rather than dependencies.

I think there are two approaches here:

  • For long-lived process (think the kind of thing that you would pass context.Background() to); I would absolutely recommend dependency injected loggers. This would include situations where a context logger might be available, but may use fan-in/singleflight to demux requests. For these cases, the logged is frequently implemented as a 'child logger', which in some frameworks allows you to adjust the log level per-child.

  • However, on the other hand, many APM services have a "log correlation" features (this is even part of the spec for OpenTelemetry). In this situation, you want your context to be propagated by the logger; as the correlation fields would be prebound to the logger and propogated down the stack.

I've used both patterns frequently in high-scale production services; and both have their places. I'd definitely like to see slog promote context-propagated logging as the observability benefits are huge.

@mminklet
Copy link

mminklet commented Oct 25, 2022

Appreciate your explanation @v3n . I'm still having a slightly hard time understanding the benefit of the logger itself being passed around in context. I understand propagating the log correlation information via context, and we currently use the otelzap implementation that does this sort of thing via ErrorContext(ctx, ...) etc logging methods. I like the WithContext methods proposed here, passing the context to the logger, in similar fashion. It's more the logger itself being passed around inside the context that feels a bit odd to me

The zap and otelzap libraries do allow for the same kind of thing, whereby you can get the logger from context etc (and I'm sure others do), it's just this being in the std library it's more of a recommendation for this kind of pattern

@seankhliao
Copy link
Member

I still want a standard handler for testing.TB.Log like https://pkg.go.dev/github.com/go-logr/logr@v1.2.3/testr#New

@jba
Copy link
Contributor Author

jba commented Oct 25, 2022

Being off by one in a log is much harder to catch

@deefdragon, we'll have a vet check for that.

@jba
Copy link
Contributor Author

jba commented Oct 25, 2022

I still want a standard handler for testing.TB.Log like https://pkg.go.dev/github.com/go-logr/logr@v1.2.3/testr#New

@seankhliao, such a handler seems easy to write, and it's not clear to me yet whether there is enough demand to include it. Let's hold off for now; we can always add it later.

@jba
Copy link
Contributor Author

jba commented Oct 25, 2022

Why has the logger stored context? It violates the most famous sentence from the documentation for context

Contexts should not be stored inside a struct type, but instead passed to each function that needs it.

@prochac, that is a design principle, not a hard-and-fast rule. It is there to steer people away from buggy code, but that has to be weighed against other factors. In this case, we knew that passing tracing information to logging was an important feature, but we didn't want to add a context argument to every log output method. This was our solution.

@jba
Copy link
Contributor Author

jba commented Oct 25, 2022

Context, from what I have always followed, is for request-scoped information

@mminklet, scoping a logger to a request is a common pattern, and is probably the main application of the ability to add a Logger to a context. It doesn't preclude dependency injection; if that works for you, stick with it.

@amnonbc
Copy link

amnonbc commented Oct 25, 2022

This is a significant proposal. @jba can you do a video talk on this. And, perhaps, a blog post?

@deefdragon
Copy link

Being off by one in a log is much harder to catch

@deefdragon, we'll have a vet check for that.

@jba As I said in my original post, I don't think that's a good solution.

relying on vet should be viewed a warning as to potential problems with the design, not a part of the design itself.

@fsouza
Copy link
Contributor

fsouza commented Oct 25, 2022

I like this in general. One API nit from an experiment in s3-upload-proxy: it would be good to have a way to convert a string into to the level (say you want to allow users set an environment variable like LOG_LEVEL=debug and have that translated to DebugLevel).

Other libraries (logrus, zerolog, zap) call that function ParseLevel (for zap it's ParseAtomicLevel, but same principle).

@jba
Copy link
Contributor Author

jba commented Oct 25, 2022

can you do a video talk on this.

Done.

@AndrewHarrisSPU
Copy link

Other libraries (logrus, zerolog, zap) call that function ParseLevel

The additional '+'/'-' terms put a twist on this, I think it'd be nice to have. (I had this laying around: https://go.dev/play/p/Izwzgd8Kmc9)

@amnonbc
Copy link

amnonbc commented Oct 26, 2022

Three comments on the proposal:

One thing which irritated me with zap was the existence of both sugared and attribute log methods.
This doubled the API surface, and created a coding style balkanisation of packages that used zap as their logger.
In Go there should be one, and preferably only one way of doing things.
slog does unfortunately replicate this duplication.

My second observation is that we have 10 Attr constructors, one for each common type we want to log, + any.
Could we have used Generics to reduce the API surface here?
But if we are going to have explicit Attr constructors, then I would like one which logs an error, one that logs a stringer,
and one which logs a []byte as a string.

Finally, I think it is very good (but perhaps overdue) that we are moving towards a canonical production strength logging library in stdlib. Most libraries need some level of logging, if only for debugging. And not having a standard interface of
sufficient power meant a lot of pain. If you want to import package foo, you also need to pull in the weird and wonderful
logging library that they use, construct a logger of the right type to pass to it, and arrange for the output to somehow
be integrated into the logging output and config used by the rest of your system. I have done this myself far too many
times and it quickly gets tedious. So great that this will probably soon move into the stdlib,
and that new packages will eventually start adopting it. If only we had had it earlier, much aggravation would have been saved.

@jba
Copy link
Contributor Author

jba commented Oct 26, 2022

slog does unfortunately replicate this duplication.

I disagree. There is only one Attr-based output method, LogAttrs. It is there as an optimization for those who need it. Most users will be fine with the other five output methods. (And yes, there is also the extra LogAttrsDepth method, but that will be even more rarely used.)

Could we have used Generics to reduce the API surface here?

The answer is, "hopefully." With the current implementation, you can only reduce the API surface at a considerable time penalty. But that may change before this API is frozen. See this item in the discussion.

I would like [an Attr constructor] which logs an error, one that logs a stringer,
and one which logs a []byte as a string.

According to my analysis, zap.Stringer uses are 2% of all constructors, and zap.ByteString is at 0.3%. I don't think those numbers warrant more API. (But we can always add them later if I'm wrong.) zap.Error calls are quite common, but we believe that the error argument to Logger.Error will absorb most of those.

@rsc
Copy link
Contributor

rsc commented Oct 26, 2022

This proposal has been added to the active column of the proposals project
and will now be reviewed at the weekly proposal review meetings.
— rsc for the proposal review group

@rabbbit
Copy link

rabbbit commented Oct 26, 2022

Why has the logger stored context? It violates the most famous sentence from the documentation for context

Contexts should not be stored inside a struct type, but instead passed to each function that needs it.

@prochac, that is a design principle, not a hard-and-fast rule. It is there to steer people away from buggy code, but that has to be weighed against other factors. In this case, we knew that passing tracing information to logging was an important feature, but we didn't want to add a context argument to every log output method. This was our solution.

I'm with @prochac and @mminklet in that passing logger in context seems awkward.

However, I see your point about context propagation and @v3n point:

I'd definitely like to see slog promote context-propagated logging as the observability benefits are huge.

But then context-propagated logging and context-propagated logg**er** are slightly different things.

@jba (apologies if this was already discussed in #54763, I tried to & failed to find it there) did you consider something like context-propagated log-context, as opposed to propagating the whole logger?

I frequently wish I could do easily is adding log-context to an already existing logger, like:

logger = slow.WithContext(ctx).With("method", r.Method ...)

.....

err := doSomething(c ctx) err {
   doSomethingElse(c ctx) {
        somethingVeryDeep(c ctx) {
             // do some other work
             slog.AddToContext(ctx, "workResult", ok)
        }
        somethingVeryDeepAgain(c ctx) {
             // do some work
             slog.AddToContext(ctx, "otherWorkResult", ok)
        }
   } ()
} ()
if err != nil {
    logger.Error("requestFailed") // `slog` extract values for me.
}

This would allow me to log once per request/error instead of tens of scattered log-lines which I then need to correlate with request/span ids.

I think this could also support https://go-review.googlesource.com/c/proposal/+/444415/5/design/56345-structured-logging.md#754

ctx, span := tracer.Start(ctx, name, opts)

since tracer.Start could add the context it needs to the log-context on the context.

This would likely require changes to the context package to support this in a performant way, but stdlib wants to support context+logging+tracing natively perhaps context can be adapted too? Maybe changes won't be necessary.

@AndrewHarrisSPU
Copy link

There may be a hint of namespace clobbring in ReplaceAttr, where matching on a key string is invoked before group prefixes are applied (example: https://go.dev/play/p/yFNXLz3gklD).

I think it's a reasonable behavior; the version of ReplaceAttr that is attentive to group prefixing doesn't seem like it would be very robust, leading to situations where it's not possible to add a Group downstream without breaking a replacement policy (or update a package because their logger added a Group, etc.). Still I wonder if it's worth noting in documentation.

A version of ReplaceAttr in Handler middleware might offer some flexibility. I really have no opinion on whether that component should be in slog - it's not very hard to implement, and eventually precisely tailoring some more elaborate replacement policies or hooks or whatever seems like it should be written outside of slog.

@biohazduck

This comment was marked as duplicate.

@paluszkiewiczB

This comment has been hidden.

@seankhliao
Copy link
Member

@paluszkiewiczB -trimpath build flag

@fgimian

This comment was marked as duplicate.

@TriAnMan

This comment was marked as duplicate.

@wizhi

This comment was marked as duplicate.

@cooldarkdryplace
Copy link

Hi, thanks for adding structured logging. This will eliminate many unnecessary dependencies in our code.

@jba, I have a question about style. In the proposal examples, all log messages start in lowercase, while the Google style guide suggests starting a log message in uppercase.

What I am talking about is:

  • slog.Info("hello", "count", 3)
    VS
  • slog.Info("Hello", "count", 3)

Style guide reference: https://google.github.io/styleguide/go/decisions#error-strings

the style for the full displayed message (logging, test failure, API response, or other UI) depends, but should typically be capitalized.

I was not able to find any other recommendations in:

Are there any suggestions, or should this be an internal team decision?

@jba
Copy link
Contributor Author

jba commented May 10, 2023

@cooldarkdryplace, it should be a team decision.

@lmittmann
Copy link

@jba Both Record.source and Attr.isEmpty would be useful when building custom handlers. Especially Attr.isEmpty contains quite important logic when trying to follow the logic of excluding attributes from being logged of the Text and JSON handlers. What do you think about making them exported?

@jba
Copy link
Contributor Author

jba commented May 11, 2023

@lmittmann, I'm not opposed to exporting Record.source, although it's easy to write yourself. Attr.isEmpty is easily written as a.Equal(Attr{}), which is probably clearer.

Any API changes require a proposal at this point. Feel free.

@telemachus
Copy link

telemachus commented May 11, 2023

@lmittmann, I'm not opposed to exporting Record.source, although it's easy to write yourself. Attr.isEmpty is easily written as a.Equal(Attr{}), which is probably clearer.

@jba My initial thought was that a.IsEmpty() was simpler and more self-documenting than a.Equal(slog.Attr{}). I'm curious: if you feel the other way, why implement Attr.isEmpty for slog internally? (Were the rules for "emptiness" originally distinct from what they are now?)

@jba
Copy link
Contributor Author

jba commented May 11, 2023

why implement Attr.isEmpty for slog internally?

It's probably a hair more efficient, though I didn't benchmark it. Call it a premature optimization.

@cuishuang
Copy link
Contributor

I would like to ask: As time goes by, the written log file will expand infinitely, and it needs to be divided according to size or time (maybe there is compression to save disk space), will log/slog or other standard library consider providing such a function in the future? Similar to https://github.com/natefinch/lumberjack

@bangzek
Copy link

bangzek commented May 13, 2023

I might miss some comment but I search and there's no mention on how best to use slog on library. The simplest thing I could think would be for library to have package level *slog.Logger variable and has some function to let user of that library to supply their own logger so by default nothing is logged when that variable is not set.

But currently slog code doesn't allow nil Logger for Debug(), Info() and the rest.
From my little peeking under the hood it's a simple fix, just add some nil check to Logger's Enabled method.

So I wonder why is it like that?
Is there a good reason for it or just some simple thing that got missed?

@jba
Copy link
Contributor Author

jba commented May 15, 2023

@cuishuang, splitting log files is not something we would add to slog. But it looks like you could pass a lumberjack.Logger directly to the built-in handlers to get what you want.

@jba
Copy link
Contributor Author

jba commented May 15, 2023

there's no mention on how best to use slog on library

@bangzek, slog tries not to take a position on that question, but it should allow for many alternatives.

If you choose to use a package-level variable, then it would need to be made concurrency-safe anyway, so you wouldn't be able to write logger.Debug anyway; you'd probably write a function logger() to retrieve the logger safely, and in that function you could check for nil and return a logger with a no-op handler instead.

@cuishuang
Copy link
Contributor

slog tries not to take a position on that question, but it should allow for many alternatives.

If you choose to use a package-level variable, then it would need to be made concurrency-safe anyway, so you wouldn't be able to write logger.Debug anyway; you'd probably write a function logger() to retrieve the logger safely, and in that function you could check for nil and return a logger with a no-op handler instead.

Thanks for your answer!

@shabbyrobe
Copy link

I have multiple sinks that I need to send each log entry to. My first thought was to write a wrapping Handler that dispatches to multiple Handlers, but was a bit surprised by the doc for Handler.WithAttrs, which says:

// The Handler owns the slice: it may retain, modify or discard it.
	WithAttrs(attrs []Attr) Handler

Does this mean that for each sink, my wrapping handler needs to clone the attrs slice before forwarding to each of the child handler's WithAttrs, to avoid one handler's license to make modifications to that slice clobbering another handler?

@samber
Copy link

samber commented May 21, 2023

@shabbyrobe i've worked on such an handler and use it in production: https://github.com/samber/slog-multi

It is called "Fanout".

@shabbyrobe
Copy link

Thanks @samber. It looks though like that is potentially vulnerable to the exact thing I described. In your WithAttrs function in pool.go, you are passing the same attrs slice to multiple handlers, which based on how I interpret that doc comment, I would think you shouldn't do without a copy.

Again, unless I'm misunderstanding that comment, you'd need to do this instead, so that users who could be using arbitrary handlers not written by yourself can be safe from unexpected side effects:

func (h *PoolHandler) WithAttrs(attrs []slog.Attr) slog.Handler {
	handers := lo.Map(h.handlers, func(h slog.Handler, _ int) slog.Handler {
		cattrs := make([]slog.Attr, len(attrs))
		copy(cattrs, attr)
		return h.WithAttrs(cattrs)
	})
	return Pool()(handers...)
}

Again, it's possible I'm simply misunderstanding that piece of documentation, but if I'm not, this is going to be a widely missed detail and I wonder if it's better for handlers to not presume ownership.

@jba
Copy link
Contributor Author

jba commented May 22, 2023

@shabbyrobe, you are correct. You should make n-1 copies of the slice. (You can pass on the original to one lucky sub-handler, provided your wrapper doesn't change it.)

@shabbyrobe
Copy link

Thanks for confirming @jba. What is the rationale for this design decision? I think this is hazardous and quite likely to be used wrong in this very scenario of fanning out, which is common. We've already seen an example, and I nearly did the exact same thing.

@bangzek
Copy link

bangzek commented May 27, 2023

there's no mention on how best to use slog on library

@bangzek, slog tries not to take a position on that question, but it should allow for many alternatives.

If you choose to use a package-level variable, then it would need to be made concurrency-safe anyway, so you wouldn't be able to write logger.Debug anyway; you'd probably write a function logger() to retrieve the logger safely, and in that function you could check for nil and return a logger with a no-op handler instead.

So it's deliberate decision to err on nil Logger instead just doing nothing?
I can argue for it just doing nothing when it's nil but I can understand if you decided it's better to err on that.

If it's deliberate decision can I ask to add do nothing Logger in the stdlib so most library writer don't need to reinvent that wheel over and over again.

Whether the package-level variable needs to be made concurrency-safe or not is a decision best left to the package author. In most cases, the added complexity is unnecessary. Typically, I set the package-level log decision once at the program's beginning, as there is no need for it to be modified once it's established.

@jba
Copy link
Contributor Author

jba commented May 27, 2023

@shabbyrobe, sorry for the delay in responding.

We let the handler own the argument for the common case where the handler wants to keep track of the attributes. It can simply store them without a copy.

Many other handlers will wrap a single handler, and they can just pass on the argument.

A handler that calls N others needs to make N-1 copies. I will mention this in a doc I'm putting together about writing handlers.

@jba
Copy link
Contributor Author

jba commented May 27, 2023

to add do nothing Logger in the stdlib so most library writer don't need to reinvent that wheel over and over again.

You can make a proposal for Go 1.22. If we do see a lot of them arising in the wild over the next six months, I'd probably be in favor.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Accepted
Development

No branches or pull requests