-
Notifications
You must be signed in to change notification settings - Fork 2
/
builtin.go
209 lines (191 loc) · 4.47 KB
/
builtin.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
// Deprecated: use slog.
// Package builtin is a wrapper of the built-in log package.
package builtin
import (
"bytes"
"encoding/json"
"fmt"
"log"
"os"
"reflect"
"time"
)
// LogLevel is level of log that will be printed.
// Will print level that is higher than your
// chosen one.
type LogLevel int8
// Available log level.
const (
TraceLevel LogLevel = iota - 1
DebugLevel
InfoLevel
WarnLevel
ErrorLevel
FatalLevel
PanicLevel
Disabled
)
// Log is logging client.
type Log struct {
log *log.Logger
level LogLevel
json bool
color bool
}
var colorMap = map[LogLevel]int{
TraceLevel: 94,
DebugLevel: 93,
InfoLevel: 32,
WarnLevel: 33,
ErrorLevel: 91,
FatalLevel: 31,
PanicLevel: 35,
}
// New to create new logging client.
// Color is not working in json format.
func New(level LogLevel, jsonFmt, color bool) *Log {
return &Log{
log: log.New(os.Stderr, "", 0),
level: level,
json: jsonFmt,
color: color,
}
}
func (l *Log) colorize(c int, s interface{}) string {
if !l.color {
return fmt.Sprintf("%s", s)
}
return fmt.Sprintf("\x1b[%dm%v\x1b[0m", c, s)
}
func (l *Log) print(lvl LogLevel, fields map[string]interface{}, str string, args ...interface{}) {
if l.json {
b := &bytes.Buffer{}
b.WriteByte('{')
b.WriteString(fmt.Sprintf(`"level":"%s"`, l.getLevelStr(lvl)))
b.WriteString(fmt.Sprintf(`,"time":"%s"`, time.Now().Format(time.RFC3339)))
if str != "" {
b.WriteString(fmt.Sprintf(`,"message":"%s"`, fmt.Sprintf(str, args...)))
}
for k, v := range fields {
j, _ := json.Marshal(v)
b.WriteString(fmt.Sprintf(`,"%s":%s`, k, j))
}
b.WriteByte('}')
l.log.Println(b)
} else {
b := &bytes.Buffer{}
b.WriteString(l.colorize(90, time.Now().Format(time.RFC3339)))
b.WriteByte(' ')
l.printLevel(lvl, b)
b.WriteByte(' ')
b.WriteString(fmt.Sprintf(str, args...))
for k, v := range fields {
if k == "error" {
b.WriteString(l.colorize(31, fmt.Sprintf(" %s=%v", k, v)))
} else {
b.WriteString(fmt.Sprintf(" %s=%v", l.colorize(36, k), v))
}
}
l.log.Println(b)
}
}
func (l *Log) printLevel(lvl LogLevel, b *bytes.Buffer) {
switch lvl {
case TraceLevel:
b.WriteString(l.colorize(colorMap[lvl], "TRC"))
case DebugLevel:
b.WriteString(l.colorize(colorMap[lvl], "DBG"))
case InfoLevel:
b.WriteString(l.colorize(colorMap[lvl], "INF"))
case WarnLevel:
b.WriteString(l.colorize(colorMap[lvl], "WRN"))
case ErrorLevel:
b.WriteString(l.colorize(colorMap[lvl], "ERR"))
case FatalLevel:
b.WriteString(l.colorize(colorMap[lvl], "FTL"))
case PanicLevel:
b.WriteString(l.colorize(colorMap[lvl], "PNC"))
}
}
func (l *Log) getLevelStr(lvl LogLevel) string {
switch lvl {
case TraceLevel:
return "trace"
case DebugLevel:
return "debug"
case InfoLevel:
return "info"
case WarnLevel:
return "warn"
case ErrorLevel:
return "error"
case FatalLevel:
return "fatal"
case PanicLevel:
return "panic"
default:
return "log"
}
}
// Trace to print trace log.
func (l *Log) Trace(fmt string, args ...interface{}) {
if l.level <= TraceLevel {
l.print(TraceLevel, nil, fmt, args...)
}
}
// Debug to print debug log.
func (l *Log) Debug(fmt string, args ...interface{}) {
if l.level <= DebugLevel {
l.print(DebugLevel, nil, fmt, args...)
}
}
// Info to print info log.
func (l *Log) Info(fmt string, args ...interface{}) {
if l.level <= InfoLevel {
l.print(InfoLevel, nil, fmt, args...)
}
}
// Warn to print warn log.
func (l *Log) Warn(fmt string, args ...interface{}) {
if l.level <= WarnLevel {
l.print(WarnLevel, nil, fmt, args...)
}
}
// Error to print error log.
func (l *Log) Error(fmt string, args ...interface{}) {
if l.level <= ErrorLevel {
l.print(ErrorLevel, nil, fmt, args...)
}
}
// Fatal to print fatal log.
// Will exit the program when called.
func (l *Log) Fatal(fmt string, args ...interface{}) {
if l.level <= FatalLevel {
l.print(FatalLevel, nil, fmt, args...)
os.Exit(1)
}
}
// Panic to print panic log.
// Will print panic error stack and exit
// like panic().
func (l *Log) Panic(str string, args ...interface{}) {
if l.level <= PanicLevel {
l.print(PanicLevel, nil, str, args...)
panic(fmt.Sprintf(str, args...))
}
}
// Log to print general log.
// Key `level` can be used to differentiate
// log level.
func (l *Log) Log(fields map[string]interface{}) {
level, ok := fields["level"]
if ok {
switch reflect.TypeOf(level).Kind() {
case reflect.Int8:
delete(fields, "level")
l.print(LogLevel(reflect.ValueOf(level).Int()), fields, "")
return
}
}
l.print(0, fields, "")
}