parent
5bde86ae3b
commit
d65b354285
@ -0,0 +1,14 @@ |
|||||||
|
module github.com/PCManiac/logrus_init |
||||||
|
|
||||||
|
go 1.17 |
||||||
|
|
||||||
|
require ( |
||||||
|
github.com/caarlos0/env/v6 v6.10.0 |
||||||
|
github.com/sirupsen/logrus v1.8.1 |
||||||
|
) |
||||||
|
|
||||||
|
require ( |
||||||
|
github.com/stretchr/testify v1.4.0 // indirect |
||||||
|
golang.org/x/sys v0.0.0-20211109184856-51b60fd695b3 // indirect |
||||||
|
gopkg.in/yaml.v2 v2.4.0 // indirect |
||||||
|
) |
||||||
@ -0,0 +1,20 @@ |
|||||||
|
github.com/caarlos0/env/v6 v6.10.0 h1:lA7sxiGArZ2KkiqpOQNf8ERBRWI+v8MWIH+eGjSN22I= |
||||||
|
github.com/caarlos0/env/v6 v6.10.0/go.mod h1:hvp/ryKXKipEkcuYjs9mI4bBCg+UI0Yhgm5Zu0ddvwc= |
||||||
|
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/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.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE= |
||||||
|
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0= |
||||||
|
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= |
||||||
|
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= |
||||||
|
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= |
||||||
|
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= |
||||||
|
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= |
||||||
|
golang.org/x/sys v0.0.0-20211109184856-51b60fd695b3 h1:T6tyxxvHMj2L1R2kZg0uNMpS8ZhB9lRa9XRGTCSA65w= |
||||||
|
golang.org/x/sys v0.0.0-20211109184856-51b60fd695b3/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= |
||||||
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= |
||||||
|
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= |
||||||
|
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= |
||||||
|
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= |
||||||
@ -0,0 +1,62 @@ |
|||||||
|
package logrus_init |
||||||
|
|
||||||
|
import ( |
||||||
|
"os" |
||||||
|
|
||||||
|
"github.com/caarlos0/env/v6" |
||||||
|
"github.com/sirupsen/logrus" |
||||||
|
) |
||||||
|
|
||||||
|
func init() { |
||||||
|
InitLogger() |
||||||
|
} |
||||||
|
|
||||||
|
type config struct { |
||||||
|
Output string `env:"LOG_OUTPUT" envDefault:"stdout"` |
||||||
|
FileName string `env:"LOG_FILENAME" envDefault:"app.log"` |
||||||
|
Formatter string `env:"LOG_FORMATTER" envDefault:"json"` |
||||||
|
Level string `env:"LOG_LEVEL" envDefault:"error"` |
||||||
|
} |
||||||
|
|
||||||
|
func InitLogger() { |
||||||
|
log := logrus.StandardLogger() |
||||||
|
|
||||||
|
var cfg config |
||||||
|
if err := env.Parse(&cfg); err != nil { |
||||||
|
log.Error("log env vars parse error", err) |
||||||
|
} |
||||||
|
|
||||||
|
logFilename := cfg.FileName |
||||||
|
switch cfg.Output { |
||||||
|
case "file": |
||||||
|
LogOutputFile, err := os.OpenFile(logFilename, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666) |
||||||
|
if err == nil { |
||||||
|
log.SetOutput(LogOutputFile) |
||||||
|
} else { |
||||||
|
log.SetOutput(os.Stdout) |
||||||
|
} |
||||||
|
case "stderr": |
||||||
|
log.SetOutput(os.Stderr) |
||||||
|
case "stdin": |
||||||
|
log.SetOutput(os.Stdin) |
||||||
|
case "stdout": |
||||||
|
fallthrough |
||||||
|
default: |
||||||
|
log.SetOutput(os.Stdout) |
||||||
|
} |
||||||
|
|
||||||
|
switch cfg.Formatter { |
||||||
|
case "text": |
||||||
|
log.SetFormatter(&logrus.TextFormatter{}) |
||||||
|
case "json": |
||||||
|
fallthrough |
||||||
|
default: |
||||||
|
log.SetFormatter(&logrus.JSONFormatter{}) |
||||||
|
} |
||||||
|
|
||||||
|
if lvl, err := logrus.ParseLevel(cfg.Level); err != nil { |
||||||
|
log.SetLevel(logrus.DebugLevel) |
||||||
|
} else { |
||||||
|
log.SetLevel(lvl) |
||||||
|
} |
||||||
|
} |
||||||
Loading…
Reference in new issue