-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.go
204 lines (162 loc) · 3.9 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
203
204
package rslog
import (
"fmt"
"runtime"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"gopkg.in/natefinch/lumberjack.v2"
"github.com/realsangil/apimonitor/pkg/rserrors"
)
const (
FormatJSON = "json"
FormatText = "text"
OutputFile = "file"
OutputConsole = "console"
LevelDebug = "debug"
LevelInfo = "info"
LevelWanr = "warn"
LevelError = "error"
LevelFatal = "fatal"
)
var (
std = logrus.New()
)
func init() {
std.SetFormatter(&Formatter{})
}
func setLevel(level string) error {
var logLevel logrus.Level
switch level {
case LevelDebug:
logLevel = logrus.DebugLevel
case LevelInfo:
logLevel = logrus.InfoLevel
case LevelWanr:
logLevel = logrus.WarnLevel
case LevelError:
logLevel = logrus.ErrorLevel
case LevelFatal:
logLevel = logrus.FatalLevel
default:
return rserrors.Error("invalid logger level")
}
std.SetLevel(logLevel)
return nil
}
func setOutput(output, path string) error {
switch output {
case OutputFile:
lumberjackLogger := &lumberjack.Logger{
Filename: path,
MaxSize: 100,
MaxBackups: 30,
MaxAge: 30,
LocalTime: true,
Compress: true,
}
std.SetOutput(lumberjackLogger)
case OutputConsole:
default:
return rserrors.Error("invalid logger output")
}
return nil
}
func Init(config LogConfig) error {
if err := setLevel(config.GetLevel()); err != nil {
return errors.WithStack(err)
}
if err := setOutput(config.GetOutput(), config.GetPath()); err != nil {
return errors.WithStack(err)
}
return nil
}
func Trace(args ...interface{}) {
getStdWithPrefix().Trace(args...)
}
func Debug(args ...interface{}) {
getStdWithPrefix().Debug(args...)
}
func Print(args ...interface{}) {
getStdWithPrefix().Print(args...)
}
func Info(args ...interface{}) {
getStdWithPrefix().Info(args...)
}
func Warn(args ...interface{}) {
getStdWithPrefix().Warn(args...)
}
func Warning(args ...interface{}) {
getStdWithPrefix().Warning(args...)
}
func Error(args ...interface{}) {
getStdWithPrefix().Error(args...)
}
func Panic(args ...interface{}) {
getStdWithPrefix().Panic(args...)
}
func Fatal(args ...interface{}) {
getStdWithPrefix().Fatal(args...)
}
func Tracef(format string, args ...interface{}) {
getStdWithPrefix().Tracef(format, args...)
}
func Debugf(format string, args ...interface{}) {
getStdWithPrefix().Debugf(format, args...)
}
func Printf(format string, args ...interface{}) {
getStdWithPrefix().Printf(format, args...)
}
func Infof(format string, args ...interface{}) {
getStdWithPrefix().Infof(format, args...)
}
func Warnf(format string, args ...interface{}) {
getStdWithPrefix().Warnf(format, args...)
}
func Warningf(format string, args ...interface{}) {
getStdWithPrefix().Warningf(format, args...)
}
func Errorf(format string, args ...interface{}) {
getStdWithPrefix().Errorf(format, args...)
}
func Panicf(format string, args ...interface{}) {
getStdWithPrefix().Panicf(format, args...)
}
func Fatalf(format string, args ...interface{}) {
getStdWithPrefix().Fatalf(format, args...)
}
func Traceln(args ...interface{}) {
getStdWithPrefix().Traceln(args...)
}
func Debugln(args ...interface{}) {
getStdWithPrefix().Debugln(args...)
}
func Println(args ...interface{}) {
getStdWithPrefix().Println(args...)
}
func Infoln(args ...interface{}) {
getStdWithPrefix().Infoln(args...)
}
func Warnln(args ...interface{}) {
getStdWithPrefix().Warnln(args...)
}
func Warningln(args ...interface{}) {
getStdWithPrefix().Warningln(args...)
}
func Errorln(args ...interface{}) {
getStdWithPrefix().Errorln(args...)
}
func Panicln(args ...interface{}) {
getStdWithPrefix().Panicln(args...)
}
func Fatalln(args ...interface{}) {
getStdWithPrefix().Fatalln(args...)
}
func getStdWithPrefix() *logrus.Entry {
pc := make([]uintptr, 15)
n := runtime.Callers(3, pc)
frames := runtime.CallersFrames(pc[:n])
frame, _ := frames.Next()
return std.WithFields(logrus.Fields{
"func": fmt.Sprintf("%s:%d", frame.Function, frame.Line),
})
}