forked from VolantMQ/volantmq
/
init.go
72 lines (56 loc) · 1.43 KB
/
init.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
package configuration
import (
"sync"
"go.uber.org/zap"
)
type config struct {
log struct {
Prod *zap.Logger
Dev *zap.Logger
}
once sync.Once
}
// Options global MQTT config
type Options struct {
// LogWithTs either display timestamp messages on log or not
LogWithTs bool
}
var cfg config
func init() {
logCfg := zap.NewProductionConfig()
logDebugCfg := zap.NewProductionConfig()
logCfg.DisableStacktrace = true
logDebugCfg.DisableStacktrace = true
logDebugCfg.Level = zap.NewAtomicLevelAt(zap.DebugLevel)
log, _ := logCfg.Build()
dLog, _ := logDebugCfg.Build()
cfg.log.Prod = log.Named("mqtt")
cfg.log.Dev = dLog.Named("mqtt")
}
// Init global MQTT config with given options
// if not being called default set by init() is used
func Init(ops Options) {
cfg.once.Do(func() {
logCfg := zap.NewProductionConfig()
logDebugCfg := zap.NewDevelopmentConfig()
logDebugCfg.Level = zap.NewAtomicLevelAt(zap.InfoLevel)
logCfg.DisableStacktrace = true
logDebugCfg.DisableStacktrace = true
if !ops.LogWithTs {
logCfg.EncoderConfig.TimeKey = ""
logDebugCfg.EncoderConfig.TimeKey = ""
}
log, _ := logCfg.Build()
dLog, _ := logDebugCfg.Build()
cfg.log.Prod = log.Named("mqtt")
cfg.log.Dev = dLog.Named("mqtt")
})
}
// GetProdLogger return production logger
func GetProdLogger() *zap.Logger {
return cfg.log.Prod
}
// GetDevLogger return development logger
func GetDevLogger() *zap.Logger {
return cfg.log.Prod
}