-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathentry.go
More file actions
150 lines (129 loc) · 4.65 KB
/
Copy pathentry.go
File metadata and controls
150 lines (129 loc) · 4.65 KB
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
// Copyright (C) 2017, ccpaging <[email protected]>. All rights reserved.
// According to https://github.com/sirupsen/logrus/blob/master/entry.go
package nxlog4go
import (
"runtime"
"time"
"github.com/ccpaging/nxlog4go/driver"
)
// A Entry contains all of the pertinent information for each message
// It is the final or intermediate logging entry also. It contains all
// the fields passed with prefix and With(key, value ...). It's finally
// logged when Trace, Debug, Info, Warn, Error, Critical called on it.
type Entry struct {
rec *driver.Recorder
log *Logger
addSkip int
}
// NewEntry creates a logging entry with a logger
func NewEntry(l *Logger) *Entry {
return &Entry{
rec: &driver.Recorder{
Prefix: l.prefix,
},
log: l,
}
}
// SetPrefix sets the output prefix for the entry.
func (e *Entry) SetPrefix(prefix string) *Entry {
e.rec.Prefix = prefix
return e
}
// AddCallerSkip increases the number of callers skipped by caller annotation.
// When building wrappers around the logger Entry, supplying this function
// to reporting the caller's caller information of the wrapper code.
func (e *Entry) AddCallerSkip(addSkip int) *Entry {
e.addSkip = addSkip
return e
}
// With creates a child logger and adds structured context to it.
// Field pairs or values added to the child don't affect the parent, and vice versa.
func (e *Entry) With(args ...interface{}) *Entry {
e.rec.With(args...)
return e
}
// WithMore appends name-value pairs to the log entry.
func (e *Entry) WithMore(args ...interface{}) *Entry {
e.rec.WithMore(args...)
return e
}
// Log sends a log message with level, and message.
// Call depth:
// 2 - Where calling the wrapper of entry.Log(...)
// 1 - Where calling entry.Log(...)
// 0 - Where internal calling entry.flush()
func (e *Entry) Log(calldepth int, level int, arg0 interface{}, args ...interface{}) {
l := e.log
if !l.enabled(level) {
return
}
r := &driver.Recorder{
Prefix: e.rec.Prefix,
Level: level,
Message: driver.ArgsToString(arg0),
Created: time.Now(),
}
r.With(e.rec.Values...)
r.WithMore(args...)
if l.caller {
// Determine caller func - it's expensive.
_, r.Source, r.Line, _ = runtime.Caller(calldepth + e.addSkip)
} else {
r.Source, r.Line = "", 0
}
l.Dispatch(r)
}
// Finest logs a message at the finest log level.
// See Debug for an explanation of the arguments.
func (e *Entry) Finest(arg0 interface{}, args ...interface{}) {
e.Log(2, FINEST, arg0, args...)
}
// Fine logs a message at the fine log level.
// See Debug for an explanation of the arguments.
func (e *Entry) Fine(arg0 interface{}, args ...interface{}) {
e.Log(2, FINE, arg0, args...)
}
// Debug is a utility method for debug log messages.
// The behavior of Debug depends on the first argument:
// - arg0 is a string
// When given a string as the first argument, this behaves like Logf but with
// the DEBUG log level: the first argument is interpreted as a format for the
// latter arguments.
// - arg0 is a func()string
// When given a closure of type func()string, this logs the string returned by
// the closure iff it will be logged. The closure runs at most one time.
// - arg0 is interface{}
// When given anything else, formatted as string (ala Sprint).
func (e *Entry) Debug(arg0 interface{}, args ...interface{}) {
e.Log(2, DEBUG, arg0, args...)
}
// Trace logs a message at the trace log level.
// See Debug for an explanation of the arguments.
func (e *Entry) Trace(arg0 interface{}, args ...interface{}) {
e.Log(2, TRACE, arg0, args...)
}
// Info logs a message at the info log level.
// See Debug for an explanation of the arguments.
func (e *Entry) Info(arg0 interface{}, args ...interface{}) {
e.Log(2, INFO, arg0, args...)
}
// Warn logs a message at the warn log level and returns the formatted error.
// At the warn level and higher, there is no performance benefit if the
// message is not actually logged, because all formats are processed and all
// closures are executed to format the error message.
// See Debug for further explanation of the arguments.
func (e *Entry) Warn(arg0 interface{}, args ...interface{}) {
e.Log(2, WARN, arg0, args...)
}
// Error logs a message at the error log level and returns the formatted error,
// See Warn for an explanation of the performance and Debug for an explanation
// of the parameters.
func (e *Entry) Error(arg0 interface{}, args ...interface{}) {
e.Log(2, ERROR, arg0, args...)
}
// Critical logs a message at the critical log level and returns the formatted error,
// See Warn for an explanation of the performance and Debug for an explanation
// of the parameters.
func (e *Entry) Critical(arg0 interface{}, args ...interface{}) {
e.Log(2, CRITICAL, arg0, args...)
}