-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentry.go
164 lines (128 loc) · 2.92 KB
/
entry.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
package log
import (
"context"
"io"
"os"
"runtime"
"strings"
"time"
"github.com/opentracing/opentracing-go"
)
type logKey struct{}
var Key logKey = struct{}{}
type Entry struct {
fields Fields
span opentracing.Span
}
var emptyEntry = &Entry{}
func WithError(err error) *Entry {
return &Entry{
fields: Fields{
"error": err,
},
}
}
func (e *Entry) WithError(err error) *Entry {
return e.WithFields(Fields{
"error": err,
})
}
func (e *Entry) WithContext(ctx context.Context) *Entry {
if fields, ok := ctx.Value(Key).(*Fields); ok {
*e = *e.WithFields(*fields)
}
return e
}
func WithContext(ctx context.Context) *Entry {
e := new(Entry)
if fields, ok := ctx.Value(Key).(Fields); ok {
e.fields = fields
}
return e
}
/* func WithSpan(span opentracing.Span) *Entry {
return &Entry{
span: span,
}
}
func (e *Entry) WithSpan(span opentracing.Span) *Entry {
e.span = span
return e
} */
func (e *Entry) Clone() *Entry {
fields := make(Fields, len(e.fields))
for k, v := range e.fields {
fields[k] = v
}
return &Entry{
fields: fields,
span: e.span,
}
}
func Debug(msg string) {
emptyEntry.Debug(msg)
}
func (e *Entry) Debug(msg string) {
e.log(LogDebug, msg)
}
func Info(msg string) {
emptyEntry.Info(msg)
}
func (e *Entry) Info(msg string) {
e.log(LogInformational, msg)
}
func Warn(msg string) {
emptyEntry.Warn(msg)
}
func (e *Entry) Warn(msg string) {
e.log(LogWarning, msg)
}
func Error(msg string) {
emptyEntry.Error(msg)
}
func (e *Entry) Error(msg string) {
e.log(LogError, msg)
}
func (e *Entry) log(level LogLevel, format string) {
if level < config.LogLevel {
return
}
now := time.Now()
builder := new(strings.Builder)
file, fileLine, funcName := getFunctionInfo()
config.logger.createLogPoint(logPoint{builder, level, fileLine, file, funcName, format, e.fields, now})
if level == LogError && config.UseStdErr {
io.WriteString(os.Stderr, builder.String())
} else {
io.WriteString(config.Output, builder.String())
}
}
func getPrefix(level LogLevel) string {
if level == LogError {
return config.ErrorPrefix
} else if level == LogWarning {
return config.WarnPrefix
} else if level == LogInformational {
return config.InfoPrefix
}
return config.DebugPrefix
}
func getFunctionInfo() (file string, line int, name string) {
pc, _, _, _ := runtime.Caller(0)
// get package name and filename
ownPkgName := strings.Join(strings.Split(runtime.FuncForPC(pc).Name(), ".")[:2], ".")
pkgName := ownPkgName
callDepth := 1
// find the first package that isnt this one, excluding our tests
for pkgName == ownPkgName && !strings.HasSuffix(file, "_test.go") {
pc, file, line, _ = runtime.Caller(callDepth)
files := strings.Split(file, "/")
file = files[len(files)-1]
pkgName = strings.Join(strings.Split(runtime.FuncForPC(pc).Name(), ".")[:2], ".")
name = runtime.FuncForPC(pc).Name()
fns := strings.Split(name, ".")
name = fns[len(fns)-1]
callDepth++
}
return
}