This repository has been archived by the owner on Jun 5, 2019. It is now read-only.
/
gomol.go
188 lines (152 loc) · 3.62 KB
/
gomol.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
package logging
import (
"fmt"
"os"
"strings"
"text/template"
"github.com/aphistic/gomol"
console "github.com/aphistic/gomol-console"
)
type GomolShim struct {
logger gomol.WrappableLogger
}
var gomolLevels = map[LogLevel]gomol.LogLevel{
LevelDebug: gomol.LevelDebug,
LevelInfo: gomol.LevelInfo,
LevelWarning: gomol.LevelWarning,
LevelError: gomol.LevelError,
LevelFatal: gomol.LevelFatal,
}
//
// Shim
func NewGomolLogger(logger *gomol.LogAdapter, initialFields Fields) Logger {
return adaptShim((&GomolShim{logger}).WithFields(initialFields))
}
func (g *GomolShim) WithFields(fields Fields) logShim {
if len(fields) == 0 {
return g
}
return &GomolShim{gomol.NewLogAdapterFor(g.logger, gomol.NewAttrsFromMap(fields))}
}
func (g *GomolShim) LogWithFields(level LogLevel, fields Fields, format string, args ...interface{}) {
g.logger.Log(gomolLevels[level], gomol.NewAttrsFromMap(addCaller(fields).normalizeTimeValues()), format, args...)
if level == LevelFatal {
g.logger.ShutdownLoggers()
os.Exit(1)
}
}
func (g *GomolShim) Sync() error {
return gomol.ShutdownLoggers()
}
//
// Init
func InitGomolShim(c *Config) (Logger, error) {
level, err := gomol.ToLogLevel(c.LogLevel)
if err != nil {
return nil, err
}
if err := gomol.ClearLoggers(); err != nil {
return nil, err
}
gomol.SetLogLevel(level)
gomol.SetConfig(&gomol.Config{SequenceAttr: "sequence_number"})
switch c.LogEncoding {
case "console":
if err := setupConsoleLogger(c); err != nil {
return nil, err
}
case "json":
if err := setupJSONLogger(c); err != nil {
return nil, err
}
}
if err := gomol.InitLoggers(); err != nil {
return nil, err
}
return NewGomolLogger(gomol.NewLogAdapter(nil), c.LogInitialFields), nil
}
func setupConsoleLogger(c *Config) error {
consoleLogger, err := console.NewConsoleLogger(&console.ConsoleLoggerConfig{
Colorize: true,
Writer: os.Stderr,
})
if err != nil {
return err
}
tpl, err := newGomolConsoleTemplate(
c.LogColorize,
c.LogShortTime,
c.LogDisplayFields,
c.LogDisplayMultilineFields,
c.LogFieldBlacklist,
)
if err != nil {
return err
}
consoleLogger.SetTemplate(tpl)
gomol.AddLogger(consoleLogger)
return nil
}
func setupJSONLogger(c *Config) error {
gomol.AddLogger(newJSONLogger(c.LogJSONFieldNames))
return nil
}
func newGomolConsoleTemplate(color, shortTime, displayFields, displayMultilineFields bool, blacklist []string) (*gomol.Template, error) {
var (
attrPrefix = " "
attrPadding = ""
attrSuffix = ""
)
if displayMultilineFields {
attrPrefix = "\n "
attrPadding = " "
attrSuffix = "\n"
}
fieldsTemplate := fmt.Sprintf(
""+
`{{if .Attrs}}`+
`{{range $key, $val := .Attrs}}`+
`{{if shouldDisplayAttr $key}}`+
`%s{{$key}}%s=%s{{$val}}`+
`{{end}}`+
`{{end}}`+
`%s`+
`{{end}}`,
attrPrefix,
attrPadding,
attrPadding,
attrSuffix,
)
timeFormat := "2006/01/02 15:04:05.000"
if shortTime {
timeFormat = "15:04:05"
}
text :=
"" +
`{{color}}` +
`[{{ucase .LevelName | printf "%1.1s"}}] ` +
fmt.Sprintf(`[{{.Timestamp.Format "%s"}}] {{.Message}}`, timeFormat) +
`{{reset}}`
if displayFields {
text += fieldsTemplate
}
if !color {
text = removeColor(text)
}
return gomol.NewTemplateWithFuncMap(text, template.FuncMap{
"shouldDisplayAttr": shouldDisplayAttr(blacklist),
})
}
func removeColor(text string) string {
return strings.NewReplacer("{{color}}", "", "{{reset}}", "").Replace(text)
}
func shouldDisplayAttr(blacklist []string) func(string) bool {
return func(attr string) bool {
for _, cmp := range blacklist {
if cmp == attr {
return false
}
}
return true
}
}