forked from kubermatic/kubermatic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zap.go
152 lines (123 loc) · 3.51 KB
/
zap.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
/*
Copyright 2020 The Kubermatic Kubernetes Platform contributors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package log
import (
"flag"
"fmt"
"os"
"strings"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
ctrlruntimelzap "sigs.k8s.io/controller-runtime/pkg/log/zap"
)
func init() {
Logger = NewDefault().Sugar()
}
var Logger *zap.SugaredLogger
// Options exports a options struct to be used by cmd's
type Options struct {
// Enable debug logs
Debug bool
// Log format (JSON or plain text)
Format Format
}
func NewDefaultOptions() Options {
return Options{
Debug: false,
Format: FormatJSON,
}
}
func (o *Options) AddFlags(fs *flag.FlagSet) {
fs.BoolVar(&o.Debug, "log-debug", o.Debug, "Enables debug logging")
fs.Var(&o.Format, "log-format", "Log format, one of "+AvailableFormats.String())
}
func (o *Options) Validate() error {
if !AvailableFormats.Contains(o.Format) {
return fmt.Errorf("invalid log-format specified %q; available: %s", o.Format, AvailableFormats.String())
}
return nil
}
type Format string
// String implements the cli.Value and flag.Value interfaces
func (f *Format) String() string {
return string(*f)
}
// Set implements the cli.Value and flag.Value interfaces
func (f *Format) Set(s string) error {
switch strings.ToLower(s) {
case "json":
*f = FormatJSON
return nil
case "console":
*f = FormatConsole
return nil
default:
return fmt.Errorf("invalid format '%s'", s)
}
}
type Formats []Format
const (
FormatJSON Format = "JSON"
FormatConsole Format = "Console"
)
var (
AvailableFormats = Formats{FormatJSON, FormatConsole}
)
func (f Formats) String() string {
const separator = ", "
var s string
for _, format := range f {
s = s + separator + string(format)
}
return strings.TrimPrefix(s, separator)
}
func (f Formats) Contains(s Format) bool {
for _, format := range f {
if s == format {
return true
}
}
return false
}
func New(debug bool, format Format) *zap.Logger {
// this basically mimics New<type>Config, but with a custom sink
sink := zapcore.AddSync(os.Stderr)
// Level - We only support setting Info+ or Debug+
lvl := zap.NewAtomicLevelAt(zap.InfoLevel)
if debug {
lvl = zap.NewAtomicLevelAt(zap.DebugLevel)
}
encCfg := zap.NewProductionEncoderConfig()
// Having a dateformat makes it more easy to look at logs outside of something like Kibana
encCfg.TimeKey = "time"
encCfg.EncodeTime = zapcore.ISO8601TimeEncoder
// production config encodes durations as a float of the seconds value, but we want a more
// readable, precise representation
encCfg.EncodeDuration = zapcore.StringDurationEncoder
var enc zapcore.Encoder
if format == FormatJSON {
enc = zapcore.NewJSONEncoder(encCfg)
} else if format == FormatConsole {
enc = zapcore.NewConsoleEncoder(encCfg)
}
opts := []zap.Option{
zap.AddCaller(),
zap.ErrorOutput(sink),
}
coreLog := zapcore.NewCore(&ctrlruntimelzap.KubeAwareEncoder{Encoder: enc}, sink, lvl)
return zap.New(coreLog, opts...)
}
// NewDefault creates new default logger
func NewDefault() *zap.Logger {
return New(false, FormatJSON)
}