From 54a1038847c3d24a8d5a41f23bf65fbc01dcec73 Mon Sep 17 00:00:00 2001 From: Simon Richardson Date: Thu, 24 Aug 2023 13:16:53 +0100 Subject: [PATCH] Slog writer 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. --- go.mod | 12 ++++++--- go.sum | 3 +++ slog/writer.go | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 78 insertions(+), 4 deletions(-) create mode 100644 slog/writer.go diff --git a/go.mod b/go.mod index 8240395..ba0a84e 100644 --- a/go.mod +++ b/go.mod @@ -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 +) diff --git a/go.sum b/go.sum index eb188de..5e7a8d6 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/slog/writer.go b/slog/writer.go new file mode 100644 index 0000000..974f224 --- /dev/null +++ b/slog/writer.go @@ -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") + } +}