coder/slog

Switch API to log via ctx instead of to logger struct

Closed this issue · 4 comments

Tired of logger argument pollution.

cc @ammario @cmoog

I'm definitely in favor of this change now.

  1. The alternative is Loggers becoming apart of random objects so their methods have smaller signatures.
  2. We already store fields as apart of the context, this fits nicely into that model.
  3. Context-based instrumentation is the basis of the built in trace package and OpenCensus.
cmoog commented

So if I understand correctly, we'd have the following

func process(ctx context.Context) {
    slog.Info(ctx, "log message")
}

Wouldn't you lose the ability to specify the io.Writer to which logs are written? You'd be restricted to a single logger implicit with each context.

But all things considered, I'm in favor as well.

@cmoog You'd create a new context with a new logger that writes to wherever you want.

i.e ctx = sloghuman.Make(ctx, os.Stdout)