/
init.go
160 lines (127 loc) · 3.84 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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package configuration
import (
"flag"
"os"
"path/filepath"
"sync"
"time"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
type config struct {
//log *zap.SugaredLogger
humanLog *zap.SugaredLogger
once sync.Once
}
// Options global MQTT config
type Options struct {
// LogWithTs either display timestamp messages on log or not
LogWithTs bool
}
var cfg config
var configFile string
// WorkDir absolute path to service working directory
var WorkDir string
// PluginsDir absolute path to service plugins directory
var PluginsDir string
func init() {
// initialize startup logger
logCfg := zap.NewProductionConfig()
logCfg.DisableStacktrace = true
logCfg.Level = zap.NewAtomicLevelAt(zap.DebugLevel)
logCfg.EncoderConfig.LevelKey = ""
logCfg.EncoderConfig.CallerKey = ""
logCfg.Encoding = "console"
logCfg.EncoderConfig.EncodeTime = func(t time.Time, encoder zapcore.PrimitiveArrayEncoder) {
encoder.AppendString(t.Format(time.RFC3339))
}
log, _ := logCfg.Build()
cfg.humanLog = log.Sugar()
WorkDir = "/var/lib/volantmq"
PluginsDir = WorkDir + "/plugins"
configFile, _ = os.LookupEnv("VOLANTMQ_CONFIG")
if str, ok := os.LookupEnv("VOLANTMQ_WORK_DIR"); ok {
WorkDir = str
}
if str, ok := os.LookupEnv("VOLANTMQ_PLUGINS_DIR"); ok {
PluginsDir = str
} else {
PluginsDir = WorkDir + "/plugins"
}
flag.StringVar(&configFile, "config", configFile, "config file")
flag.StringVar(&WorkDir, "work-dir", WorkDir, "service work directory")
flag.StringVar(&PluginsDir, "plugins-dir", PluginsDir, "service plugins directory")
var err error
WorkDir, err = filepath.Abs(WorkDir)
if err != nil {
panic(err.Error())
}
PluginsDir, err = filepath.Abs(PluginsDir)
if err != nil {
panic(err.Error())
}
}
// GetLogger return production logger
func GetLogger() *zap.SugaredLogger {
//return cfg.log
return cfg.humanLog
}
// GetHumanLogger return production logger
func GetHumanLogger() *zap.SugaredLogger {
return cfg.humanLog
}
var configTimeFormatMap = map[string]string{
"ANSIC": time.ANSIC,
"UNIX": time.UnixDate,
"RubyDate": time.RubyDate,
"RFC822": time.RFC822,
"RFC822Z": time.RFC822Z,
"RFC850": time.RFC850,
"RFC1123": time.RFC1123,
"RFC1123Z": time.RFC1123Z,
"RFC3339": time.RFC3339,
"RFC3339Nano": time.RFC3339Nano,
}
func ConfigureLoggers(c *LogConfig) error {
// initialize startup logger
//logCfg := zap.NewProductionConfig()
logCfg := zap.NewDevelopmentEncoderConfig()
var level zapcore.Level
if err := level.UnmarshalText([]byte(c.Console.Level)); err != nil {
return err
}
if c.Console.Timestamp != nil {
if f, ok := configTimeFormatMap[c.Console.Timestamp.Format]; !ok {
GetLogger().Warn("unsupported time format supplied by config. using RFCC3339")
c.Console.Timestamp.Format = time.RFC3339
} else {
c.Console.Timestamp.Format = f
}
logCfg.EncodeTime = func(t time.Time, enc zapcore.PrimitiveArrayEncoder) {
enc.AppendString(t.Format(c.Console.Timestamp.Format))
}
} else {
logCfg.EncodeTime = nil
}
logCfg.EncodeLevel = zapcore.CapitalColorLevelEncoder
logCfg.StacktraceKey = ""
consoleEncoder := zapcore.NewConsoleEncoder(logCfg)
//logger, _ = logCfg.Build()
// High-priority output should also go to standard error, and low-priority
// output should also go to standard out.
consoleDebugging := zapcore.Lock(os.Stdout)
consoleErrors := zapcore.Lock(os.Stderr)
// First, define our level-handling logic.
highPriority := zap.LevelEnablerFunc(func(lvl zapcore.Level) bool {
return lvl >= zapcore.ErrorLevel
})
lowPriority := zap.LevelEnablerFunc(func(lvl zapcore.Level) bool {
return lvl < zapcore.ErrorLevel
})
core := zapcore.NewTee(
zapcore.NewCore(consoleEncoder, consoleErrors, highPriority),
zapcore.NewCore(consoleEncoder, consoleDebugging, lowPriority))
cfg.humanLog = zap.New(core).Sugar()
cfg.humanLog.Sync()
return nil
}