-
-
Notifications
You must be signed in to change notification settings - Fork 109
/
logrus.go
43 lines (34 loc) · 989 Bytes
/
logrus.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package logrusx
import (
"github.com/sirupsen/logrus"
"github.com/spf13/viper"
"github.com/ory/x/stringsx"
)
// New initializes logrus with environment variable configuration LOG_LEVEL and LOG_FORMAT.
func New() *logrus.Logger {
l := logrus.New()
ll, err := logrus.ParseLevel(
stringsx.Coalesce(
viper.GetString("log.level"),
viper.GetString("LOG_LEVEL"),
),
)
if err != nil {
ll = logrus.InfoLevel
}
l.Level = ll
if stringsx.Coalesce(
viper.GetString("log.format"),
viper.GetString("LOG_FORMAT"),
) == "json" {
l.Formatter = new(logrus.JSONFormatter)
}
return l
}
// HelpMessage returns a string containing a help message for setting up the logger.
func HelpMessage() string {
return `- LOG_LEVEL: Set the log level, supports "panic", "fatal", "error", "warn", "info" and "debug". Defaults to "info".
Example: LOG_LEVEL=panic
- LOG_FORMAT: Leave empty for text based log format, or set to "json" for JSON formatting.
Example: LOG_FORMAT=json`
}