-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdefault.go
168 lines (149 loc) · 4.38 KB
/
default.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
package prettyconsole
import (
"io"
"os"
"path/filepath"
"reflect"
"runtime/debug"
"strconv"
"strings"
"time"
"github.com/Code-Hex/dd"
"go.uber.org/zap/buffer"
"go.uber.org/zap/zapcore"
)
const (
colorBlack = iota + 30
colorRed
colorGreen
colorYellow
colorBlue
colorMagenta
colorCyan
colorWhite
colorBold = 1
colorDarkGray = 90
)
func DefaultTimeEncoder(format string) func(time.Time, zapcore.PrimitiveArrayEncoder) {
return func(t time.Time, enc zapcore.PrimitiveArrayEncoder) {
buf := _bufferPoolGet()
colorize(buf, t.Format(format), strconv.Itoa(colorDarkGray))
enc.AppendString(buf.String())
buf.Free()
}
}
func defaultDurationEncoder(dur time.Duration, enc zapcore.PrimitiveArrayEncoder) {
enc.AppendString(dur.String())
}
const defaultColourOffset = 2
var defaultColours = [10][]string{
// DIY trace level
zapcore.DebugLevel - 1 + defaultColourOffset: {strconv.Itoa(colorDarkGray)},
zapcore.DebugLevel + defaultColourOffset: {strconv.Itoa(colorCyan)},
zapcore.InfoLevel + defaultColourOffset: {strconv.Itoa(colorGreen)},
zapcore.WarnLevel + defaultColourOffset: {strconv.Itoa(colorYellow)},
zapcore.ErrorLevel + defaultColourOffset: {strconv.Itoa(colorRed)},
zapcore.FatalLevel + defaultColourOffset: {strconv.Itoa(colorRed), strconv.Itoa(colorBold)},
zapcore.DPanicLevel + defaultColourOffset: {strconv.Itoa(colorRed), strconv.Itoa(colorBold)},
zapcore.PanicLevel + defaultColourOffset: {strconv.Itoa(colorRed), strconv.Itoa(colorBold)},
}
func defaultLevelEncoder(l zapcore.Level, enc zapcore.PrimitiveArrayEncoder) {
var str string
switch l {
// DIY trace level
case zapcore.DebugLevel - 1:
str = "TRC"
case zapcore.DebugLevel:
str = "DBG"
case zapcore.InfoLevel:
str = "INF"
case zapcore.WarnLevel:
str = "WRN"
case zapcore.ErrorLevel:
str = "ERR"
case zapcore.FatalLevel:
str = "FTL"
case zapcore.DPanicLevel:
str = "DPNC"
case zapcore.PanicLevel:
str = "PNC"
default:
l = zapcore.PanicLevel
str = "???"
}
buf := _bufferPoolGet()
colorize(buf, str, defaultColours[l+defaultColourOffset]...)
enc.AppendString(buf.String())
buf.Free()
}
func defaultCallerEncoder(caller zapcore.EntryCaller, enc zapcore.PrimitiveArrayEncoder) {
callerFullPath := caller.FullPath()
var str string
if cwd, err := os.Getwd(); err == nil {
if rel, err := filepath.Rel(cwd, callerFullPath); err == nil {
str = rel
}
}
if str == "" {
// May have been built with -trimpath which will cause paths to be
// package paths instead of file paths try trimming the main module
// path else this will fall back to the full path
str = callerFullPath
if buildInfo, ok := debug.ReadBuildInfo(); ok {
str = strings.TrimPrefix(str, buildInfo.Main.Path+"/")
}
}
buf := _bufferPoolGet()
colorize(buf, str, strconv.Itoa(colorBold))
enc.AppendString(buf.String())
buf.Free()
}
func defaultNameEncoder(name string, enc zapcore.PrimitiveArrayEncoder) {
buf := _bufferPoolGet()
colorize(buf, name, strconv.Itoa(colorBold))
enc.AppendString(buf.String())
buf.Free()
}
var reflectedListBreakSize = map[interface{}]int{
new(byte): 16, reflect.TypeOf(*new(byte)): 16,
new(bool): 8, *new(bool): 8,
new(complex64): 8, *new(complex64): 8,
new(time.Duration): 8, *new(time.Duration): 8,
new(float32): 8, *new(float32): 8,
new(float64): 8, *new(float64): 8,
new(int): 8, *new(int): 8,
new(int16): 8, *new(int16): 8,
new(int32): 8, *new(int32): 8,
new(int64): 8, *new(int64): 8,
new(int16): 16, *new(int16): 16,
new(string): 8, *new(string): 8,
new(time.Time): 8, *new(time.Time): 8,
new(uint): 8, *new(uint): 8,
new(uint16): 8, *new(uint16): 8,
new(uint32): 8, *new(uint32): 8,
new(uint64): 8, *new(uint64): 8,
new(uint16): 16, *new(uint16): 16,
}
func defaultReflectedEncoder(w io.Writer) zapcore.ReflectedEncoder {
opts := make([]dd.OptionFunc, 0, len(reflectedListBreakSize))
for key, val := range reflectedListBreakSize {
opts = append(opts, dd.WithListBreakLineSize(key, val))
}
return ddEncoder{w: w, opts: opts}
}
type ddEncoder struct {
w io.Writer
opts []dd.OptionFunc
}
func (d ddEncoder) Encode(i interface{}) error {
_, err := d.w.Write([]byte(dd.Dump(i, d.opts...)))
return err
}
// colorize returns the string s wrapped in ANSI code c
func colorize(buf *buffer.Buffer, s string, cols ...string) {
for _, col := range cols {
buf.AppendString("\x1b[" + col + "m")
}
buf.AppendString(s)
buf.AppendString("\x1b[0m")
}