From 75567f297a0ceba0f96ac81a9cf91aa6658b78e6 Mon Sep 17 00:00:00 2001 From: Jack Kawell Date: Mon, 20 Oct 2025 11:28:33 -0500 Subject: [PATCH] feat: pkg/loggers - add support for caller depth --- pkg/loggers/go.mod | 12 ++++------ pkg/loggers/go.sum | 10 ++++---- pkg/loggers/logrus/logrus.go | 43 ++++++++++++++++++---------------- pkg/loggers/zerolog/zerolog.go | 17 +++++++++++++- 4 files changed, 47 insertions(+), 35 deletions(-) diff --git a/pkg/loggers/go.mod b/pkg/loggers/go.mod index cb7dafb..e7c737d 100644 --- a/pkg/loggers/go.mod +++ b/pkg/loggers/go.mod @@ -1,16 +1,13 @@ module github.com/steady-bytes/draft/pkg/loggers -go 1.21.3 +go 1.25.3 -// replace ( -// github.com/steady-bytes/draft/api => ../../api -// github.com/steady-bytes/draft/pkg/chassis => ../chassis -// ) +// replace github.com/steady-bytes/draft/pkg/chassis => ../chassis require ( github.com/rs/zerolog v1.32.0 github.com/sirupsen/logrus v1.9.3 - github.com/steady-bytes/draft/pkg/chassis v0.2.0 + github.com/steady-bytes/draft/pkg/chassis v0.6.1 ) require ( @@ -19,7 +16,6 @@ require ( github.com/armon/go-metrics v0.4.1 // indirect github.com/boltdb/bolt v1.3.1 // indirect github.com/cloudevents/sdk-go/binding/format/protobuf/v2 v2.15.0 // indirect - github.com/envoyproxy/go-control-plane v0.12.0 // indirect github.com/fatih/color v1.14.1 // indirect github.com/fsnotify/fsnotify v1.7.0 // indirect github.com/hashicorp/go-hclog v1.5.0 // indirect @@ -42,7 +38,7 @@ require ( github.com/spf13/cast v1.6.0 // indirect github.com/spf13/pflag v1.0.5 // indirect github.com/spf13/viper v1.18.2 // indirect - github.com/steady-bytes/draft/api v0.0.2 // indirect + github.com/steady-bytes/draft/api v1.0.0 // indirect github.com/subosito/gotenv v1.6.0 // indirect go.etcd.io/bbolt v1.3.7 // indirect go.uber.org/multierr v1.11.0 // indirect diff --git a/pkg/loggers/go.sum b/pkg/loggers/go.sum index f3f9c64..a3959c1 100644 --- a/pkg/loggers/go.sum +++ b/pkg/loggers/go.sum @@ -24,8 +24,6 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM= github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/envoyproxy/go-control-plane v0.12.0 h1:4X+VP1GHd1Mhj6IB5mMeGbLCleqxjletLK6K0rbxyZI= -github.com/envoyproxy/go-control-plane v0.12.0/go.mod h1:ZBTaoJ23lqITozF0M6G4/IragXCQKCnYbmlmtHvwRG0= github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk= github.com/fatih/color v1.14.1 h1:qfhVLaG5s+nCROl1zJsZRxFeYrHLqWroPOQ8BWiNb4w= github.com/fatih/color v1.14.1/go.mod h1:2oHN61fhTpgcxD3TSWCgKDiH1+x4OiDVVGH8WlgGZGg= @@ -151,10 +149,10 @@ github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= github.com/spf13/viper v1.18.2 h1:LUXCnvUvSM6FXAsj6nnfc8Q2tp1dIgUfY9Kc8GsSOiQ= github.com/spf13/viper v1.18.2/go.mod h1:EKmWIqdnk5lOcmR72yw6hS+8OPYcwD0jteitLMVB+yk= -github.com/steady-bytes/draft/api v0.0.2 h1:xT4KrYXFADor9jFBrUNe2zLLMxsSM4AlZ3RWo1ygVpg= -github.com/steady-bytes/draft/api v0.0.2/go.mod h1:mlwxjvRiqvwySGdzVmF8voFhMffWq2F7dyd+xt6kENA= -github.com/steady-bytes/draft/pkg/chassis v0.2.0 h1:v/bbOo6EbVjhJWoohPx05odKanO9G8oU53IwUHn9oFQ= -github.com/steady-bytes/draft/pkg/chassis v0.2.0/go.mod h1:RlPToEXUnFNvuR5ySdYDGqFOLgWmsDP23FiHOiTzk18= +github.com/steady-bytes/draft/api v1.0.0 h1:QIvboAJgU0fBOV6BKChLMhHYIjBTR53+LJaR3w3g0NY= +github.com/steady-bytes/draft/api v1.0.0/go.mod h1:mlwxjvRiqvwySGdzVmF8voFhMffWq2F7dyd+xt6kENA= +github.com/steady-bytes/draft/pkg/chassis v0.6.1 h1:2q25EJPi9yMJ0AJ9K7BOWwUxIe3IxIlUwOmTCVAXjB8= +github.com/steady-bytes/draft/pkg/chassis v0.6.1/go.mod h1:B/b4s/czQydwL3BXk2CIUkVigxpcX2yp1tPC8btIX8Y= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= diff --git a/pkg/loggers/logrus/logrus.go b/pkg/loggers/logrus/logrus.go index f8addef..e4a3c85 100644 --- a/pkg/loggers/logrus/logrus.go +++ b/pkg/loggers/logrus/logrus.go @@ -21,11 +21,14 @@ type ( logger struct { entry *logrus.Entry + depth int } ) func New() chassis.Logger { - return &logger{} + return &logger{ + depth: 2, + } } // TODO: need to rethink this. maybe add the hook to the interface: GetHook()? @@ -56,8 +59,11 @@ func (l *logger) Start(config chassis.Config) { l.entry.Logger.SetLevel(logrus.Level(chassis.ParseLogLevel(levelString))) } -func newLogger(e *logrus.Entry) chassis.Logger { - return &logger{entry: e} +func (l *logger) newLogger(e *logrus.Entry) chassis.Logger { + return &logger{ + entry: e, + depth: l.depth, + } } func (l *logger) SetLevel(level chassis.LogLevel) { @@ -73,15 +79,15 @@ func (l *logger) Wrap(err error) error { } func (l *logger) WithError(err error) chassis.Logger { - return newLogger(l.entry.WithError(err)) + return l.newLogger(l.entry.WithError(err)) } func (l *logger) WithContext(ctx context.Context) chassis.Logger { - return newLogger(l.entry.WithContext(ctx)) + return l.newLogger(l.entry.WithContext(ctx)) } func (l *logger) WithField(key string, value interface{}) chassis.Logger { - return newLogger(l.entry.WithField(key, value)) + return l.newLogger(l.entry.WithField(key, value)) } func (l *logger) WithFields(fields chassis.Fields) chassis.Logger { @@ -89,35 +95,32 @@ func (l *logger) WithFields(fields chassis.Fields) chassis.Logger { for index, element := range fields { logrusFields[index] = element } - return newLogger(l.entry.WithFields(logrusFields)) + return l.newLogger(l.entry.WithFields(logrusFields)) } -// Implement `log.Logger` for `envoyproxy/go-control-plane/pkg/cache/v3` -// go-control-plane has it's own logger interface that needs to be implemented for logging -// to work correctly +func (l *logger) WithCallDepth(depth int) chassis.Logger { + l.depth = depth + return l +} -// dropping the args parameter for now, as it is not used func (l *logger) Debugf(msg string, args ...any) { - l.correctFunctionName().entry.Debug(msg) + l.correctFunctionName().entry.Debugf(msg, args...) } -// dropping the args parameter for now, as it is not used func (l *logger) Infof(msg string, args ...any) { - l.correctFunctionName().entry.Info(msg) + l.correctFunctionName().entry.Infof(msg, args...) } -// dropping the args parameter for now, as it is not used func (l *logger) Warnf(msg string, args ...any) { - l.correctFunctionName().entry.Warn(msg) + l.correctFunctionName().entry.Warnf(msg, args...) } -// dropping the args parameter for now, as it is not used func (l *logger) Errorf(msg string, args ...any) { - l.correctFunctionName().entry.Error(msg) + l.correctFunctionName().entry.Errorf(msg, args...) } func (l *logger) WithTime(t time.Time) chassis.Logger { - return newLogger(l.entry.WithTime(t)) + return l.newLogger(l.entry.WithTime(t)) } func (l *logger) Trace(msg string) { @@ -154,7 +157,7 @@ func (l *logger) Panic(msg string) { } func (l *logger) correctFunctionName() *logger { - pc, _, _, ok := runtime.Caller(2) + pc, _, _, ok := runtime.Caller(l.depth) if !ok { return nil } diff --git a/pkg/loggers/zerolog/zerolog.go b/pkg/loggers/zerolog/zerolog.go index b758272..ea9e04b 100644 --- a/pkg/loggers/zerolog/zerolog.go +++ b/pkg/loggers/zerolog/zerolog.go @@ -23,12 +23,14 @@ type ( logger zerolog.Logger fields chassis.Fields level chassis.LogLevel + depth int } ) func New() chassis.Logger { return &logger{ fields: make(chassis.Fields), + depth: 2, } } @@ -70,6 +72,7 @@ func (l *logger) WithError(err error) chassis.Logger { logger: (l.logger.With().Str("error", err.Error()).Logger()), fields: l.fields, level: l.level, + depth: l.depth, } } @@ -92,6 +95,8 @@ func (l *logger) WithFields(fields chassis.Fields) chassis.Logger { new := &logger{ logger: l.logger.With().Logger(), fields: newFields, + level: l.level, + depth: l.depth, } // append new fields for key, value := range fields { @@ -103,6 +108,11 @@ func (l *logger) WithFields(fields chassis.Fields) chassis.Logger { return new } +func (l *logger) WithCallDepth(depth int) chassis.Logger { + l.depth = depth + return l +} + // Implement `log.Logger` for `envoyproxy/go-control-plane/pkg/cache/v3` // go-control-plane has it's own logger interface that needs to be implemented for logging // to work correctly @@ -148,6 +158,8 @@ func (l *logger) WrappedError(err error, msg string) { n := &logger{ logger: l.logger, fields: e.Fields(), + level: l.level, + depth: l.depth, } for key, value := range e.Fields() { n.logger = n.logger.With().Str(key, fmt.Sprintf("%v", value)).Logger() @@ -165,7 +177,7 @@ func (l *logger) Panic(msg string) { } func (l *logger) correctFunctionName() *logger { - pc, _, _, ok := runtime.Caller(2) + pc, _, _, ok := runtime.Caller(l.depth) if !ok { return nil } @@ -173,6 +185,9 @@ func (l *logger) correctFunctionName() *logger { functionName := fn[strings.LastIndex(fn, ".")+1:] return &logger{ logger: l.logger.With().Str("function", functionName).Logger(), + fields: l.fields, + level: l.level, + depth: l.depth, } }