-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.go
285 lines (240 loc) · 5.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
// Copyright Project Harbor Authors
//
// 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 log
import (
"fmt"
"io"
"os"
"runtime"
"strings"
"sync"
"time"
)
var logger = New(os.Stdout, NewTextFormatter(), WarningLevel, 4)
const srcSeparator = "harbor" + string(os.PathSeparator) + "src"
func init() {
lvl := os.Getenv("LOG_LEVEL")
if len(lvl) == 0 {
logger.SetLevel(InfoLevel)
return
}
level, err := parseLevel(lvl)
if err != nil {
logger.SetLevel(InfoLevel)
return
}
logger.SetLevel(level)
}
// Logger provides a struct with fields that describe the details of logger.
type Logger struct {
out io.Writer
fmtter Formatter
lvl Level
callDepth int
skipLine bool
mu sync.Mutex
}
// New returns a customized Logger
func New(out io.Writer, fmtter Formatter, lvl Level, options ...interface{}) *Logger {
// Default set to be 3
depth := 3
// If passed in as option, then reset depth
// Use index 0
if len(options) > 0 {
d, ok := options[0].(int)
if ok && d > 0 {
depth = d
}
}
return &Logger{
out: out,
fmtter: fmtter,
lvl: lvl,
callDepth: depth,
}
}
// DefaultLogger returns the default logger within the pkg, i.e. the one used in log.Infof....
func DefaultLogger() *Logger {
return logger
}
// SetOutput sets the output of Logger l
func (l *Logger) SetOutput(out io.Writer) {
l.mu.Lock()
defer l.mu.Unlock()
l.out = out
}
// SetFormatter sets the formatter of Logger l
func (l *Logger) SetFormatter(fmtter Formatter) {
l.mu.Lock()
defer l.mu.Unlock()
l.fmtter = fmtter
}
// SetLevel sets the level of Logger l
func (l *Logger) SetLevel(lvl Level) {
l.mu.Lock()
defer l.mu.Unlock()
l.lvl = lvl
}
// SetOutput sets the output of default Logger
func SetOutput(out io.Writer) {
logger.SetOutput(out)
}
// SetFormatter sets the formatter of default Logger
func SetFormatter(fmtter Formatter) {
logger.SetFormatter(fmtter)
}
// SetLevel sets the level of default Logger
func SetLevel(lvl Level) {
logger.SetLevel(lvl)
}
func (l *Logger) output(record *Record) (err error) {
b, err := l.fmtter.Format(record)
if err != nil {
return
}
l.mu.Lock()
defer l.mu.Unlock()
_, err = l.out.Write(b)
return
}
// Debug ...
func (l *Logger) Debug(v ...interface{}) {
if l.lvl <= DebugLevel {
record := NewRecord(time.Now(), fmt.Sprint(v...), l.getLine(), DebugLevel)
l.output(record)
}
}
// Debugf ...
func (l *Logger) Debugf(format string, v ...interface{}) {
if l.lvl <= DebugLevel {
record := NewRecord(time.Now(), fmt.Sprintf(format, v...), l.getLine(), DebugLevel)
l.output(record)
}
}
// Info ...
func (l *Logger) Info(v ...interface{}) {
if l.lvl <= InfoLevel {
record := NewRecord(time.Now(), fmt.Sprint(v...), l.getLine(), InfoLevel)
l.output(record)
}
}
// Infof ...
func (l *Logger) Infof(format string, v ...interface{}) {
if l.lvl <= InfoLevel {
record := NewRecord(time.Now(), fmt.Sprintf(format, v...), l.getLine(), InfoLevel)
l.output(record)
}
}
// Warning ...
func (l *Logger) Warning(v ...interface{}) {
if l.lvl <= WarningLevel {
record := NewRecord(time.Now(), fmt.Sprint(v...), l.getLine(), WarningLevel)
l.output(record)
}
}
// Warningf ...
func (l *Logger) Warningf(format string, v ...interface{}) {
if l.lvl <= WarningLevel {
record := NewRecord(time.Now(), fmt.Sprintf(format, v...), l.getLine(), WarningLevel)
l.output(record)
}
}
// Error ...
func (l *Logger) Error(v ...interface{}) {
if l.lvl <= ErrorLevel {
record := NewRecord(time.Now(), fmt.Sprint(v...), l.getLine(), ErrorLevel)
l.output(record)
}
}
// Errorf ...
func (l *Logger) Errorf(format string, v ...interface{}) {
if l.lvl <= ErrorLevel {
record := NewRecord(time.Now(), fmt.Sprintf(format, v...), l.getLine(), ErrorLevel)
l.output(record)
}
}
// Fatal ...
func (l *Logger) Fatal(v ...interface{}) {
if l.lvl <= FatalLevel {
record := NewRecord(time.Now(), fmt.Sprint(v...), l.getLine(), FatalLevel)
l.output(record)
}
os.Exit(1)
}
// Fatalf ...
func (l *Logger) Fatalf(format string, v ...interface{}) {
if l.lvl <= FatalLevel {
record := NewRecord(time.Now(), fmt.Sprintf(format, v...), l.getLine(), FatalLevel)
l.output(record)
}
os.Exit(1)
}
func (l *Logger) getLine() string {
if l.skipLine {
return ""
}
return line(l.callDepth)
}
// Debug ...
func Debug(v ...interface{}) {
logger.Debug(v...)
}
// Debugf ...
func Debugf(format string, v ...interface{}) {
logger.Debugf(format, v...)
}
// Info ...
func Info(v ...interface{}) {
logger.Info(v...)
}
// Infof ...
func Infof(format string, v ...interface{}) {
logger.Infof(format, v...)
}
// Warning ...
func Warning(v ...interface{}) {
logger.Warning(v...)
}
// Warningf ...
func Warningf(format string, v ...interface{}) {
logger.Warningf(format, v...)
}
// Error ...
func Error(v ...interface{}) {
logger.Error(v...)
}
// Errorf ...
func Errorf(format string, v ...interface{}) {
logger.Errorf(format, v...)
}
// Fatal ...
func Fatal(v ...interface{}) {
logger.Fatal(v...)
}
// Fatalf ...
func Fatalf(format string, v ...interface{}) {
logger.Fatalf(format, v...)
}
func line(callDepth int) string {
_, file, line, ok := runtime.Caller(callDepth)
if !ok {
file = "???"
line = 0
}
l := strings.SplitN(file, srcSeparator, 2)
if len(l) > 0 {
file = l[1]
}
return fmt.Sprintf("[%s:%d]:", file, line)
}