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
| // Debugf ...
func Debugf(ctx context.Context, format string, v ...interface{}) {
logger.WithDepth(4).Debugf(ctx, format, v...)
}
// WithDepth returns cloned logger with new depth
func (l *Logger) WithDepth(depth int) *Logger {
r := l.clone()
r.callDepth = depth
return r
}
func (l *Logger) clone() *Logger {
return &Logger{
out: l.out,
fmtter: l.fmtter,
lvl: l.lvl,
callDepth: l.callDepth,
skipLine: l.skipLine,
fields: l.fields,
fieldsStr: l.fieldsStr,
mu: l.mu,
}
}
// 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)
}
}
func (l *Logger) getLine() string {
var str string
if !l.skipLine {
str = line(l.callDepth)
}
str = str + l.fieldsStr
if str != "" {
str = str + ":"
}
return str
}
func (l *Logger) output(record *Record) (err error) {
b := l.fmtter.Format(record)
l.mu.Lock()
defer l.mu.Unlock()
_, err = l.out.Write(b)
return
}
// Format formats the logs as "time [level] line message"
func (t *TextFormatter) Format(r *Record) (b []byte, err error) {
s := fmt.Sprintf("%s [%s] ", r.Time.Format(t.timeFormat), r.Lvl.string())
if len(r.Line) != 0 {
s = s + r.Line + " "
}
if len(r.Msg) != 0 {
s = s + r.Msg
}
b = []byte(s)
if len(b) == 0 || b[len(b)-1] != '\n' {
b = append(b, '\n')
}
return
}
|