-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlogger.go
126 lines (100 loc) · 2.69 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
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
package logger
import (
"context"
"github.com/spf13/pflag"
"github.com/lukasjarosch/genki/config"
)
// Global log instance to be able to directly access the log functions
var log Logger
const DefaultLevel = InfoLevel
type Logger interface {
Debug(fields ...interface{})
Debugf(format string, args ...interface{})
Info(fields ...interface{})
Infof(format string, args ...interface{})
Warn(fields ...interface{})
Warnf(format string, args ...interface{})
Error(fields ...interface{})
Errorf(format string, args ...interface{})
Fatal(fields ...interface{})
Fatalf(format string, args ...interface{})
WithFields(keyValues Fields) Logger
WithMetadata(ctx context.Context) Logger
}
type Fields map[string]interface{}
const (
DebugLevel = "debug"
InfoLevel = "info"
WarnLevel = "warn"
ErrorLevel = "error"
FatalLevel = "fatal"
DefaultCallerSkip = 2
LogLevelConfigKey = "log-level"
)
func NewLogger(level string) error {
if level == "" {
level = DefaultLevel
}
logger, err := newZapLogger(level, DefaultCallerSkip)
if err != nil {
return err
}
log = logger
return nil
}
func Debug(fields ...interface{}) {
log.Debug(fields...)
}
func Debugf(format string, args ...interface{}) {
log.Debugf(format, args...)
}
func Info(fields ...interface{}) {
log.Info(fields...)
}
func Infof(format string, args ...interface{}) {
log.Infof(format, args...)
}
func Warn(fields ...interface{}) {
log.Warn(fields...)
}
func Warnf(format string, args ...interface{}) {
log.Warnf(format, args...)
}
func Error(fields ...interface{}) {
log.Error(fields...)
}
func Errorf(format string, args ...interface{}) {
log.Errorf(format, args...)
}
func Fatal(fields ...interface{}) {
log.Fatal(fields...)
}
func Fatalf(format string, args ...interface{}) {
log.Fatalf(format, args...)
}
func WithFields(keyValues Fields) Logger {
return log.WithFields(keyValues)
}
func WithMetadata(ctx context.Context) Logger {
return log.WithMetadata(ctx)
}
// Flags is a convenience function to quickly add the log options as CLI flags
// Implements the cli.FlagProvider type
func Flags() *pflag.FlagSet {
fs := pflag.NewFlagSet("logger", pflag.ContinueOnError)
fs.String(
LogLevelConfigKey,
DefaultLevel,
"log level defines the lowest level of logs printed (debug, info, warn, error, fatal)",
)
return fs
}
// EnsureLoggerFromConfig is a convenience function to quickly create a new logger from
// the configuration.
// This requires that the configuration has already be bound from the flags.
// The function will FATAL if the logger could not be created.
func EnsureLoggerFromConfig() {
if err := NewLogger(config.GetString(LogLevelConfigKey)); err != nil {
log.Fatal(err.Error())
}
}