Skip to content

Commit

Permalink
Slog writer
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
SimonRichardson committed Aug 24, 2023
1 parent eebad3a commit 54a1038
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 4 deletions.
12 changes: 8 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
module github.com/juju/loggo

go 1.14
go 1.21

require (
github.com/juju/ansiterm v0.0.0-20180109212912-720a0952cc2a
github.com/lunixbochs/vtclean v0.0.0-20160125035106-4fbf7632a2c6
github.com/mattn/go-colorable v0.0.6
github.com/mattn/go-isatty v0.0.0-20160806122752-66b8e73f3f5c
gopkg.in/check.v1 v1.0.0-20160105164936-4f90aeace3a2
)

require (
github.com/lunixbochs/vtclean v0.0.0-20160125035106-4fbf7632a2c6 // indirect
github.com/mattn/go-colorable v0.0.6 // indirect
github.com/mattn/go-isatty v0.0.0-20160806122752-66b8e73f3f5c // indirect
golang.org/x/sys v0.11.0 // indirect
)
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
github.com/juju/ansiterm v0.0.0-20180109212912-720a0952cc2a h1:FaWFmfWdAUKbSCtOU2QjDaorUexogfaMgbipgYATUMU=
github.com/juju/ansiterm v0.0.0-20180109212912-720a0952cc2a/go.mod h1:UJSiEoRfvx3hP73CvoARgeLjaIOjybY9vj8PUPPFGeU=
github.com/lunixbochs/vtclean v0.0.0-20160125035106-4fbf7632a2c6 h1:yjdywwaxd8vTEXuA4EdgUBkiCQEQG7YAY3k9S1PaZKg=
github.com/lunixbochs/vtclean v0.0.0-20160125035106-4fbf7632a2c6/go.mod h1:pHhQNgMf3btfWnGBVipUOjRYhoOsdGqdm/+2c2E2WMI=
github.com/mattn/go-colorable v0.0.6 h1:jGqlOoCjqVR4hfTO9H1qrR2xi0xZNYmX2T1xlw7P79c=
github.com/mattn/go-colorable v0.0.6/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
github.com/mattn/go-isatty v0.0.0-20160806122752-66b8e73f3f5c h1:3nKFouDdpgGUV/uerJcYWH45ZbJzX0SiVWfTgmUeTzc=
github.com/mattn/go-isatty v0.0.0-20160806122752-66b8e73f3f5c/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
golang.org/x/sys v0.11.0 h1:eG7RXZHdqOJ1i+0lgLgCpSXAp6M3LYlAo6osgSi0xOM=
golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v1.0.0-20160105164936-4f90aeace3a2 h1:+j1SppRob9bAgoYmsdW9NNBdKZfgYuWpqnYHv78Qt8w=
gopkg.in/check.v1 v1.0.0-20160105164936-4f90aeace3a2/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
67 changes: 67 additions & 0 deletions slog/writer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package slog

import (
"context"
"log/slog"
"strings"

"github.com/juju/loggo"
)

type slogWriter struct {
writer slog.Handler
}

// NewSlowWriter will write out slog severity levels.
func NewSlowWriter(writer slog.Handler) loggo.Writer {
return &slogWriter{writer}
}

// Write implements Writer.
func (w *slogWriter) Write(entry loggo.Entry) {
record := slog.NewRecord(
entry.Timestamp,
level(entry.Level),
entry.Message,
// TODO (stickupkid): Add a way to log the caller ptr in the
// loggo.Entry. That way we can push the information directly into
// the slog.Record.
0,
)

record.AddAttrs(
slog.String("module", entry.Module),
slog.String("filename", entry.Filename),
slog.Int("line", entry.Line),
)
if len(entry.Labels) > 0 {
record.AddAttrs(slog.String("labels", strings.Join(entry.Labels, ",")))
}

w.writer.Handle(context.Background(), record)
}

// The level function allows levels to be mapped to slog levels. Although,
// slog doesn't explicitly implement all the levels that we require for mapping
// it does allow for custom levels to be added. This is done by using the
// slog.Level type as an int64.
// Reading the documentation https://pkg.go.dev/log/slog#Level explains how
// to insert custom levels.
func level(level loggo.Level) slog.Level {
switch level {
case loggo.TRACE:
return slog.LevelDebug - 1
case loggo.DEBUG:
return slog.LevelDebug
case loggo.INFO:
return slog.LevelInfo
case loggo.WARNING:
return slog.LevelInfo + 1
case loggo.ERROR:
return slog.LevelError
case loggo.CRITICAL:
return slog.LevelError + 1
default:
panic("unknown level")
}
}

0 comments on commit 54a1038

Please sign in to comment.