-
Notifications
You must be signed in to change notification settings - Fork 177
/
logger.go
167 lines (145 loc) · 4.1 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
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// Copyright 2020 ZUP IT SERVICOS EM TECNOLOGIA E INOVACAO SA
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package logger
import (
"encoding/json"
"fmt"
"log"
"github.com/sirupsen/logrus" // nolint
)
const (
// PanicLevel level, highest level of severity. Logs and then calls panic with the
// message passed to Debug, Info, ...
PanicLevel = logrus.PanicLevel
// FatalLevel level. Logs and then calls `logger.Exit(1)`. It will exit even if the
// logging level is set to Panic.
FatalLevel = logrus.FatalLevel
// ErrorLevel level. Logs. Used for errors that should definitely be noted.
// Commonly used for hooks to send errors to an error tracking service.
ErrorLevel = logrus.ErrorLevel
// WarnLevel level. Non-critical entries that deserve eyes.
WarnLevel = logrus.WarnLevel
// InfoLevel level. General operational entries about what's going on inside the
// application.
InfoLevel = logrus.InfoLevel
// DebugLevel level. Usually only enabled when debugging. Very verbose logging.
DebugLevel = logrus.DebugLevel
// TraceLevel level. Designates finer-grained informational events than the Debug.
TraceLevel = logrus.TraceLevel
)
var CurrentLevel = InfoLevel
func LogPanic(msg string, err error, args ...map[string]interface{}) {
if err != nil {
if len(args) > 0 {
logrus.WithFields(args[0]).WithError(err).Panic(msg)
return
}
logrus.WithError(err).Panic(msg)
}
}
func LogError(msg string, err error, args ...map[string]interface{}) {
if err != nil {
if len(args) > 0 {
logrus.WithFields(args[0]).WithError(err).Error(msg)
return
}
logrus.WithError(err).Error(msg)
}
}
func LogInfo(msg string, args ...interface{}) {
if args != nil {
logrus.Info(msg, args)
} else {
logrus.Info(msg)
}
}
func LogPrint(msg string) {
log.SetFlags(0)
log.Println(msg)
}
func SetLogLevel(level string) {
logLevel, err := logrus.ParseLevel(level)
if err != nil {
msg := fmt.Sprintf(
"Log level of type %s is wrong. Setting default level: \"%s\"", level, InfoLevel.String())
logrus.Error(msg)
logLevel = InfoLevel
}
logrus.SetLevel(logLevel)
CurrentLevel = logLevel
}
func LogPanicWithLevel(msg string, err error, args ...map[string]interface{}) {
if logrus.IsLevelEnabled(PanicLevel) && err != nil {
if len(args) > 0 {
logrus.WithFields(args[0]).WithError(err).Panic(msg)
}
logrus.WithError(err).Panic(msg)
}
}
func LogErrorWithLevel(msg string, err error, args ...map[string]interface{}) {
if logrus.IsLevelEnabled(ErrorLevel) && err != nil {
if len(args) > 0 {
logrus.WithFields(args[0]).WithError(err).Error(msg)
return
}
logrus.WithError(err).Error(msg)
}
}
func LogWarnWithLevel(msg string, args ...interface{}) {
if logrus.IsLevelEnabled(WarnLevel) {
if args != nil {
logrus.Warn(msg, args)
} else {
logrus.Warn(msg)
}
}
}
func LogInfoWithLevel(msg string, args ...interface{}) {
if logrus.IsLevelEnabled(InfoLevel) {
if args != nil {
logrus.Info(msg, args)
} else {
logrus.Info(msg)
}
}
}
func LogDebugWithLevel(msg string, args ...interface{}) {
if logrus.IsLevelEnabled(DebugLevel) {
if args != nil {
logrus.Debug(msg, args)
} else {
logrus.Debug(msg)
}
}
}
func LogTraceWithLevel(msg string, args ...interface{}) {
if logrus.IsLevelEnabled(TraceLevel) {
if args != nil {
logrus.Trace(msg, args)
} else {
logrus.Trace(msg)
}
}
}
func LogStringAsError(msg string) {
logrus.Error(msg)
}
func LogDebugJSON(message string, content interface{}) {
contentBytes, err := json.Marshal(content)
if err == nil {
LogDebugWithLevel(message, string(contentBytes))
} else {
LogDebugWithLevel(message, fmt.Sprintf("%v", content))
}
}