Skip to content

Commit

Permalink
logger: add With() method to persist log labels (#5)
Browse files Browse the repository at this point in the history
* logger: add With() method to persist log labels

Add `With()` method to loggers to create a sublogger with a set of
persistent labels that can be used to generate consistent log messages
for a given resource.

* chore: add missing license headers

* loggers: share interface between Event and Context
  • Loading branch information
lgfa29 authored Jul 30, 2024
1 parent 838b0a7 commit ba1e898
Show file tree
Hide file tree
Showing 8 changed files with 482 additions and 20 deletions.
87 changes: 87 additions & 0 deletions loggers/noop/context.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// SPDX-License-Identifier: Apache-2.0

package noop

import (
"net"

"github.com/loopholelabs/logging/types"
)

var _ types.Context = (*Context)(nil)

type Context struct {
l *Logger
}

func (c *Context) Logger() types.SubLogger {
return c.l
}

func (c *Context) Str(key string, val string) types.Context {
return c
}

func (c *Context) Bool(key string, val bool) types.Context {
return c
}

func (c *Context) Int(key string, val int) types.Context {
return c
}

func (c *Context) Int8(key string, val int8) types.Context {
return c
}

func (c *Context) Int16(key string, val int16) types.Context {
return c
}

func (c *Context) Int32(key string, val int32) types.Context {
return c
}

func (c *Context) Int64(key string, val int64) types.Context {
return c
}

func (c *Context) Uint(key string, val uint) types.Context {
return c
}

func (c *Context) Uint8(key string, val uint8) types.Context {
return c
}

func (c *Context) Uint16(key string, val uint16) types.Context {
return c
}

func (c *Context) Uint32(key string, val uint32) types.Context {
return c
}

func (c *Context) Uint64(key string, val uint64) types.Context {
return c
}

func (c *Context) Float32(key string, val float32) types.Context {
return c
}

func (c *Context) Float64(key string, val float64) types.Context {
return c
}

func (c *Context) IPAddr(key string, ipAddr net.IP) types.Context {
return c
}

func (c *Context) MACAddr(key string, macAddr net.HardwareAddr) types.Context {
return c
}

func (c *Context) Err(err error) types.Context {
return c
}
4 changes: 4 additions & 0 deletions loggers/noop/noop.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ func (s *Logger) Level() types.Level {

func (s *Logger) SubLogger(string) types.SubLogger { return s }

func (s *Logger) With() types.Context {
return &Context{l: s}
}

func (s *Logger) Fatal() types.Event {
return new(Event)
}
Expand Down
159 changes: 159 additions & 0 deletions loggers/slog/context.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
// SPDX-License-Identifier: Apache-2.0

package slog

import (
"log/slog"
"net"

"github.com/loopholelabs/logging/types"
)

var _ types.Context = (*Context)(nil)

type Context struct {
l *Logger
attrs []any
}

func (c *Context) Logger() types.SubLogger {
l := New(c.l.source, c.l.level, c.l.output)
l.logger = c.l.logger.With(c.attrs...)
return l
}

func (c *Context) Str(key string, val string) types.Context {
c.attrs = append(c.attrs, slog.Attr{
Key: key,
Value: slog.StringValue(val),
})
return c
}

func (c *Context) Bool(key string, val bool) types.Context {
c.attrs = append(c.attrs, slog.Attr{
Key: key,
Value: slog.BoolValue(val),
})
return c
}

func (c *Context) Int(key string, val int) types.Context {
c.attrs = append(c.attrs, slog.Attr{
Key: key,
Value: slog.IntValue(val),
})
return c
}

func (c *Context) Int8(key string, val int8) types.Context {
c.attrs = append(c.attrs, slog.Attr{
Key: key,
Value: slog.IntValue(int(val)),
})
return c
}

func (c *Context) Int16(key string, val int16) types.Context {
c.attrs = append(c.attrs, slog.Attr{
Key: key,
Value: slog.IntValue(int(val)),
})
return c
}

func (c *Context) Int32(key string, val int32) types.Context {
c.attrs = append(c.attrs, slog.Attr{
Key: key,
Value: slog.IntValue(int(val)),
})
return c
}

func (c *Context) Int64(key string, val int64) types.Context {
c.attrs = append(c.attrs, slog.Attr{
Key: key,
Value: slog.Int64Value(val),
})
return c
}

func (c *Context) Uint(key string, val uint) types.Context {
c.attrs = append(c.attrs, slog.Attr{
Key: key,
Value: slog.Uint64Value(uint64(val)),
})
return c
}

func (c *Context) Uint8(key string, val uint8) types.Context {
c.attrs = append(c.attrs, slog.Attr{
Key: key,
Value: slog.Uint64Value(uint64(val)),
})
return c
}

func (c *Context) Uint16(key string, val uint16) types.Context {
c.attrs = append(c.attrs, slog.Attr{
Key: key,
Value: slog.Uint64Value(uint64(val)),
})
return c
}

func (c *Context) Uint32(key string, val uint32) types.Context {
c.attrs = append(c.attrs, slog.Attr{
Key: key,
Value: slog.Uint64Value(uint64(val)),
})
return c
}

func (c *Context) Uint64(key string, val uint64) types.Context {
c.attrs = append(c.attrs, slog.Attr{
Key: key,
Value: slog.Uint64Value(val),
})
return c
}

func (c *Context) Float32(key string, val float32) types.Context {
c.attrs = append(c.attrs, slog.Attr{
Key: key,
Value: slog.Float64Value(float64(val)),
})
return c
}

func (c *Context) Float64(key string, val float64) types.Context {
c.attrs = append(c.attrs, slog.Attr{
Key: key,
Value: slog.Float64Value(float64(val)),
})
return c
}

func (c *Context) IPAddr(key string, ipAddr net.IP) types.Context {
c.attrs = append(c.attrs, slog.Attr{
Key: key,
Value: slog.StringValue(ipAddr.String()),
})
return c
}

func (c *Context) MACAddr(key string, macAddr net.HardwareAddr) types.Context {
c.attrs = append(c.attrs, slog.Attr{
Key: key,
Value: slog.StringValue(macAddr.String()),
})
return c
}

func (c *Context) Err(err error) types.Context {
c.attrs = append(c.attrs, slog.Attr{
Key: "error",
Value: slog.StringValue(err.Error()),
})
return c
}
4 changes: 4 additions & 0 deletions loggers/slog/slog.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ func (s *Logger) SubLogger(source string) types.SubLogger {
return newSlog(fmt.Sprintf("%s:%s", s.source, source), sloglevel, s.output)
}

func (s *Logger) With() types.Context {
return &Context{l: s}
}

func (s *Logger) Fatal() types.Event {
return &Event{
level: slog.LevelError + 1,
Expand Down
110 changes: 110 additions & 0 deletions loggers/zerolog/context.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
// SPDX-License-Identifier: Apache-2.0

package zerolog

import (
"net"

"github.com/loopholelabs/logging/types"
"github.com/rs/zerolog"
)

var _ types.Context = (*Context)(nil)

type Context struct {
l *Logger
zeroCtx zerolog.Context
}

func (c *Context) Logger() types.SubLogger {
return &Logger{
logger: c.zeroCtx.Logger(),
source: c.l.source,
level: c.l.level,
}
}

func (c *Context) Str(key string, val string) types.Context {
c.zeroCtx = c.zeroCtx.Str(key, val)
return c
}

func (c *Context) Bool(key string, val bool) types.Context {
c.zeroCtx = c.zeroCtx.Bool(key, val)
return c
}

func (c *Context) Int(key string, val int) types.Context {
c.zeroCtx = c.zeroCtx.Int(key, val)
return c
}

func (c *Context) Int8(key string, val int8) types.Context {
c.zeroCtx = c.zeroCtx.Int8(key, val)
return c
}

func (c *Context) Int16(key string, val int16) types.Context {
c.zeroCtx = c.zeroCtx.Int16(key, val)
return c
}

func (c *Context) Int32(key string, val int32) types.Context {
c.zeroCtx = c.zeroCtx.Int32(key, val)
return c
}

func (c *Context) Int64(key string, val int64) types.Context {
c.zeroCtx = c.zeroCtx.Int64(key, val)
return c
}

func (c *Context) Uint(key string, val uint) types.Context {
c.zeroCtx = c.zeroCtx.Uint(key, val)
return c
}

func (c *Context) Uint8(key string, val uint8) types.Context {
c.zeroCtx = c.zeroCtx.Uint8(key, val)
return c
}

func (c *Context) Uint16(key string, val uint16) types.Context {
c.zeroCtx = c.zeroCtx.Uint16(key, val)
return c
}

func (c *Context) Uint32(key string, val uint32) types.Context {
c.zeroCtx = c.zeroCtx.Uint32(key, val)
return c
}

func (c *Context) Uint64(key string, val uint64) types.Context {
c.zeroCtx = c.zeroCtx.Uint64(key, val)
return c
}

func (c *Context) Float32(key string, val float32) types.Context {
c.zeroCtx = c.zeroCtx.Float32(key, val)
return c
}

func (c *Context) Float64(key string, val float64) types.Context {
c.zeroCtx = c.zeroCtx.Float64(key, val)
return c
}

func (c *Context) IPAddr(key string, val net.IP) types.Context {
c.zeroCtx = c.zeroCtx.IPAddr(key, val)
return c
}

func (c *Context) MACAddr(key string, val net.HardwareAddr) types.Context {
c.zeroCtx = c.zeroCtx.MACAddr(key, val)
return c
}

func (c *Context) Err(err error) types.Context {
c.zeroCtx = c.zeroCtx.Err(err)
return c
}
7 changes: 7 additions & 0 deletions loggers/zerolog/zerolog.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ func (z *Logger) SubLogger(source string) types.SubLogger {
}
}

func (z *Logger) With() types.Context {
return &Context{
l: z,
zeroCtx: z.logger.With(),
}
}

func (z *Logger) Fatal() types.Event {
return (*Event)(z.logger.Fatal().Timestamp().Str(types.SourceKey, z.source))
}
Expand Down
Loading

0 comments on commit ba1e898

Please sign in to comment.