-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlog.go
More file actions
141 lines (119 loc) · 2.65 KB
/
log.go
File metadata and controls
141 lines (119 loc) · 2.65 KB
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
package teapot
import (
"io"
"os"
"runtime"
"strconv"
"strings"
"sync"
"time"
)
// Option
type Option func(*Logger)
// WithLevel
func WithLevel(level Level) Option { return func(l *Logger) { l.lvl = level } }
// WithWriter
func WithWriter(w io.Writer) Option { return func(l *Logger) { l.w = w } }
// Logger
type Logger struct {
mu sync.Mutex
p *sync.Pool
w io.Writer
lvl Level
f Format
}
// New
func New(opts ...Option) *Logger {
l := &Logger{
w: os.Stdout,
lvl: DEBUG,
f: JSON,
mu: sync.Mutex{},
p: &sync.Pool{
New: func() any {
buf := make([]byte, 0, 1024)
return &buf
},
},
}
for _, o := range opts {
o(l)
}
return l
}
// Debug
func (l *Logger) Debug(format string, attrs ...Attr) {
l.printf(format, DEBUG, attrs...)
}
// Info
func (l *Logger) Info(format string, attrs ...Attr) {
l.printf(format, INFO, attrs...)
}
// Error
func (l *Logger) Error(format string, attrs ...Attr) {
l.printf(format, ERROR, attrs...)
}
// Fatal
func (l *Logger) Fatal(format string, attrs ...Attr) {
l.printf(format, FATAL, attrs...)
os.Exit(1)
}
func (l *Logger) printf(format string, level Level, attrs ...Attr) {
if level < l.lvl {
return
}
ptr := l.p.Get().(*[]byte)
buf := (*ptr)[:0]
switch l.f {
// TODO
// implement text logger
// case TEXT:
case JSON:
buf = append(buf, `{"time":"`...)
buf = time.Now().UTC().AppendFormat(buf, time.RFC3339Nano)
buf = append(buf, `", "level":"`...)
buf = append(buf, levelName[l.lvl]...)
buf = append(buf, `", "message":"`...)
buf = append(buf, format...)
buf = append(buf, `"`...)
for _, attr := range attrs {
buf = append(buf, `,"`...)
buf = append(buf, attr.key...)
buf = append(buf, `":`...)
switch attr.kfield {
case kint:
buf = strconv.AppendInt(buf, int64(attr.intValue), 10)
case kint16:
buf = strconv.AppendInt(buf, int64(attr.int16Value), 10)
case kint32:
buf = strconv.AppendInt(buf, int64(attr.int32Value), 10)
case kint64:
buf = strconv.AppendInt(buf, attr.int64Value, 10)
case kbool:
buf = strconv.AppendBool(buf, attr.boolValue)
case kstring:
buf = strconv.AppendQuote(buf, attr.stringValue)
case kstringslice:
s := strings.Join(attr.stringSliceValue, ",")
buf = strconv.AppendQuote(buf, s)
case kerr:
buf = strconv.AppendQuote(buf, attr.errValue.Error())
}
}
if level >= ERROR {
buf = append(buf, `, "stacktrace":`...)
var sb = make([]byte, 2048)
n := runtime.Stack(sb, false)
buf = strconv.AppendQuote(buf, string(sb[:n]))
}
buf = append(buf, "}"...)
default:
return
}
buf = append(buf, '\n')
l.mu.Lock()
l.w.Write(buf)
l.mu.Unlock()
*ptr = buf
l.p.Put(ptr)
}