...
1 package fog
2
3 import (
4 "go.uber.org/zap"
5 "go.uber.org/zap/buffer"
6 "go.uber.org/zap/zapcore"
7 )
8
9
10
11 type encoder struct {
12 zapcore.Encoder
13 }
14
15 func newEncoder(cfg zapcore.EncoderConfig) zapcore.Encoder {
16 return encoder{zapcore.NewJSONEncoder(cfg)}
17 }
18
19
20
21 func (e encoder) Clone() zapcore.Encoder {
22 return encoder{e.Encoder.Clone()}
23 }
24
25
26 func (e encoder) EncodeEntry(entry zapcore.Entry, fields []zapcore.Field) (*buffer.Buffer, error) {
27 fields = append(fields, zap.Object(SourceKey, caller{&entry.Caller}))
28 return e.Encoder.EncodeEntry(entry, fields)
29 }
30
31
32
33 type caller struct {
34 *zapcore.EntryCaller
35 }
36
37 func (c caller) MarshalLogObject(enc zapcore.ObjectEncoder) error {
38 enc.AddString("function", c.EntryCaller.Function)
39 enc.AddString("file", c.EntryCaller.File)
40 enc.AddInt("line", c.EntryCaller.Line)
41 return nil
42 }
43
44 func encodeLevel() zapcore.LevelEncoder {
45 return func(l zapcore.Level, enc zapcore.PrimitiveArrayEncoder) {
46 switch {
47 case l == 0:
48 enc.AppendString(Info)
49 case l < 0:
50
51 enc.AppendString(Debug)
52 case l == zap.ErrorLevel:
53 enc.AppendString(Error)
54 }
55 }
56 }
57
View as plain text