-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
85 lines (74 loc) · 1.5 KB
/
config.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
package log
import (
"fmt"
"go.uber.org/zap"
)
type Config struct {
Development bool
Format Format
Level Level
StackTrace bool
Caller bool
}
type Format string
const (
FormatJSON Format = "json"
FormatText Format = "text"
)
func (f Format) Validate() error {
switch f {
case FormatJSON, FormatText:
return nil
default:
return fmt.Errorf("invalid log format '%s'", f)
}
}
func (f Format) ZapEncoding() string {
var zf string
switch f {
case FormatJSON:
zf = "json"
case FormatText:
fallthrough
default:
zf = "console"
}
return zf
}
type Level string
const (
LevelDebug Level = "debug"
LevelInfo Level = "info"
LevelWarn Level = "warn"
LevelError Level = "error"
LevelPanic Level = "panic"
LevelFatal Level = "fatal"
)
func (l Level) Validate() error {
switch l {
case LevelDebug, LevelInfo, LevelWarn, LevelError, LevelPanic, LevelFatal:
return nil
default:
return fmt.Errorf("invalid log level '%s'", l)
}
}
func (l Level) ZapLevel() zap.AtomicLevel {
var zlevel zap.AtomicLevel
switch l {
case LevelDebug:
zlevel = zap.NewAtomicLevelAt(zap.DebugLevel)
case LevelInfo:
zlevel = zap.NewAtomicLevelAt(zap.InfoLevel)
case LevelWarn:
zlevel = zap.NewAtomicLevelAt(zap.WarnLevel)
case LevelError:
zlevel = zap.NewAtomicLevelAt(zap.ErrorLevel)
case LevelPanic:
zlevel = zap.NewAtomicLevelAt(zap.PanicLevel)
case LevelFatal:
zlevel = zap.NewAtomicLevelAt(zap.FatalLevel)
default:
zlevel = zap.NewAtomicLevelAt(zap.InfoLevel)
}
return zlevel
}