-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsonwriter.go
140 lines (113 loc) · 3.04 KB
/
jsonwriter.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
package golog
import (
"context"
"fmt"
"io"
"sync"
"time"
"github.com/domonda/go-encjson"
)
var jsonWriterPool sync.Pool
type JSONWriter struct {
writer io.Writer
format *Format
buf []byte
}
func NewJSONWriter(writer io.Writer, format *Format) *JSONWriter {
return &JSONWriter{
writer: writer,
format: format,
buf: make([]byte, 0, 1024),
}
}
func getJSONWriter(writer io.Writer, format *Format) *JSONWriter {
if recycled, ok := jsonWriterPool.Get().(*JSONWriter); ok {
recycled.writer = writer
recycled.format = format
return recycled
}
return NewJSONWriter(writer, format)
}
func (w *JSONWriter) BeginMessage(_ context.Context, logger *Logger, t time.Time, level Level, text string) Writer {
next := getJSONWriter(w.writer, w.format)
next.beginWriteMessage(logger, t, level, text)
return next
}
func (w *JSONWriter) beginWriteMessage(logger *Logger, t time.Time, level Level, text string) {
w.buf = append(w.buf, '{')
if w.format.TimestampKey != "" {
w.buf = encjson.AppendKey(w.buf, w.format.TimestampKey)
w.buf = encjson.AppendTime(w.buf, t, w.format.TimestampFormat)
}
if w.format.LevelKey != "" {
w.buf = encjson.AppendKey(w.buf, w.format.LevelKey)
w.buf = encjson.AppendString(w.buf, logger.Config().Levels().Name(level))
}
if w.format.MessageKey != "" && text != "" {
if logger.prefix != "" {
text = logger.prefix + w.format.PrefixSep + text
}
w.buf = encjson.AppendKey(w.buf, w.format.MessageKey)
w.buf = encjson.AppendString(w.buf, text)
}
}
func (w *JSONWriter) CommitMessage() {
// Flush f.buf
if len(w.buf) > 0 {
_, err := w.writer.Write(append(w.buf, '}', ',', '\n'))
if err != nil && ErrorHandler != nil {
ErrorHandler(fmt.Errorf("golog.JSONWriter error: %w", err))
}
}
// Free
w.writer = nil
w.format = nil
w.buf = w.buf[:0]
jsonWriterPool.Put(w)
}
func (w *JSONWriter) FlushUnderlying() {
flushUnderlying(w.writer)
}
func (w *JSONWriter) String() string {
return string(w.buf)
}
func (w *JSONWriter) WriteKey(key string) {
w.buf = encjson.AppendKey(w.buf, key)
}
func (w *JSONWriter) WriteSliceKey(key string) {
w.buf = encjson.AppendKey(w.buf, key)
w.buf = encjson.AppendArrayStart(w.buf)
}
func (w *JSONWriter) WriteSliceEnd() {
w.buf = encjson.AppendArrayEnd(w.buf)
}
func (w *JSONWriter) WriteNil() {
w.buf = encjson.AppendNull(w.buf)
}
func (w *JSONWriter) WriteBool(val bool) {
w.buf = encjson.AppendBool(w.buf, val)
}
func (w *JSONWriter) WriteInt(val int64) {
w.buf = encjson.AppendInt(w.buf, val)
}
func (w *JSONWriter) WriteUint(val uint64) {
w.buf = encjson.AppendUint(w.buf, val)
}
func (w *JSONWriter) WriteFloat(val float64) {
w.buf = encjson.AppendFloat(w.buf, val)
}
func (w *JSONWriter) WriteString(val string) {
w.buf = encjson.AppendString(w.buf, val)
}
func (w *JSONWriter) WriteError(val error) {
w.buf = encjson.AppendString(w.buf, val.Error())
}
func (w *JSONWriter) WriteUUID(val [16]byte) {
w.buf = encjson.AppendUUID(w.buf, val)
}
func (w *JSONWriter) WriteJSON(val []byte) {
if len(val) == 0 {
val = []byte("null")
}
w.buf = append(w.buf, val...)
}