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

WIP feat: use slog only #29

Draft
wants to merge 16 commits into
base: main
Choose a base branch
from
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@

# Dependency directories (remove the comment below to include it)
# vendor/

# IDEs
.idea
113 changes: 21 additions & 92 deletions config.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
package logging

import (
"encoding/json"
"fmt"
"github.com/zitadel/logging/handlers"
"log/slog"
"os"

"github.com/sirupsen/logrus"
)

type Config struct {
Expand All @@ -21,91 +18,12 @@ type formatter struct {
Data map[string]interface{} `json:"data"`
}

type loggingConfig Config

func (c *Config) UnmarshalYAML(unmarshal func(interface{}) error) error {
log = (*logger)(logrus.New())
err := unmarshal((*loggingConfig)(c))
if err != nil {
return err
}
return c.SetLogger()
}

func (c *Config) UnmarshalJSON(data []byte) error {
log = (*logger)(logrus.New())
err := json.Unmarshal(data, (*loggingConfig)(c))
if err != nil {
return err
}
return c.SetLogger()
}

func (c *Config) SetLogger() (err error) {
err = c.parseFormatter()
if err != nil {
return err
}
err = c.parseLevel()
if err != nil {
return err
}
err = c.unmarshalFormatter()
if err != nil {
return err
}
c.setGlobal()
return nil
}

func (c *Config) setGlobal() {
if c.LocalLogger {
return
}
logrus.SetFormatter(log.Formatter)
logrus.SetLevel(log.Level)
logrus.SetReportCaller(log.ReportCaller)
log = (*logger)(logrus.StandardLogger())
}

func (c *Config) unmarshalFormatter() error {
formatterData, err := json.Marshal(c.Formatter.Data)
if err != nil {
return err
}
return json.Unmarshal(formatterData, log.Formatter)
}

func (c *Config) parseLevel() error {
if c.Level == "" {
log.Level = logrus.InfoLevel
return nil
}
level, err := logrus.ParseLevel(c.Level)
if err != nil {
return err
}
log.Level = level
return nil
}

const (
FormatterText = "text"
FormatterJSON = "json"
FormatterText = "text"
FormatterJSON = "json"
FormatterGoogle = "google"
)

func (c *Config) parseFormatter() error {
switch c.Formatter.Format {
case FormatterJSON:
log.Formatter = &logrus.JSONFormatter{}
case FormatterText, "":
log.Formatter = &logrus.TextFormatter{}
default:
return fmt.Errorf("%s formatter not supported", c.Formatter)
}
return nil
}

// Slog constructs a slog.Logger with the Formatter and Level from config.
func (c *Config) Slog() *slog.Logger {
logger := slog.Default()
Expand All @@ -116,22 +34,33 @@ func (c *Config) Slog() *slog.Logger {
return logger
}
opts := &slog.HandlerOptions{
AddSource: c.AddSource,
AddSource: false,
Level: level,
ReplaceAttr: c.fieldMapToPlaceKey(),
}

if c.Formatter.Format == FormatterGoogle {
opts.ReplaceAttr = handlers.ReplaceAttrForGoogleFunc(c.fieldMapToPlaceKey())
}
var handler slog.Handler
switch c.Formatter.Format {
case FormatterText:
return slog.New(slog.NewTextHandler(os.Stderr, opts))
case FormatterJSON:
return slog.New(slog.NewJSONHandler(os.Stderr, opts))
handler = slog.NewTextHandler(os.Stderr, opts)
case FormatterJSON, FormatterGoogle:
handler = slog.NewJSONHandler(os.Stderr, opts)
case "":
logger.Warn("no slog format in config, using text handler")
default:
logger.Warn("unknown slog format in config, using text handler", "format", c.Formatter.Format)
}
return slog.New(slog.NewTextHandler(os.Stderr, opts))
if c.Formatter.Format == FormatterGoogle {
handler = handlers.ForGoogleCloudLogging(handler, c.Formatter.Data)
}
if c.AddSource {
// The order matters.
// If AddCallerAndStack wraps the GoogleHandler, the caller field is added to the app context.
handler = handlers.AddCallerAndStack(handler)
}
return slog.New(handler)
}

func (c *Config) fieldMapToPlaceKey() func(groups []string, a slog.Attr) slog.Attr {
Expand Down
126 changes: 0 additions & 126 deletions config_test.go

This file was deleted.

7 changes: 1 addition & 6 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,12 @@ module github.com/zitadel/logging

go 1.21

require (
github.com/sirupsen/logrus v1.9.3
github.com/stretchr/testify v1.9.0
gopkg.in/yaml.v2 v2.4.0
)
require github.com/stretchr/testify v1.9.0

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/kr/pretty v0.1.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/sys v0.27.0 // indirect
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
11 changes: 0 additions & 11 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
Expand All @@ -8,20 +7,10 @@ github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.27.0 h1:wBqf8DvsY9Y/2P8gAfPDEYNuS30J4lPHJxXSb/nJZ+s=
golang.org/x/sys v0.27.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Loading