Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(zerolog): Add default log level helpers #26

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/labstack/echo/v4 v4.10.2
github.com/pkg/errors v0.9.1
github.com/rs/zerolog v1.29.0
github.com/sethvargo/go-envconfig v0.9.0
github.com/stretchr/testify v1.8.1
go.opentelemetry.io/otel v1.13.0
go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc v0.36.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFR
github.com/rs/xid v1.4.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/rs/zerolog v1.29.0 h1:Zes4hju04hjbvkVkOhdl2HpZa+0PmVwigmo8XoORE5w=
github.com/rs/zerolog v1.29.0/go.mod h1:NILgTygv/Uej1ra5XxGf82ZFSLk58MFGAUS2o6usyD0=
github.com/sethvargo/go-envconfig v0.9.0 h1:Q6FQ6hVEeTECULvkJZakq3dZMeBQ3JUpcKMfPQbKMDE=
github.com/sethvargo/go-envconfig v0.9.0/go.mod h1:Iz1Gy1Sf3T64TQlJSvee81qDhf7YIlt8GMUX6yyNFs0=
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
Expand Down
70 changes: 70 additions & 0 deletions loglevel/loglevel.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package loglevel

import (
"context"
"os"

"github.com/rs/zerolog"
"github.com/sethvargo/go-envconfig"
)

type Config struct {
Debug bool `env:"DEBUG,default=false"`
}

func SetGlobal() {
var c Config
if err := envconfig.Process(context.Background(), &c); err != nil {
// do nothing, leave it at the default "not debug, therefore info+"
}

zerolog.SetGlobalLevel(zerolog.InfoLevel)
if c.Debug {
zerolog.SetGlobalLevel(zerolog.DebugLevel)
}
}

func SetLoggerLevel(l zerolog.Logger) zerolog.Logger {
lvl := zerolog.InfoLevel
var c Config
if err := envconfig.Process(context.Background(), &c); err != nil {
// do nothing, leave it at the default "not debug, therefore info+"
}

if c.Debug {
lvl = zerolog.DebugLevel
}

return l.Level(lvl)
}

func FromEnv(prefix string) zerolog.Level {
return FromLookuper(prefix, envconfig.OsLookuper())
}

func FromLookuper(prefix string, l envconfig.Lookuper) zerolog.Level {
l = envconfig.PrefixLookuper(prefix, l)
c := getC(l)

if c.Debug {
return zerolog.DebugLevel
}

return zerolog.InfoLevel
}

func getC(l envconfig.Lookuper) Config {
var c Config
if err := envconfig.ProcessWith(context.Background(), &c, l); err != nil {
// do nothing
}
return c
}

func Baseline(prefix string) zerolog.Logger {
zerolog.TimeFieldFormat = zerolog.TimeFormatUnix
return zerolog.New(os.Stderr).With().
Timestamp().
Logger().
Level(FromEnv(prefix))
}