-
Notifications
You must be signed in to change notification settings - Fork 7
/
configuration.go
114 lines (103 loc) · 2.83 KB
/
configuration.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
package log
import (
"fmt"
"os"
"strconv"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
// Init initializes the global logger with proided level.
//
// Default level is zapcore.InfoLevel and non-development.
func Init(c *Configuration) {
if c == nil {
c = &Configuration{}
}
encoder := zapcore.EncoderConfig{
TimeKey: "@timestamp",
LevelKey: "level",
NameKey: "logger",
CallerKey: "caller",
MessageKey: "message",
StacktraceKey: "stacktrace",
LineEnding: zapcore.DefaultLineEnding,
EncodeLevel: zapcore.LowercaseLevelEncoder,
EncodeTime: zapcore.ISO8601TimeEncoder,
EncodeDuration: zapcore.StringDurationEncoder,
EncodeCaller: zapcore.ShortCallerEncoder,
}
config := zap.Config{
Level: zap.NewAtomicLevelAt(c.Level.Level),
Development: false,
Encoding: "json",
EncoderConfig: encoder,
OutputPaths: []string{"stderr"},
ErrorOutputPaths: []string{"stderr"},
}
if c.Development {
config.Development = true
config.Encoding = "console"
config.EncoderConfig.TimeKey = ""
}
zapLogger, _ := config.Build(zap.AddCallerSkip(2))
logger = &Logger{sugar: zapLogger.Sugar()}
}
// Configuration represents a log configuration.
type Configuration struct {
Level Level
Development bool
}
// ParseFromEnvironmnet parses configuration from the environment and writes
// found values to configuration struct c.
func (c *Configuration) ParseFromEnvironmnet() {
if c == nil {
c = &Configuration{}
}
parseEnvLevel(c)
parseEnvDevelopment(c)
}
func parseEnvLevel(c *Configuration) {
l, ok := os.LookupEnv("LOG_LEVEL")
if !ok {
return
}
var level zapcore.Level
err := level.Set(l)
if err != nil {
fmt.Printf("internal/log: failed to parse LOG_LEVEL: %v\n", err)
return
}
c.Level = Level{
Level: level,
}
}
func parseEnvDevelopment(c *Configuration) {
d, ok := os.LookupEnv("LOG_DEVELOPMENT")
if !ok {
return
}
development, err := strconv.ParseBool(d)
if err != nil {
fmt.Printf("internal/log: failed to parse LOG_DEVELOPMENT '%s' as bool\n", d)
return
}
c.Development = development
}
// RegisterFlags registers logging configuration flags on command cmd and
// returns pointers to the values.
func RegisterFlags(cmd *cobra.Command) *Configuration {
var c Configuration
cmd.PersistentFlags().Var(&c.Level, "log.level", "configure log level. Available values are \"debug\", \"info\", \"error\" (fallback to LOG_LEVEL)")
cmd.PersistentFlags().BoolVar(&c.Development, "log.development", false, "configure log for development with human readable output (fallback to LOG_DEVELOPMENT)")
return &c
}
var _ pflag.Value = &Level{}
// Level is a wrapped zapcore.Level that implements the pflag.Value interface.
type Level struct {
zapcore.Level
}
func (*Level) Type() string {
return "string"
}