loggo icon indicating copy to clipboard operation
loggo copied to clipboard

Slog writer

Open SimonRichardson opened this issue 1 year ago • 0 comments

To take advantage of the new logging package from the go 1.21 release, the loggo package starts incorporate these new changes. There are features that we could start to add to loggo to then pass directly to slog. The migration over to slog could then be done in stages.

For now, it might be useful to at least play around with slog and see if there are any advantages to it.


Running the new example, we can take advantage of slog for the backend with structured logging.

go run ./example "<root>=TRACE" slog

Current logging levels:
<root>=TRACE
Using log/slog writer:

time=2023-09-26T11:27:31.541+01:00 level=INFO msg="Start of test." module="" filename=/home/simon/go/src/github.com/juju/loggo/example/main.go line=47 foo=bar
time=2023-09-26T11:27:31.542+01:00 level=ERROR+1 msg="first critical" module=first filename=/home/simon/go/src/github.com/juju/loggo/example/first.go line=11 baz=boo
time=2023-09-26T11:27:31.542+01:00 level=ERROR msg="first error" module=first filename=/home/simon/go/src/github.com/juju/loggo/example/first.go line=15
time=2023-09-26T11:27:31.542+01:00 level=WARN msg="first warning" module=first filename=/home/simon/go/src/github.com/juju/loggo/example/first.go line=19
time=2023-09-26T11:27:31.542+01:00 level=INFO msg="first info" module=first filename=/home/simon/go/src/github.com/juju/loggo/example/first.go line=23
time=2023-09-26T11:27:31.542+01:00 level=DEBUG msg="first debug" module=first filename=/home/simon/go/src/github.com/juju/loggo/example/first.go line=27
time=2023-09-26T11:27:31.542+01:00 level=DEBUG-1 msg="first trace" module=first filename=/home/simon/go/src/github.com/juju/loggo/example/first.go line=31
time=2023-09-26T11:27:31.542+01:00 level=ERROR+1 msg="second critical" module=second filename=/home/simon/go/src/github.com/juju/loggo/example/second.go line=10
time=2023-09-26T11:27:31.542+01:00 level=ERROR msg="second error" module=second filename=/home/simon/go/src/github.com/juju/loggo/example/second.go line=14
time=2023-09-26T11:27:31.542+01:00 level=WARN msg="second warning" module=second filename=/home/simon/go/src/github.com/juju/loggo/example/second.go line=18
time=2023-09-26T11:27:31.542+01:00 level=INFO msg="second info" module=second filename=/home/simon/go/src/github.com/juju/loggo/example/second.go line=22
time=2023-09-26T11:27:31.542+01:00 level=DEBUG msg="second debug" module=second filename=/home/simon/go/src/github.com/juju/loggo/example/second.go line=26
time=2023-09-26T11:27:31.542+01:00 level=DEBUG-1 msg="second trace" module=second filename=/home/simon/go/src/github.com/juju/loggo/example/second.go line=29

This still works with namespacing as well. Notice that first is at trace and everything else is at warning.

go run ./example "<root>=WARNING;first=TRACE" slog

Current logging levels:
<root>=WARNING;first=TRACE
Using log/slog writer:

time=2023-09-26T14:15:20.783+01:00 level=ERROR+1 msg="first critical" module=first filename=/home/simon/go/src/github.com/juju/loggo/example/first.go line=11 baz=boo
time=2023-09-26T14:15:20.783+01:00 level=ERROR msg="first error" module=first filename=/home/simon/go/src/github.com/juju/loggo/example/first.go line=15
time=2023-09-26T14:15:20.783+01:00 level=WARN msg="first warning" module=first filename=/home/simon/go/src/github.com/juju/loggo/example/first.go line=19
time=2023-09-26T14:15:20.783+01:00 level=INFO msg="first info" module=first filename=/home/simon/go/src/github.com/juju/loggo/example/first.go line=23
time=2023-09-26T14:15:20.783+01:00 level=DEBUG msg="first debug" module=first filename=/home/simon/go/src/github.com/juju/loggo/example/first.go line=27
time=2023-09-26T14:15:20.783+01:00 level=DEBUG-1 msg="first trace" module=first filename=/home/simon/go/src/github.com/juju/loggo/example/first.go line=31
time=2023-09-26T14:15:20.783+01:00 level=ERROR+1 msg="second critical" module=second filename=/home/simon/go/src/github.com/juju/loggo/example/second.go line=10
time=2023-09-26T14:15:20.783+01:00 level=ERROR msg="second error" module=second filename=/home/simon/go/src/github.com/juju/loggo/example/second.go line=14
time=2023-09-26T14:15:20.783+01:00 level=WARN msg="second warning" module=second filename=/home/simon/go/src/github.com/juju/loggo/example/second.go line=18

SimonRichardson avatar Aug 24 '23 12:08 SimonRichardson