forked from dominant-strategies/go-quai
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.go
202 lines (171 loc) · 4.75 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package log
import (
"fmt"
"os"
"path/filepath"
"runtime"
"strings"
"github.com/natefinch/lumberjack"
"github.com/sirupsen/logrus"
"gopkg.in/urfave/cli.v1"
)
type Logger struct {
*logrus.Logger
}
var Log Logger = Logger{logrus.New()}
func init() {
}
func ConfigureLogger(ctx *cli.Context) {
logLevel := logrus.Level(ctx.GlobalInt("verbosity"))
Log.SetLevel(logLevel)
logToStdOut := ctx.GlobalBool("logtostdout")
log_filename := "nodelogs"
regionNum := ctx.GlobalString("region")
if ctx.GlobalIsSet("zone") {
zoneNum := ctx.GlobalString("zone")
log_filename = filepath.Join(log_filename, "zone-"+regionNum+"-"+zoneNum)
} else if ctx.GlobalIsSet("region") {
log_filename = filepath.Join(log_filename, "region-"+regionNum)
} else {
log_filename = filepath.Join(log_filename, "prime")
}
log_filename += ".log"
Log.Formatter = &logrus.TextFormatter{
ForceColors: ctx.GlobalBool("showcolors"),
PadLevelText: true,
FullTimestamp: true,
TimestampFormat: "01-02|15:04:05.000",
}
if logToStdOut {
Log.SetOutput(os.Stdout)
} else {
Log.SetOutput(&lumberjack.Logger{
Filename: log_filename,
MaxSize: 500, // megabytes
MaxBackups: 5,
MaxAge: 28, //days
})
}
}
func SetLevelInt(level int) {
Log.SetLevel(logrus.Level(level))
}
func SetLevelString(level string) {
logLevel, err := logrus.ParseLevel(level)
if err != nil {
Log.Error("Invalid log level: ", level)
return
}
Log.SetLevel(logLevel)
}
func New(out_path string) Logger {
logger := logrus.New()
logger.SetOutput(&lumberjack.Logger{
Filename: out_path,
MaxSize: 500, // megabytes
MaxBackups: 3,
MaxAge: 28, //days
})
return Logger{logger}
}
// Uses of the global logger will use the following static method.
func Trace(msg string, args ...interface{}) {
Log.Trace(constructLogMessage(msg, args...))
}
// Individual logging instances will use the following method.
func (l Logger) Trace(msg string, args ...interface{}) {
l.Logger.Trace(constructLogMessage(msg, args...))
}
func Debug(msg string, args ...interface{}) {
Log.Debug(constructLogMessage(msg, args...))
}
func (l Logger) Debug(msg string, args ...interface{}) {
l.Logger.Debug(constructLogMessage(msg, args...))
}
func Info(msg string, args ...interface{}) {
Log.Info(constructLogMessage(msg, args...))
}
func (l Logger) Info(msg string, args ...interface{}) {
l.Logger.Info(constructLogMessage(msg, args...))
}
func Warn(msg string, args ...interface{}) {
Log.Warn(constructLogMessage(msg, args...))
}
func (l Logger) Warn(msg string, args ...interface{}) {
l.Logger.Warn(constructLogMessage(msg, args...))
}
func Error(msg string, args ...interface{}) {
Log.Error(constructLogMessage(msg, args...))
}
func (l Logger) Error(msg string, args ...interface{}) {
l.Logger.Error(constructLogMessage(msg, args...))
}
func Fatal(msg string, args ...interface{}) {
Log.Fatal(constructLogMessage(msg, args...))
}
func (l Logger) Fatal(msg string, args ...interface{}) {
l.Logger.Fatal(constructLogMessage(msg, args...))
}
func Panic(msg string, args ...interface{}) {
Log.Panic(constructLogMessage(msg, args...))
}
func (l Logger) Panic(msg string, args ...interface{}) {
l.Logger.Panic(constructLogMessage(msg, args...))
}
func Lazy(fn func() string, logLevel string) {
level, err := logrus.ParseLevel(logLevel)
if err == nil && Log.IsLevelEnabled(level) {
callCorrectLevel(level, fn())
}
}
func reportLineNumber(skiplevel int) string {
if Logger.GetLevel(Log) < logrus.DebugLevel {
return ""
}
_, file, line, ok := runtime.Caller(skiplevel + 1)
fileAndDir := filepath.Join(filepath.Base(filepath.Dir(file)), filepath.Base(file))
if !ok || fileAndDir == "log/logger.go" {
return ""
}
return fmt.Sprintf("%s:%d", fileAndDir, line)
}
func callCorrectLevel(level logrus.Level, msg string, args ...interface{}) {
switch level {
case logrus.TraceLevel:
Trace(msg, args...)
case logrus.DebugLevel:
Debug(msg, args...)
case logrus.InfoLevel:
Info(msg, args...)
case logrus.WarnLevel:
Warn(msg, args...)
case logrus.ErrorLevel:
Error(msg, args...)
case logrus.FatalLevel:
Fatal(msg, args...)
case logrus.PanicLevel:
Panic(msg, args...)
default:
Error("Unknown log level: %v", level)
}
}
func constructLogMessage(msg string, fields ...interface{}) string {
var pairs []string
lineInfo := reportLineNumber(2)
if len(fields) != 1 {
// Sometimes we want to log a single string,
if len(fields)%2 != 0 {
fields = append(fields, "MISSING VALUE")
}
for i := 0; i < len(fields); i += 2 {
key := fields[i]
value := fields[i+1]
pairs = append(pairs, fmt.Sprintf("%v=%v", key, value))
}
}
if lineInfo != "" {
return fmt.Sprintf("%-40s %-40s %s", lineInfo, msg, strings.Join(pairs, " "))
} else {
return fmt.Sprintf("%-40s %s", msg, strings.Join(pairs, " "))
}
}