Skip to content

Commit

Permalink
Merge pull request #57 from aserto-dev/logrus
Browse files Browse the repository at this point in the history
Logging helper to wire logrus to zerolog
  • Loading branch information
ronenh authored Aug 22, 2024
2 parents 91a627e + ff6eb84 commit 252e179
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 4 deletions.
5 changes: 3 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/aserto-dev/runtime
go 1.21

require (
github.com/aserto-dev/logger v0.0.6
github.com/fsnotify/fsnotify v1.7.0
github.com/google/uuid v1.6.0
github.com/google/wire v0.6.0
Expand All @@ -12,6 +13,7 @@ require (
github.com/opencontainers/image-spec v1.1.0
github.com/pkg/errors v0.9.1
github.com/rs/zerolog v1.33.0
github.com/sirupsen/logrus v1.9.3
github.com/stretchr/testify v1.9.0
)

Expand Down Expand Up @@ -51,7 +53,6 @@ require (
github.com/prometheus/common v0.54.0 // indirect
github.com/prometheus/procfs v0.15.1 // indirect
github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/tchap/go-patricia/v2 v2.3.1 // indirect
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb // indirect
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
Expand All @@ -63,7 +64,7 @@ require (
golang.org/x/mod v0.17.0 // indirect
golang.org/x/net v0.27.0 // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/sys v0.22.0 // indirect
golang.org/x/sys v0.24.0 // indirect
golang.org/x/text v0.16.0 // indirect
golang.org/x/time v0.5.0 // indirect
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d // indirect
Expand Down
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ github.com/agnivade/levenshtein v1.1.1 h1:QY8M92nrzkmr798gCo3kmMyqXFzdQVpxLlGPRB
github.com/agnivade/levenshtein v1.1.1/go.mod h1:veldBMzWxcCG2ZvUTKD2kJNRdCk5hVbJomOvKkmgYbo=
github.com/arbovm/levenshtein v0.0.0-20160628152529-48b4e1c0c4d0 h1:jfIu9sQUG6Ig+0+Ap1h4unLjW6YQJpKZVmUzxsD4E/Q=
github.com/arbovm/levenshtein v0.0.0-20160628152529-48b4e1c0c4d0/go.mod h1:t2tdKJDJF9BV14lnkjHmOQgcvEKgtqs5a1N3LNdJhGE=
github.com/aserto-dev/logger v0.0.6 h1:C5u4eU6LJAlyWOjkz/IZmkIXfOH0SBomOHU74o6mUEc=
github.com/aserto-dev/logger v0.0.6/go.mod h1:0wakoQsaQiagtzLxqyOus7ITaY0P5n5MWoQo6GbenWY=
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
github.com/bytecodealliance/wasmtime-go/v3 v3.0.2 h1:3uZCA/BLTIu+DqCfguByNMJa2HVHpXvjfy0Dy7g6fuA=
Expand Down Expand Up @@ -213,8 +215,8 @@ golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
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.24.0 h1:Twjiwq9dn6R1fQcyiK+wQyHWfaz/BJB+YIpzU/Cv3Xg=
golang.org/x/sys v0.24.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
Expand Down
70 changes: 70 additions & 0 deletions logger/logrus.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package logger

import (
"io"

"github.com/aserto-dev/logger"
"github.com/rs/zerolog"
"github.com/sirupsen/logrus" // nolint // we're only using logrus to make sure any packages using it still end up to zerolog
)

func NewLogger(logOutput logger.Writer, errorOutput logger.ErrWriter, cfg *logger.Config) (*zerolog.Logger, error) {
log, err := logger.NewLogger(logOutput, errorOutput, cfg)
if err != nil {
return nil, err
}

addLogrusHook(log)

return log, nil
}

func addLogrusHook(log *zerolog.Logger) {
logrusLogger := log.With().Str("log-source", "logrus").Logger()
logrus.AddHook(&logrusHook{logger: &logrusLogger})
logrus.SetLevel(logrus.TraceLevel)
logrus.SetOutput(io.Discard)

}

// logrusHook is a logrus hook that writes to zerolog.
type logrusHook struct {
logger *zerolog.Logger
}

var _ logrus.Hook = &logrusHook{}

// Fire will be called when some logging function is called.
func (hook *logrusHook) Fire(entry *logrus.Entry) error {
switch entry.Level {
case logrus.PanicLevel:
hook.logger.Panic().Fields(entry.Data).Msg(entry.Message)
case logrus.FatalLevel:
hook.logger.Fatal().Fields(entry.Data).Msg(entry.Message)
case logrus.ErrorLevel:
hook.logger.Error().Fields(entry.Data).Msg(entry.Message)
case logrus.WarnLevel:
hook.logger.Warn().Fields(entry.Data).Msg(entry.Message)
case logrus.InfoLevel:
hook.logger.Info().Fields(entry.Data).Msg(entry.Message)
case logrus.DebugLevel:
hook.logger.Debug().Fields(entry.Data).Msg(entry.Message)
case logrus.TraceLevel:
hook.logger.Trace().Fields(entry.Data).Msg(entry.Message)
}

return nil
}

// Levels defines on which log levels this hook would trigger.
func (hook *logrusHook) Levels() []logrus.Level {
return []logrus.Level{
logrus.PanicLevel,
logrus.FatalLevel,
logrus.ErrorLevel,
logrus.WarnLevel,
logrus.InfoLevel,
logrus.DebugLevel,
logrus.TraceLevel,
}
}

0 comments on commit 252e179

Please sign in to comment.