-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.go
75 lines (61 loc) · 1.41 KB
/
logger.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package main
import (
"os"
"strings"
log "github.com/sirupsen/logrus"
"github.com/dan13ram/wpokt-oracle/models"
)
var logger *log.Entry
func loggerInit() {
logFormat := strings.ToLower(os.Getenv("LOGGER_FORMAT"))
if logFormat == "text" {
log.SetFormatter(&log.TextFormatter{})
} else {
log.SetFormatter(&log.JSONFormatter{})
}
logLevel := strings.ToLower(os.Getenv("LOGGER_LEVEL"))
if logLevel == "debug" {
log.SetLevel(log.DebugLevel)
} else {
log.SetLevel(log.InfoLevel)
}
logger = log.WithFields(log.Fields{"module": "main"})
}
func init() {
loggerInit()
}
func initLogger(config models.LoggerConfig) {
logLevel := strings.ToLower(config.Level)
logger.Debug("Initializing logger")
switch logLevel {
case "trace":
log.SetLevel(log.TraceLevel)
case "debug":
log.SetLevel(log.DebugLevel)
case "info":
log.SetLevel(log.InfoLevel)
case "warn":
log.SetLevel(log.WarnLevel)
case "error":
log.SetLevel(log.ErrorLevel)
case "fatal":
log.SetLevel(log.FatalLevel)
case "panic":
log.SetLevel(log.PanicLevel)
default:
log.SetLevel(log.InfoLevel)
}
logFormat := strings.ToLower(config.Format)
switch logFormat {
case "text":
log.SetFormatter(&log.TextFormatter{})
case "json":
log.SetFormatter(&log.JSONFormatter{})
default:
log.SetFormatter(&log.JSONFormatter{})
}
logger.
WithField("log_format", logFormat).
WithField("log_level", logLevel).
Info("Initialized logger")
}