mirror of
https://github.com/imgproxy/imgproxy.git
synced 2024-11-24 08:12:38 +02:00
cfa4b596d1
* feat(logger): implement structured log formatter gcp compliant * docs: add new gcp logging format
55 lines
1.0 KiB
Go
55 lines
1.0 KiB
Go
package logger
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
|
|
logrus "github.com/sirupsen/logrus"
|
|
|
|
"github.com/imgproxy/imgproxy/v3/config/configurators"
|
|
)
|
|
|
|
func Init() error {
|
|
logrus.SetOutput(os.Stdout)
|
|
|
|
logFormat := "pretty"
|
|
logLevel := "info"
|
|
|
|
configurators.String(&logFormat, "IMGPROXY_LOG_FORMAT")
|
|
configurators.String(&logLevel, "IMGPROXY_LOG_LEVEL")
|
|
|
|
switch logFormat {
|
|
case "structured":
|
|
logrus.SetFormatter(&structuredFormatter{})
|
|
case "json":
|
|
logrus.SetFormatter(&logrus.JSONFormatter{})
|
|
case "gcp":
|
|
logrus.SetFormatter(&logrus.JSONFormatter{
|
|
FieldMap: logrus.FieldMap{
|
|
"level": "severity",
|
|
"msg": "message",
|
|
},
|
|
})
|
|
default:
|
|
logrus.SetFormatter(newPrettyFormatter())
|
|
}
|
|
|
|
levelLogLevel, err := logrus.ParseLevel(logLevel)
|
|
if err != nil {
|
|
levelLogLevel = logrus.InfoLevel
|
|
}
|
|
|
|
logrus.SetLevel(levelLogLevel)
|
|
|
|
if isSyslogEnabled() {
|
|
slHook, err := newSyslogHook()
|
|
if err != nil {
|
|
return fmt.Errorf("Unable to connect to syslog daemon: %s", err)
|
|
}
|
|
|
|
logrus.AddHook(slHook)
|
|
}
|
|
|
|
return nil
|
|
}
|