/
main.go
176 lines (156 loc) · 4.81 KB
/
main.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package main
import (
"flag"
"fmt"
"io"
"net/http"
_ "net/http/pprof"
"os"
ospath "path"
"time"
rolling "github.com/arthurkiller/rollingWriter"
"github.com/shafreeck/configo"
"github.com/shafreeck/continuous"
"github.com/sirupsen/logrus"
"github.com/tipsio/tips"
"github.com/tipsio/tips/conf"
"github.com/tipsio/tips/metrics"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
func main() {
var confPath string
var pdAddrs string
flag.StringVar(&confPath, "c", "conf/tips.toml", "conf file path")
flag.StringVar(&pdAddrs, "pd-addrs", "", "pd cluster addresses")
flag.Parse()
config := &conf.Tips{}
if err := configo.Load(confPath, config); err != nil {
fmt.Printf("unmarshal config file failed, %s\n", err)
os.Exit(1)
}
if pdAddrs != "" {
config.Server.Tikv.PdAddrs = pdAddrs
}
if err := ConfigureZap(config.Logger.Name, config.Logger.Path, config.Logger.Level,
config.Logger.TimeRotate, config.Logger.Compress); err != nil {
fmt.Printf("create logger failed, %s\n", err)
os.Exit(1)
}
if err := ConfigureLogrus(config.TikvLog.Path, config.TikvLog.Level,
config.TikvLog.TimeRotate, config.TikvLog.Compress); err != nil {
fmt.Printf("create tikv logger failed, %s\n", err)
os.Exit(1)
}
tips, err := tips.NewTips(config.Server.Tikv.PdAddrs)
if err != nil {
zap.L().Fatal("open db failed", zap.Error(err))
os.Exit(1)
}
serv := NewServer(&config.Server, tips)
svr := metrics.NewServer(&config.Status)
writer, err := Writer(config.Logger.Path, config.Logger.TimeRotate, config.Logger.Compress)
if err != nil {
zap.L().Fatal("create writer for continuous failed", zap.Error(err))
}
cont := continuous.New(continuous.LoggerOutput(writer), continuous.PidFile(config.PIDFileName))
if err := cont.AddServer(serv, &continuous.ListenOn{Network: "tcp", Address: config.Server.Listen}); err != nil {
zap.L().Fatal("add tips server failed:", zap.Error(err))
}
if err := cont.AddServer(svr, &continuous.ListenOn{Network: "tcp", Address: config.Status.Listen}); err != nil {
zap.L().Fatal("add statues server failed:", zap.Error(err))
}
if err := cont.Serve(); err != nil {
zap.L().Fatal("run server failed:", zap.Error(err))
}
}
// ConfigureZap customize the zap logger
func ConfigureZap(name, path, level, pattern string, compress bool) error {
writer, err := Writer(path, pattern, compress)
if err != nil {
return err
}
var lv = zap.NewAtomicLevel()
switch level {
case "debug":
lv.SetLevel(zap.DebugLevel)
case "info":
lv.SetLevel(zap.InfoLevel)
case "warn":
lv.SetLevel(zap.WarnLevel)
case "error":
lv.SetLevel(zap.ErrorLevel)
case "panic":
lv.SetLevel(zap.PanicLevel)
case "fatal":
lv.SetLevel(zap.FatalLevel)
default:
return fmt.Errorf("unknown log level(%s)", level)
}
timeEncoder := func(t time.Time, enc zapcore.PrimitiveArrayEncoder) {
enc.AppendString(t.Local().Format("2006-01-02 15:04:05.999999999"))
}
encoderCfg := zapcore.EncoderConfig{
NameKey: "Name",
StacktraceKey: "Stack",
MessageKey: "Message",
LevelKey: "Level",
TimeKey: "TimeStamp",
CallerKey: "Caller",
EncodeTime: timeEncoder,
EncodeLevel: zapcore.CapitalLevelEncoder,
EncodeDuration: zapcore.StringDurationEncoder,
EncodeCaller: zapcore.ShortCallerEncoder,
}
output := zapcore.AddSync(writer)
var zapOpts []zap.Option
zapOpts = append(zapOpts, zap.AddCaller())
// zapOpts = append(zapOpts, zap.Hooks(metrics.Measure))
logger := zap.New(zapcore.NewCore(zapcore.NewJSONEncoder(encoderCfg), output, lv), zapOpts...)
logger.Named(name)
log := logger.With(zap.Int("PID", os.Getpid()))
zap.ReplaceGlobals(log)
//http change log level
http.Handle("/tips/log/level", lv)
return nil
}
// ConfigureLogrus customize the logrus logger, which is used by TiKV SDK
func ConfigureLogrus(path, level, pattern string, compress bool) error {
writer, err := Writer(path, pattern, compress)
if err != nil {
return err
}
logrus.SetOutput(writer)
switch level {
case "debug":
logrus.SetLevel(logrus.DebugLevel)
case "info":
logrus.SetLevel(logrus.InfoLevel)
case "warn":
logrus.SetLevel(logrus.WarnLevel)
case "error":
logrus.SetLevel(logrus.ErrorLevel)
case "panic":
logrus.SetLevel(logrus.PanicLevel)
case "fatal":
logrus.SetLevel(logrus.FatalLevel)
default:
return fmt.Errorf("unknown log level(%s)", level)
}
return nil
}
//Writer generate the rollingWriter
func Writer(path, pattern string, compress bool) (io.Writer, error) {
var opts []rolling.Option
opts = append(opts, rolling.WithRollingTimePattern(pattern))
if compress {
opts = append(opts, rolling.WithCompress())
}
dir, filename := ospath.Split(path)
opts = append(opts, rolling.WithLogPath(dir), rolling.WithFileName(filename), rolling.WithLock())
writer, err := rolling.NewWriter(opts...)
if err != nil {
return nil, fmt.Errorf("create IOWriter failed, %s", err)
}
return writer, nil
}