/
handler_impl.go
323 lines (290 loc) · 7.73 KB
/
handler_impl.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
package logging
import (
"context"
"encoding/csv"
"io"
"os"
"strconv"
"sync"
"sync/atomic"
"github.com/ugorji/go/codec"
"github.com/ugorji/go-common/ioutil"
"github.com/ugorji/go-common/osutil"
// "runtime/debug"
"fmt"
"strings"
"github.com/ugorji/go-common/errorutil"
)
// closedErr is returned by a Handler.Handle when logging is closed.
var closedErr = errorutil.String("logging: closed")
//const timeFmt = "2006-01-02 15:04:05.000000"
const timeFmt = "20060102 15:04:05.000000"
// fmtRecordMessage ensures that multi-line messages are indented for clarity
func fmtRecordMessage(s string) string {
var buf strings.Builder
var j = strings.IndexByte(s, '\n') + 1
for j != 0 && j < len(s) {
buf.WriteString(s[:j])
buf.WriteByte('\t')
s = s[j:]
j = strings.IndexByte(s, '\n') + 1
}
if buf.Len() == 0 {
return s
}
buf.WriteString(s)
return buf.String()
}
func fmtCtxId(ctx context.Context) (sId string) {
if ctx != nil {
if appctx, ok := ctx.Value(AppContextKey).(hasId); ok {
sId = appctx.Id()
} else if tid := ctx.Value(CorrelationIDContextKey); tid != nil {
sId = fmt.Sprintf("%v", tid)
}
}
if sId == "" {
sId = "-"
}
return
}
func fmtProgFunc(s string) string {
// Given any of these 3, return reload
// (*watcher).reload.func1
// (*watcher).reload
// reload
if i := strings.IndexByte(s, '.'); i != -1 {
s = s[i+1:]
if i = strings.IndexByte(s, '.'); i != -1 {
s = s[:i]
}
}
return s
}
var jsonHandle = codec.JsonHandle{
TermWhitespace: true,
HTMLCharsAsIs: true,
}
type JSONFormatter struct{}
func (h JSONFormatter) Format(ctx context.Context, r *Record, seqId string, w io.Writer) error {
// const timeFmt = "20060102 15:04:05.000000"
var t = struct {
Seq string `codec:"q"`
ContextID string `codec:"id"`
*Record
Message string `codec:"m"`
}{seqId, fmtCtxId(ctx), r, fmtRecordMessage(r.Message)}
return codec.NewEncoder(w, &jsonHandle).Encode(&t)
}
type CSVFormatter struct{}
func (h CSVFormatter) Format(ctx context.Context, r *Record, seqId string, w io.Writer) (err error) {
// Seq Level Timestamp Target Func File Line Message
var s [9]string
s[0] = seqId
s[1] = fmtCtxId(ctx)
s[2] = level2s[r.Level]
s[3] = r.Time.Format(timeFmt)
s[4] = r.Target
s[5] = r.ProgramFunc
s[6] = r.ProgramFile
s[7] = strconv.Itoa(int(r.ProgramLine))
s[8] = fmtRecordMessage(r.Message)
ww := csv.NewWriter(w)
if err = ww.Write(s[:]); err == nil {
ww.Flush()
err = ww.Error()
}
// v = buf.Bytes()
// // go's csv writer adds a new line to end of output - strip it
// if v[len(v)-1] == '\n' {
// v = v[:len(v)-1]
// }
return
}
type HumanFormatter struct {
ANSIColor bool
}
func (h HumanFormatter) Format(ctx context.Context, r *Record, seqId string, w io.Writer) (err error) {
// even if file is deleted or moved, write will not fail on an open file descriptor.
// so no need to try multiple times.
var sId = fmtCtxId(ctx)
var fmtstr string
if len(r.ProgramFile) < 2 {
if h.ANSIColor {
fmtstr = "%c %s %s \033[0;94m%s\033[0m \033[0;93m%s]\033[0m %s\n"
} else {
fmtstr = "%c %s %s %s %s] %s\n"
}
_, err = fmt.Fprintf(w, fmtstr,
r.Level.ShortString(), seqId, sId, r.Time.Format(timeFmt),
r.Target,
fmtRecordMessage(r.Message))
} else {
if h.ANSIColor {
fmtstr = "%c %s %s \033[0;94m%s\033[0m \033[0;93m%s\033[0m \033[0;92m%s %s:%d]\033[0m %s\n"
} else {
fmtstr = "%c %s %s %s %s %s %s:%d] %s\n"
}
_, err = fmt.Fprintf(w, fmtstr,
r.Level.ShortString(), seqId, sId, r.Time.Format(timeFmt),
r.Target, fmtProgFunc(r.ProgramFunc), r.ProgramFile, r.ProgramLine,
fmtRecordMessage(r.Message))
}
return
// return runtimeutil.BytesView(s)
}
// handlerWriter can handle writing to a stream or a file.
type handlerWriter struct {
fname string // file name ("" if not a regular opened file)
w0 io.Writer
f *os.File
bw *ioutil.BufWriter
ff Filter
buf []byte
mu sync.RWMutex
fmter Formatter
seq uint64
closed uint32 // 1=closed. 0=open. Use mutex/atomic to update.
}
// newHandlerWriter returns an un-opened writer.
// It returns nil if both w and fname are empty.
// When passed to AddLogger, AddLogger will call its Open method.
//
// if w=nil and fname is <stderr> or <stdout> respectively,
// then write to the standard err or standart out streams respectively.
func newHandlerWriter(w io.Writer, fname string, fmter Formatter, ff Filter) (h *handlerWriter) {
if w != nil {
fname = ""
} else if fname == "" {
return nil
}
// runtimeutil.P("returning new handlerWriter: w: %v, fname: %s", w, fname)
h = &handlerWriter{
w0: w,
fname: fname,
ff: ff,
closed: 1,
}
if fmter == nil {
h.fmter = HumanFormatter{ANSIColor: false}
} else {
h.fmter = fmter
}
return
}
// NewHandlerWriter returns an un-opened handler.
func NewHandlerWriter(w io.Writer, fmter Formatter, ff Filter) (h *handlerWriter) {
return newHandlerWriter(w, "", fmter, ff)
}
// NewHandlerFile returns an un-opened handler.
func NewHandlerFile(fname string, fmter Formatter, ff Filter) (h *handlerWriter) {
return newHandlerWriter(nil, fname, fmter, ff)
}
func (h *handlerWriter) Open(buffer uint16) (err error) {
// defer func() { runtimeutil.P("handlerWriter.Open closed: %d, error: %v", h.closed, err) }()
// debug.PrintStack()
h.mu.Lock()
defer h.mu.Unlock()
if h.closed == 0 {
return
}
// runtimeutil.P("opening ...")
h.buf = make([]byte, int(buffer))
if h.w0 != nil {
h.bw = ioutil.NewBufWriter(h.w0, h.buf)
} else if h.fname != "" {
h.f, err = os.OpenFile(h.fname, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
return
}
h.bw = ioutil.NewBufWriter(h.f, h.buf)
} else {
return NoWriterForHandlerErr
}
// h.w = h.bw
h.closed = 0
return
}
func (h *handlerWriter) Close() (err error) {
h.mu.Lock()
defer h.mu.Unlock()
if h.closed == 1 {
return
}
// runtimeutil.P("closing ...")
err = h.flush(false)
if h.fname == "" {
return
}
if h.f != nil {
err = errorutil.Multi([]error{err, h.f.Close()}).NonNilError()
}
// if v, ok := h.f.(io.Closer); ok {
// err = errorutil.Multi([]error{err, v.Close()}).NonNil()
// }
h.closed = 1
return
}
func (h *handlerWriter) flush(lock bool) (err error) {
if lock {
h.mu.Lock()
defer h.mu.Unlock()
}
if h.bw == nil {
return
}
err = h.bw.Flush()
return
}
func (h *handlerWriter) Filter() Filter {
return h.ff
}
func (h *handlerWriter) Flush() error {
return h.flush(true)
}
// Handle writes record to output.
func (h *handlerWriter) Handle(ctx context.Context, r *Record) (err error) {
// Handle is on the fast path, so use fine-grained locking, and atomic functions if possible
defer errorutil.OnError(&err)
if atomic.LoadUint32(&h.closed) == 1 {
return closedErr
}
h.mu.Lock()
err = h.fmter.Format(ctx, r, strconv.Itoa(int(atomic.AddUint64(&h.seq, 1))), h.bw)
// if _, err = h.bw.Write(rec); err == nil {
// _, err = h.bw.Write(h.nl[:])
// }
h.mu.Unlock()
return
}
func lhw(f *os.File) Handler {
return NewHandlerWriter(f,
// JSONFormatter{},
// CSVFormatter{},
HumanFormatter{ANSIColor: osutil.IsTerminal(int(f.Fd()))},
nil)
}
// BasicInit is used to simply initialize the logging subsystem.
//
// It creates a Handler for each name, logging using the HumanFormatter.
//
// - If the name is "" or <stderr>, then it logs to standard error stream
// - Else If the name is <stdout>, then it logs to standard output stream
// - Else it logs to a file with the name given
func BasicInit(names []string, c Config) (err error) {
for _, n := range names {
switch n {
case "", stderrName:
err = AddHandler(n, lhw(os.Stderr))
case stdoutName:
err = AddHandler(n, lhw(os.Stdout))
default:
err = AddHandler(n, NewHandlerFile(n, HumanFormatter{}, nil))
}
if err != nil {
return
}
}
AddLogger("", c.MinLevel, nil, names)
return Open(c)
}