Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 4 additions & 8 deletions pkg/loggers/go.mod
Original file line number Diff line number Diff line change
@@ -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 (
Expand All @@ -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
Expand All @@ -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
Expand Down
10 changes: 4 additions & 6 deletions pkg/loggers/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down Expand Up @@ -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=
Expand Down
43 changes: 23 additions & 20 deletions pkg/loggers/logrus/logrus.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()?
Expand Down Expand Up @@ -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) {
Expand All @@ -73,51 +79,48 @@ 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 {
logrusFields := logrus.Fields{}
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) {
Expand Down Expand Up @@ -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
}
Expand Down
17 changes: 16 additions & 1 deletion pkg/loggers/zerolog/zerolog.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}

Expand Down Expand Up @@ -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,
}
}

Expand All @@ -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 {
Expand All @@ -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
Expand Down Expand Up @@ -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()
Expand All @@ -165,14 +177,17 @@ 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
}
fn := runtime.FuncForPC(pc).Name()
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,
}
}

Expand Down