/
factory.go
178 lines (152 loc) · 4.38 KB
/
factory.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
// +build !windows
package klog
import (
"fmt"
"github.com/enfabrica/enkit/lib/kflags"
"github.com/tchap/zapext/zapsyslog"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"io"
"log/syslog"
"os"
"strings"
)
type Logger struct {
*zap.SugaredLogger
}
func (l *Logger) SetOutput(writer io.Writer) {
}
func Syslog(tag string, sl syslog.Priority, zl zapcore.Level) (zapcore.Core, error) {
writer, err := syslog.New(sl|syslog.LOG_USER, tag)
if err != nil {
return nil, fmt.Errorf("could not initialize syslog - %w", err)
}
encoder := zapcore.NewJSONEncoder(zap.NewProductionEncoderConfig())
return zapsyslog.NewCore(zap.LevelEnablerFunc(func(lvl zapcore.Level) bool { return lvl == zl }), encoder, writer), nil
}
type Flags struct {
ConsoleLevel string
SyslogLevel string
Verbosity int
Quiet bool
}
func DefaultFlags() *Flags {
return &Flags{
ConsoleLevel: "warn",
SyslogLevel: "info",
Verbosity: 0,
}
}
func (cf *Flags) Register(flags kflags.FlagSet, prefix string) *Flags {
flags.StringVar(&cf.ConsoleLevel, prefix+"loglevel-console", cf.ConsoleLevel, "Can be debug, info, warn, error. Indicates the minimum severity of messages to log on the console")
flags.StringVar(&cf.SyslogLevel, prefix+"loglevel-syslog", cf.SyslogLevel, "Can be debug, info, warn, error. Indicates the minimum severity of messages to log in syslog")
flags.IntVar(&cf.Verbosity, prefix+"verbosity", cf.Verbosity, "Increases the verbosity level of logs by the specified amount")
flags.BoolVar(&cf.Quiet, prefix+"quiet", cf.Quiet, "If set to true, only errors will be logged on the console")
return cf
}
type options struct {
minConsole zapcore.Level
minSyslog zapcore.Level
}
type Modifier func(o *options) error
type Modifiers []Modifier
func (mods Modifiers) Apply(o *options) error {
for _, m := range mods {
if err := m(o); err != nil {
return err
}
}
return nil
}
type Level struct {
Name string
Value zapcore.Level
}
type Levels []Level
func (levels Levels) Find(name string) (int, *Level) {
name = strings.TrimSpace(strings.ToLower(name))
for ix, level := range levels {
if strings.HasPrefix(level.Name, name) {
return ix, &levels[ix]
}
}
return 0, nil
}
func (levels Levels) String() string {
keys := []string{}
for _, key := range levels {
keys = append(keys, key.Name)
}
return "[" + strings.Join(keys, ", ") + "]"
}
var DefaultLevels = Levels{
{"debug", zapcore.DebugLevel},
{"info", zapcore.InfoLevel},
{"warning", zapcore.WarnLevel},
{"error", zapcore.ErrorLevel},
}
func max(a, b int) int {
if a > b {
return a
}
return b
}
func FromFlags(flags Flags) Modifier {
return func(o *options) error {
cx, cl := DefaultLevels.Find(flags.ConsoleLevel)
if cl == nil {
return kflags.NewUsageErrorf("invalid --loglevel-console passed - %s is unknown, valid: %s", flags.ConsoleLevel, DefaultLevels)
}
sx, sl := DefaultLevels.Find(flags.SyslogLevel)
if sl == nil {
return kflags.NewUsageErrorf("invalid --loglevel-syslog passed - %s is unknown, valid: %s", flags.ConsoleLevel, DefaultLevels)
}
cx = max(0, cx-flags.Verbosity)
sx = max(0, sx-flags.Verbosity)
o.minConsole = DefaultLevels[cx].Value
o.minSyslog = DefaultLevels[sx].Value
if flags.Quiet {
o.minConsole = zapcore.ErrorLevel
}
return nil
}
}
func New(name string, mods ...Modifier) (*Logger, error) {
options := &options{
minConsole: zap.WarnLevel,
minSyslog: zap.InfoLevel,
}
if err := Modifiers(mods).Apply(options); err != nil {
return nil, err
}
matchConsole := zap.LevelEnablerFunc(func(lvl zapcore.Level) bool {
return lvl >= options.minConsole
})
matchSyslog := zap.LevelEnablerFunc(func(lvl zapcore.Level) bool {
return lvl >= options.minSyslog
})
console := zapcore.NewConsoleEncoder(zap.NewDevelopmentEncoderConfig())
tees := []zapcore.Core{
zapcore.NewCore(console, zapcore.Lock(os.Stderr), matchConsole),
}
for _, level := range []struct {
Syslog syslog.Priority
Zap zapcore.Level
}{
{Syslog: syslog.LOG_INFO, Zap: zapcore.InfoLevel},
{Syslog: syslog.LOG_ERR, Zap: zapcore.ErrorLevel},
{Syslog: syslog.LOG_WARNING, Zap: zapcore.WarnLevel},
{Syslog: syslog.LOG_DEBUG, Zap: zapcore.DebugLevel},
} {
if !matchSyslog(level.Zap) {
continue
}
logger, err := Syslog(name, level.Syslog, level.Zap)
if err != nil {
continue
}
tees = append(tees, logger)
}
logger := zap.New(zapcore.NewTee(tees...)).Sugar()
return &Logger{logger}, nil
}