-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.go
49 lines (41 loc) · 1.13 KB
/
logger.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
package main
import (
"fmt"
"github.com/go-logr/logr"
"github.com/go-logr/zapr"
"github.com/spf13/cobra"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
var setupLogger logr.Logger = logr.Discard()
func init() {
zapConfig := zap.NewDevelopmentConfig()
zapConfig.Level = zap.NewAtomicLevelAt(zapcore.Level(-12))
zapLog, err := zapConfig.Build()
if err != nil {
panic(err)
}
setupLogger = zapr.NewLogger(zapLog).WithName("setup")
}
func createLogger(cmd *cobra.Command) (logr.Logger, error) {
logLevel, err := cmd.Flags().GetInt8(cliFlagLogLevel)
if err != nil {
return logr.Discard(), fmt.Errorf("read %s: %w", cliFlagLogLevel, err)
}
logDevel, err := cmd.Flags().GetBool(cliFlagLogDevel)
if err != nil {
return logr.Discard(), fmt.Errorf("read %s: %w", cliFlagLogDevel, err)
}
var zapConfig zap.Config
if logDevel {
zapConfig = zap.NewDevelopmentConfig()
} else {
zapConfig = zap.NewProductionConfig()
}
zapConfig.Level = zap.NewAtomicLevelAt(zapcore.Level(-logLevel))
zapLog, err := zapConfig.Build()
if err != nil {
return logr.Discard(), fmt.Errorf("create logger: %w", err)
}
return zapr.NewLogger(zapLog), nil
}