/
logging.go
104 lines (88 loc) · 2.28 KB
/
logging.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
package clib
import (
"fmt"
"os"
"time"
"github.com/spf13/cobra"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
// LoggingMode represents a logging configuration specification.
type LoggingMode int
// LoggingMode values
const (
LoggingNop LoggingMode = iota
LoggingVerbose
LoggingDebug
)
var (
logging = LoggingNop
// DebugLogConfig is used to generate a *zap.Logger for debug mode.
DebugLogConfig = func() zap.Config {
cfg := zap.NewProductionConfig()
cfg.Level = zap.NewAtomicLevelAt(zapcore.DebugLevel)
cfg.DisableStacktrace = true
return cfg
}()
// VerboseLogConfig is used to generate a *zap.Logger for verbose mode.
VerboseLogConfig = func() zap.Config {
cfg := zap.NewDevelopmentConfig()
cfg.EncoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder
cfg.Level = zap.NewAtomicLevelAt(zapcore.InfoLevel)
cfg.EncoderConfig.EncodeTime = func(t time.Time, enc zapcore.PrimitiveArrayEncoder) {
enc.AppendString(t.Local().Format("2006-01-02 15:04:05 MST"))
}
return cfg
}()
)
// AddLoggingFlags sets "--debug" and "--verbose" flags to the given *cobra.Command instance.
func AddLoggingFlags(cmd *cobra.Command) {
var (
debugEnabled, verboseEnabled bool
)
cmd.PersistentFlags().BoolVar(
&debugEnabled,
"debug",
false,
fmt.Sprintf("Debug level output"),
)
cmd.PersistentFlags().BoolVarP(
&verboseEnabled,
"verbose",
"v",
false,
fmt.Sprintf("Verbose level output"),
)
cobra.OnInitialize(func() {
switch {
case debugEnabled:
Debug()
case verboseEnabled:
Verbose()
}
})
}
// Debug sets a debug logger in global.
func Debug() {
logging = LoggingDebug
replaceLogger(DebugLogConfig)
}
// Verbose sets a verbose logger in global.
func Verbose() {
logging = LoggingVerbose
replaceLogger(VerboseLogConfig)
}
// IsDebug returns true if a debug logger is used.
func IsDebug() bool { return logging == LoggingDebug }
// IsVerbose returns true if a verbose logger is used.
func IsVerbose() bool { return logging == LoggingVerbose }
// Logging returns a current logging mode.
func Logging() LoggingMode { return logging }
func replaceLogger(cfg zap.Config) {
l, err := cfg.Build()
if err != nil {
fmt.Fprintf(os.Stderr, "failed to initialize a debug logger: %v\n", err)
}
addCloseFunc(func() { l.Sync() })
addCloseFunc(zap.ReplaceGlobals(l))
}