-
Notifications
You must be signed in to change notification settings - Fork 328
/
log.go
153 lines (125 loc) · 3.66 KB
/
log.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Cilium
package logger
import (
"fmt"
"os"
"strings"
"github.com/sirupsen/logrus"
)
type LogFormat string
const (
levelOpt = "level"
formatOpt = "format"
logFormatText LogFormat = "text"
logFormatJSON LogFormat = "json"
defaultLogFormat LogFormat = logFormatText
defaultLogLevel logrus.Level = logrus.InfoLevel
)
var (
// DefaultLogger is the base logrus logger. It is different from the logrus
// default to avoid external dependencies from writing out unexpectedly
DefaultLogger = InitializeDefaultLogger()
)
// LogOptions maps configuration key-value pairs related to logging.
type LogOptions map[string]string
// InitializeDefaultLogger returns a logrus Logger with a custom text formatter.
func InitializeDefaultLogger() (logger *logrus.Logger) {
logger = logrus.New()
fmt, _ := getFormatter(defaultLogFormat)
logger.SetFormatter(fmt)
logger.SetLevel(defaultLogLevel)
return
}
// getFormatter returns a configured logrus.Formatter with some specific values
// we want to have
func getFormatter(format LogFormat) (logrus.Formatter, error) {
switch format {
case logFormatText:
return &logrus.TextFormatter{
DisableColors: true,
}, nil
case logFormatJSON:
return &logrus.JSONFormatter{}, nil
default:
return &logrus.TextFormatter{}, fmt.Errorf("invalid log format '%s'", string(format))
}
}
func (o LogOptions) getLogLevel() (level logrus.Level) {
l, ok := o[levelOpt]
if !ok {
return defaultLogLevel
}
var err error
if level, err = logrus.ParseLevel(l); err != nil {
logrus.WithError(err).Warning("Ignoring user-configured log level")
return defaultLogLevel
}
return
}
func (o LogOptions) getLogFormat() LogFormat {
format, ok := o[formatOpt]
if !ok {
return defaultLogFormat
}
// It was already validate with PopulateLogOpts()
return LogFormat(strings.ToLower(format))
}
func ResetLogOutput() {
DefaultLogger.SetOutput(os.Stdout)
}
func GetLogLevel() logrus.Level {
return DefaultLogger.GetLevel()
}
func SetLogLevel(logLevel logrus.Level) {
DefaultLogger.SetLevel(logLevel)
}
func setLogLevelToDebug() {
DefaultLogger.SetLevel(logrus.DebugLevel)
}
func setLogFormat(logFormat LogFormat) {
fmt, err := getFormatter(logFormat)
if err != nil {
logrus.WithError(err).Warning("Ignoring user-configured log format")
}
DefaultLogger.SetFormatter(fmt)
}
// PopulateLogOpts populates the logger options making sure that passed values are valid.
func PopulateLogOpts(o LogOptions, level string, format string) {
if level != "" {
_, err := logrus.ParseLevel(level)
if err != nil {
logrus.WithError(fmt.Errorf("incorrect log level '%s'", level)).Warning("Ignoring user-configured log level")
} else {
o[levelOpt] = level
}
}
if format != "" {
format = strings.ToLower(format)
switch LogFormat(format) {
case logFormatText, logFormatJSON:
o[formatOpt] = format
default:
logrus.WithError(fmt.Errorf("incorrect log format '%s', expected 'text' or 'json'", format)).Warning("Ignoring user-configured log format")
}
}
}
// SetupLogging setup logger options taking into consideration the debug flag.
func SetupLogging(o LogOptions, debug bool) error {
// Updating the default log format
setLogFormat(o.getLogFormat())
logrus.SetOutput(os.Stdout)
// Updating the default log level, overriding the log options if the debug arg is being set
if debug {
setLogLevelToDebug()
} else {
SetLogLevel(o.getLogLevel())
}
// always suppress the default logger so libraries don't print things
logrus.SetLevel(logrus.PanicLevel)
return nil
}
// GetLogger returns the DefaultLogger that was previously setup
func GetLogger() logrus.FieldLogger {
return DefaultLogger
}