-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
319 lines (267 loc) · 7.47 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
package main
import (
"context"
"flag"
"fmt"
"io"
"net/http"
"os"
"syscall"
"time"
"github.com/gin-gonic/gin"
"github.com/natefinch/lumberjack"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
const server = "log_dotter"
type Dotter struct {
ctx context.Context
cancel context.CancelFunc
t <-chan time.Time
Timeout uint64 `json:"timeout"` // set log cron timeout (minute)
Interval uint64 `json:"interval"` // set log cron interval time (ms)
}
func (d *Dotter) Restart(ctx context.Context, interval, timeout uint64) {
d.ctx, d.cancel = context.WithTimeout(ctx, time.Minute*time.Duration(timeout))
d.t = time.Tick(time.Duration(interval) * time.Second)
d.Interval = interval
d.Timeout = timeout
go d.startCron()
}
func (d *Dotter) Tick() <-chan time.Time {
return d.t
}
func (d *Dotter) startCron() {
zap.L().Info("dotter server is started.")
l := zap.L()
for {
select {
case <-d.ctx.Done():
zap.L().Info("dotter server is stopped with timeout.")
return
case <-d.Tick():
}
// max 170*1000
for i := 0; i < 150000; i++ {
// Random generation was not used
// because avoiding the generation of random data might affect the actual time of logging
l.Info("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
logTotal.Inc()
}
}
}
var dotter = Dotter{}
type LogFileCfg struct {
Filename string // File Name with path
MaxSize int // Single file size (M)
MaxBackups int // Number of backups
MaxAge int // Storage days
Compress bool
}
type LogWriter struct {
*zap.SugaredLogger
}
func (l *LogWriter) Write(d []byte) (n int, err error) {
l.Info(string(d))
return len(d), nil
}
type NullWriter struct{}
func (l *NullWriter) Write(d []byte) (n int, err error) {
return len(d), nil
}
func NewLogger(fileCfg *LogFileCfg, development bool, level string, opts ...zap.Option) (*zap.Logger, error) {
cfg := zap.NewProductionConfig()
cfg.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
cfg.Development = development
if err := cfg.Level.UnmarshalText([]byte(level)); err != nil {
return nil, fmt.Errorf("fail to parse log level, level = %s, err = %s", level, err.Error())
}
var l *zap.Logger
if fileCfg != nil {
writer := &lumberjack.Logger{
Filename: fileCfg.Filename,
MaxSize: fileCfg.MaxSize,
MaxBackups: fileCfg.MaxBackups,
MaxAge: fileCfg.MaxAge,
Compress: fileCfg.Compress,
}
core := zapcore.NewCore(
zapcore.NewJSONEncoder(cfg.EncoderConfig),
zapcore.NewMultiWriteSyncer(zapcore.AddSync(writer)),
cfg.Level,
)
l = zap.New(core, append(opts, zap.AddCaller())...)
// panic info reset to logger file
// the following info log can't be deleted
// it assists in creating log files and directories
// avoid directories that do not have errors
l.Info("reset panic stderr to logger file, open file again")
f, err := os.OpenFile(fileCfg.Filename, os.O_CREATE|os.O_WRONLY|os.O_APPEND, os.ModePerm)
if err != nil {
return nil, fmt.Errorf("can't open new log file: %v", err)
}
defer f.Close()
err = syscall.Dup2(int(f.Fd()), int(os.Stderr.Fd()))
if err != nil {
return nil, fmt.Errorf("failed to redirect stderr to file: %v", err)
}
return l, nil
}
zap.S().Infof("no logger file, just stdout")
var e error
l, e = cfg.Build()
if e != nil {
return nil, fmt.Errorf("fail to build logger, err = %s", e.Error())
}
if 0 != len(opts) {
l = l.WithOptions(opts...)
}
return l, nil
}
func NewGinEngine(out io.Writer, logLevel string) *gin.Engine {
gin.SetMode(gin.ReleaseMode)
e := gin.New()
if logLevel == "debug" {
gin.SetMode(gin.DebugMode)
out = os.Stdout
}
e.Use(gin.RecoveryWithWriter(out))
e.Use(gin.LoggerWithWriter(out))
e.Use(CheckParamsIsValid())
return e
}
func CheckParamsIsValid() gin.HandlerFunc {
return func(ctx *gin.Context) {
paramsMap := make(map[string]string)
for _, param := range ctx.Params {
paramsMap[param.Key] = param.Value
if param.Value == "" {
ctx.JSON(http.StatusBadRequest, gin.H{"message": fmt.Sprintf("params[%s] is required!", param.Key)})
ctx.Abort()
return
}
//if strings.Contains(param.Value, " ") {
// ctx.JSON(http.StatusBadRequest, gin.H{"message": fmt.Sprintf("[%s] cannot contain spaces!", param.Key)})
// ctx.Abort()
// return
//}
}
}
}
var (
httpEnable bool
logDevelopment bool
logLevel string
logInterval uint64
logTimeOut uint64
logFile string
)
func ParseFlags() {
flag.BoolVar(&httpEnable, "http", false, "start http.")
flag.BoolVar(&logDevelopment, "development", false, "set log development.")
flag.StringVar(&logLevel, "level", "info", "set log level.")
flag.Uint64Var(&logInterval, "interval", 1000, "set log cron interval time (s).")
flag.Uint64Var(&logTimeOut, "timeout", 60, "set log cron timeout (minute).")
flag.StringVar(&logFile, "file", "", "log file name with path.")
flag.Parse()
}
var (
logTotal = promauto.NewGauge(prometheus.GaugeOpts{
Name: "cron_log_total",
Help: "The total number of cron log",
})
)
func SyncStartWithHTTP(ctx context.Context, cancel context.CancelFunc) {
mainCtx := ctx
engine := NewGinEngine(&NullWriter{}, logLevel)
//engine := NewGinEngine(&LogWriter{zap.S()}, logLevel)
//engine := gin.Default()
engine.GET("/config", func(ctx *gin.Context) {
if dotter.ctx == nil {
ctx.JSON(http.StatusOK, gin.H{
"deadline": "",
"interval": "",
})
return
}
deadline, _ := dotter.ctx.Deadline()
ctx.JSON(http.StatusOK, gin.H{
"deadline": deadline,
"interval": dotter.Interval,
})
})
engine.POST("/reset", func(ctx *gin.Context) {
req := Dotter{}
err := ctx.BindJSON(&req)
if err != nil {
ctx.JSON(http.StatusBadRequest, gin.H{"message": err.Error()})
return
}
if req.Timeout == 0 || req.Interval == 0 {
ctx.JSON(http.StatusBadRequest, gin.H{"message": "timeout, interval is required"})
return
}
dotter.Restart(mainCtx, req.Interval, req.Timeout)
logTotal.Set(0)
ctx.JSON(http.StatusOK, gin.H{"message": "success"})
})
engine.POST("/stop", func(ctx *gin.Context) {
dotter.cancel()
ctx.JSON(http.StatusOK, gin.H{"message": "success"})
})
go func() {
err := http.ListenAndServe(fmt.Sprintf("0.0.0.0:%d", 9093), engine)
if err != nil {
zap.S().Panic("failed to listen and serve http server.", zap.Error(err))
cancel()
}
}()
}
func SyncStartWithShellTimeOut(ctx context.Context, cancel context.CancelFunc, interval, timeout uint64) {
go func() {
dotter.Restart(ctx, interval, timeout)
time.Sleep(time.Minute * time.Duration(dotter.Timeout))
cancel()
}()
}
func main() {
ParseFlags()
var fileCfg *LogFileCfg
if logFile != "" {
fileCfg = &LogFileCfg{
Filename: logFile,
MaxSize: 1024,
MaxBackups: 1,
MaxAge: 7,
Compress: false,
}
}
l, err := NewLogger(fileCfg, logDevelopment, logLevel)
if err != nil {
panic(err)
}
l.With(zap.String("name", server))
zap.ReplaceGlobals(l)
ctx, cancel := context.WithCancel(context.Background())
http.Handle("/metrics", promhttp.Handler())
go func() {
err = http.ListenAndServe(fmt.Sprintf("0.0.0.0:%d", 9094), nil)
if err != nil {
l.Panic("failed to listen and serve admin http server.", zap.Error(err))
cancel()
}
}()
if httpEnable {
SyncStartWithHTTP(ctx, cancel)
} else {
SyncStartWithShellTimeOut(ctx, cancel, logInterval, logTimeOut)
}
select {
case <-ctx.Done():
os.Exit(0)
return
}
}