Skip to content

Commit

Permalink
Allow log level names to be overridden
Browse files Browse the repository at this point in the history
  • Loading branch information
richardwilkes committed Sep 9, 2024
1 parent 5fc1ea2 commit 6ac0111
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 31 deletions.
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ go 1.22.5
require (
github.com/jackpal/gateway v1.0.15
github.com/pkg/term v1.1.0
golang.org/x/exp v0.0.0-20240707233637-46b078467d37
golang.org/x/image v0.18.0
golang.org/x/exp v0.0.0-20240909161429-701f63a606c0
golang.org/x/image v0.20.0
gopkg.in/yaml.v3 v3.0.1
)

Expand All @@ -15,6 +15,6 @@ require (
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/objx v0.5.2 // indirect
github.com/stretchr/testify v1.9.0 // indirect
golang.org/x/net v0.27.0 // indirect
golang.org/x/sys v0.22.0 // indirect
golang.org/x/net v0.29.0 // indirect
golang.org/x/sys v0.25.0 // indirect
)
16 changes: 8 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
golang.org/x/exp v0.0.0-20240707233637-46b078467d37 h1:uLDX+AfeFCct3a2C7uIWBKMJIR3CJMhcgfrUAqjRK6w=
golang.org/x/exp v0.0.0-20240707233637-46b078467d37/go.mod h1:M4RDyNAINzryxdtnbRXRL/OHtkFuWGRjvuhBJpk2IlY=
golang.org/x/image v0.18.0 h1:jGzIakQa/ZXI1I0Fxvaa9W7yP25TqT6cHIHn+6CqvSQ=
golang.org/x/image v0.18.0/go.mod h1:4yyo5vMFQjVjUcVk4jEQcU9MGy/rulF5WvUILseCM2E=
golang.org/x/net v0.27.0 h1:5K3Njcw06/l2y9vpGCSdcxWOYHOUk3dVNGDXN+FvAys=
golang.org/x/net v0.27.0/go.mod h1:dDi0PyhWNoiUOrAS8uXv/vnScO4wnHQO4mj9fn/RytE=
golang.org/x/exp v0.0.0-20240909161429-701f63a606c0 h1:e66Fs6Z+fZTbFBAxKfP3PALWBtpfqks2bwGcexMxgtk=
golang.org/x/exp v0.0.0-20240909161429-701f63a606c0/go.mod h1:2TbTHSBQa924w8M6Xs1QcRcFwyucIwBGpK1p2f1YFFY=
golang.org/x/image v0.20.0 h1:7cVCUjQwfL18gyBJOmYvptfSHS8Fb3YUDtfLIZ7Nbpw=
golang.org/x/image v0.20.0/go.mod h1:0a88To4CYVBAHp5FXJm8o7QbUl37Vd85ply1vyD8auM=
golang.org/x/net v0.29.0 h1:5ORfpBpCs4HzDYoodCDBbwHzdR5UrLBZ3sOnUJmFoHo=
golang.org/x/net v0.29.0/go.mod h1:gLkgy8jTGERgjzMic6DS9+SP0ajcu6Xu3Orq/SpETg0=
golang.org/x/sys v0.0.0-20200909081042-eff7692f9009/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI=
golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34=
golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Expand Down
47 changes: 28 additions & 19 deletions log/tracelog/tracelog.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ var _ slog.Handler = &Handler{}
type Config struct {
// Level is the minimum log level that will be emitted. Defaults to slog.LevelInfo if not set.
Level slog.Leveler
// LevelNames is an optional map of slog.Level to string that will be used to format the log level. If not set, the
// default slog level names will be used.
LevelNames map[slog.Level]string
// Sink is the io.Writer that will receive the formatted log output. Defaults to os.Stderr if not set.
Sink io.Writer
// BufferDepth greater than 0 will enable asynchronous delivery of log messages to the sink. When enabled, if there
Expand All @@ -55,11 +58,12 @@ func (c *Config) Normalize() {
// not optimized for performance, as I expect those that need to run this is environments where that matters will use
// one of the implementations provided by slog itself.
type Handler struct {
level slog.Leveler
delivery chan []byte
lock *sync.Mutex
sink io.Writer
list []entry
level slog.Leveler
levelNames map[slog.Level]string
delivery chan []byte
lock *sync.Mutex
sink io.Writer
list []entry
}

type entry struct {
Expand All @@ -74,9 +78,10 @@ func New(cfg *Config) *Handler {
}
cfg.Normalize()
h := Handler{
level: cfg.Level,
lock: &sync.Mutex{},
sink: cfg.Sink,
level: cfg.Level,
levelNames: cfg.LevelNames,
lock: &sync.Mutex{},
sink: cfg.Sink,
}
if cfg.BufferDepth > 0 {
h.delivery = make(chan []byte, cfg.BufferDepth)
Expand Down Expand Up @@ -117,17 +122,21 @@ func (h *Handler) withGroupOrAttrs(ga entry) *Handler {
// Handle implements slog.Handler.
func (h *Handler) Handle(_ context.Context, r slog.Record) error { //nolint:gocritic // Must use defined API
var buffer bytes.Buffer
switch r.Level {
case slog.LevelDebug:
buffer.WriteString("DBG")
case slog.LevelInfo:
buffer.WriteString("INF")
case slog.LevelWarn:
buffer.WriteString("WRN")
case slog.LevelError:
buffer.WriteString("ERR")
default:
fmt.Fprintf(&buffer, "%3d", r.Level)
if name, ok := h.levelNames[r.Level]; ok {
buffer.WriteString(name)
} else {
switch r.Level {
case slog.LevelDebug:
buffer.WriteString("DBG")
case slog.LevelInfo:
buffer.WriteString("INF")
case slog.LevelWarn:
buffer.WriteString("WRN")
case slog.LevelError:
buffer.WriteString("ERR")
default:
fmt.Fprintf(&buffer, "%3d", r.Level)
}
}
buffer.WriteString(r.Time.Round(0).Format(" | 2006-01-02 | 15:04:05.000 | "))
buffer.WriteString(r.Message)
Expand Down

0 comments on commit 6ac0111

Please sign in to comment.