Skip to content

Commit

Permalink
remove logrus
Browse files Browse the repository at this point in the history
  • Loading branch information
eliobischof committed Dec 3, 2024
1 parent e9b9865 commit 7f1cf17
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 276 deletions.
91 changes: 5 additions & 86 deletions config.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
package logging

import (
"encoding/json"
"fmt"
"log/slog"
"os"

"github.com/sirupsen/logrus"
)

type Config struct {
Expand All @@ -21,91 +17,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 @@ -126,6 +43,8 @@ func (c *Config) Slog() *slog.Logger {
return slog.New(slog.NewTextHandler(os.Stderr, opts))
case FormatterJSON:
return slog.New(slog.NewJSONHandler(os.Stderr, opts))
case FormatterGoogle:
return slog.New(NewGoogleHandler(os.Stderr, opts, c.Formatter.Data))
case "":
logger.Warn("no slog format in config, using text handler")
default:
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=
31 changes: 0 additions & 31 deletions logger.go

This file was deleted.

Loading

0 comments on commit 7f1cf17

Please sign in to comment.