/
logger.go
197 lines (169 loc) · 5.28 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
package cxlogger
import (
"fmt"
"math/rand"
"strings"
"time"
log "gopkg.in/inconshreveable/log15.v2"
)
type Logger struct {
log.Logger
Context string
Level log.Lvl
}
func NewWithContext(context string, logOut string, lvl interface{}) (*Logger, error) {
l := Logger{}
return &l, l.InitializeWithContext(context, logOut, lvl)
}
func New(logOut string, lvl interface{}) (*Logger, error) {
l := Logger{}
return &l, l.Initialize(logOut, lvl)
}
func (l *Logger) InitializeWithContext(context string, logOut string, lvl interface{}) error {
l.Context = context
return l.Initialize(logOut, lvl)
}
func (l *Logger) Initialize(logOut string, lvl interface{}) error {
var (
level log.Lvl
err error
)
if l.Context == "" {
r := rand.New(rand.NewSource(time.Now().UnixNano()))
randInt := r.Int()
t := time.Now()
l.Context = fmt.Sprintf("%s#%d", t.Format(time.RFC3339), randInt)
}
if str, ok := lvl.(string); ok {
level, err = log.LvlFromString(str)
if err != nil {
return err
}
} else {
level = lvl.(log.Lvl)
}
l.Logger = log.New(log.Ctx{"context": l.Context})
l.Level = level
if logOut == "STDOUT" {
normalHandler := log.LvlFilterHandler(level, log.StdoutHandler)
errorHandler := log.LvlFilterHandler(level, log.CallerStackHandler("%+v", log.StdoutHandler))
handler := errorMultiHandler(normalHandler, errorHandler)
l.SetHandler(handler)
} else if logOut == "NONE" {
l.SetHandler(log.DiscardHandler())
} else {
fileHandler := log.Must.FileHandler(logOut, log.LogfmtFormat())
normalHandler := log.LvlFilterHandler(level, fileHandler)
errorHandler := log.LvlFilterHandler(level, log.CallerStackHandler("%+v", fileHandler))
handler := errorMultiHandler(normalHandler, errorHandler)
l.SetHandler(handler)
}
return nil
}
func (l *Logger) Debug(v ...interface{}) {
l.output(func(l *log.Logger, msg string, v ...interface{}) {
(*l).Debug(msg, v...)
}, v...)
}
func (l *Logger) Info(v ...interface{}) {
l.output(func(l *log.Logger, msg string, v ...interface{}) {
(*l).Info(msg, v...)
}, v...)
}
func (l *Logger) Warn(v ...interface{}) {
l.output(func(l *log.Logger, msg string, v ...interface{}) {
(*l).Warn(msg, v...)
}, v...)
}
func (l *Logger) Error(v ...interface{}) {
l.output(func(l *log.Logger, msg string, v ...interface{}) {
(*l).Error(msg, v...)
}, v...)
}
func (l *Logger) Crit(v ...interface{}) {
l.output(func(l *log.Logger, msg string, v ...interface{}) {
(*l).Crit(msg, v...)
}, v...)
}
func (l *Logger) Debugf(format string, v ...interface{}) { l.Debug(fmt.Sprintf(format, v...)) }
func (l *Logger) Infof(format string, v ...interface{}) { l.Info(fmt.Sprintf(format, v...)) }
func (l *Logger) Warnf(format string, v ...interface{}) { l.Warn(fmt.Sprintf(format, v...)) }
func (l *Logger) Errorf(format string, v ...interface{}) { l.Error(fmt.Sprintf(format, v...)) }
func (l *Logger) Critf(format string, v ...interface{}) { l.Crit(fmt.Sprintf(format, v...)) }
func (l *Logger) DebugIndent(indentation int, v ...interface{}) {
l.outputWithIndentation(func(l *Logger, v ...interface{}) {
l.Debug(v...)
}, indentation, v...)
}
func (l *Logger) InfoIndent(indentation int, v ...interface{}) {
l.outputWithIndentation(func(l *Logger, v ...interface{}) {
l.Info(v...)
}, indentation, v...)
}
func (l *Logger) WarnIndent(indentation int, v ...interface{}) {
l.outputWithIndentation(func(l *Logger, v ...interface{}) {
l.Warn(v...)
}, indentation, v...)
}
func (l *Logger) ErrorIndent(indentation int, v ...interface{}) {
l.outputWithIndentation(func(l *Logger, v ...interface{}) {
l.Error(v...)
}, indentation, v...)
}
func (l *Logger) CritIndent(indentation int, v ...interface{}) {
l.outputWithIndentation(func(l *Logger, v ...interface{}) {
l.Crit(v...)
}, indentation, v...)
}
func (l *Logger) IncreaseIndentation() {
contextAttributes.Lock()
defer contextAttributes.Unlock()
contextAttributes.Indentations[l.Context] += 1
}
func (l *Logger) DecreaseIndentation() {
contextAttributes.Lock()
defer contextAttributes.Unlock()
contextAttributes.Indentations[l.Context] -= 1
}
func errorMultiHandler(normalHandler, errorHandler log.Handler) log.Handler {
return log.FuncHandler(func(r *log.Record) error {
if len(r.Ctx) > 1 {
_, ok := r.Ctx[1].(error)
if ok {
r.Ctx = r.Ctx[2:]
errorHandler.Log(r)
} else {
normalHandler.Log(r)
}
} else {
normalHandler.Log(r)
}
return nil
})
}
func (l *Logger) output(logFunc func(l *log.Logger, msg string, v ...interface{}), v ...interface{}) {
err, ok := v[0].(error)
if ok {
logFunc(&l.Logger, l.currentIndentation()+err.Error(), "err", err)
} else {
msg := v[0].(string)
if len(v) > 1 {
logFunc(&l.Logger, l.currentIndentation()+msg, v[1:]...)
} else {
logFunc(&l.Logger, l.currentIndentation()+msg)
}
}
}
func (l *Logger) outputWithIndentation(logFunc func(l *Logger, v ...interface{}), indentation int, v ...interface{}) {
contextAttributes.Lock()
defer contextAttributes.Unlock()
oldIndentation := contextAttributes.Indentations[l.Context]
contextAttributes.Indentations[l.Context] = indentation
logFunc(l, v...)
contextAttributes.Indentations[l.Context] = oldIndentation
}
func (l *Logger) currentIndentation() string {
contextAttributes.RLock()
defer contextAttributes.RUnlock()
return strings.Repeat(" ", tabWidth*contextAttributes.Indentations[l.Context])
}