1
0
mirror of https://github.com/imgproxy/imgproxy.git synced 2024-12-09 09:56:01 +02:00
imgproxy/logger/log.go
Christopher Brookes cfa4b596d1
feat(logger): implement structured log formatter gcp compliant (#1028)
* feat(logger): implement structured log formatter gcp compliant

* docs: add new gcp logging format
2022-11-05 18:20:57 +06:00

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
}