-
Notifications
You must be signed in to change notification settings - Fork 1
/
json_formatter.go
127 lines (108 loc) · 2.48 KB
/
json_formatter.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
package golog
import (
"bytes"
"encoding/json"
"fmt"
"reflect"
"strconv"
"time"
)
// checl interface implementation
var _ Formatter = new(JSONFormatter)
// JSONFormatter structure
type JSONFormatter struct {
DateFormat string
}
// Format log message to json format
func (f *JSONFormatter) Format(
b *bytes.Buffer,
lvl Level,
ctx Context,
msg string,
trace []byte,
) *bytes.Buffer {
dateFormat := f.DateFormat
if dateFormat == "" {
dateFormat = time.RFC3339
}
b.WriteString(`{"_t":"`)
b.WriteString(time.Now().Format(dateFormat))
b.WriteString(`", "_l":"`)
b.WriteString(LevelToString(lvl))
b.WriteString(`", "`)
for k, v := range ctx {
b.WriteString(k)
b.WriteString(`":`)
f.appendValue(b, v)
b.WriteString(`, "`)
}
b.WriteString(`_m":`)
b.WriteString(strconv.Quote(string(msg)))
if len(trace) > 0 {
b.WriteString(`,"_trace":`)
b.WriteString(strconv.Quote(string(trace)))
}
b.WriteString(`}`)
b.WriteByte('\n')
return b
}
func (f *JSONFormatter) appendValue(buf *bytes.Buffer, val interface{}) {
if val == nil {
buf.WriteString("null")
return
}
if err, ok := val.(error); ok {
buf.WriteString(`"`)
buf.WriteString(err.Error())
buf.WriteString(`"`)
return
}
value := reflect.ValueOf(val)
kind := value.Kind()
if kind == reflect.Ptr {
if value.IsNil() {
buf.WriteString("null")
return
}
value = value.Elem()
kind = value.Kind()
}
switch kind {
case reflect.Bool:
if value.Bool() {
buf.WriteString("true")
} else {
buf.WriteString("false")
}
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
buf.WriteString(strconv.FormatInt(value.Int(), 10))
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
buf.WriteString(strconv.FormatUint(value.Uint(), 10))
case reflect.Float32:
buf.WriteString(strconv.FormatFloat(value.Float(), 'g', -1, 32))
case reflect.Float64:
buf.WriteString(strconv.FormatFloat(value.Float(), 'g', -1, 64))
default:
var err error
var b []byte
if stringer, ok := val.(fmt.Stringer); ok {
b, err = json.Marshal(stringer.String())
} else {
b, err = json.Marshal(val)
}
if err != nil {
if s, ok := val.(string); ok {
b, err = json.Marshal(s)
} else if s, ok := val.(fmt.Stringer); ok {
b, err = json.Marshal(s.String())
} else {
b, err = json.Marshal(fmt.Sprintf("%#v", val))
}
if err != nil {
buf.WriteString(`"Could not Sprintf value"`)
return
}
}
buf.Write(b)
}
}