/
init.go
70 lines (56 loc) · 1.58 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
package log
import (
"context"
"github.com/si9ma/KillOJ-common/utils"
"github.com/opentracing/opentracing-go"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
type LogEncoding string
const Json LogEncoding = "json"
const Console LogEncoding = "console"
var zapLogger *zap.Logger
// auto init
func init() {
_ = Init([]string{}, Json)
}
// manual init
func Init(outputPaths []string, encode LogEncoding) (err error) {
// write to stdout when output path is empty
if len(outputPaths) == 0 {
outputPaths = []string{"stdout"}
}
// on debug mode, write log to stdout at the same time
if utils.IsDebug() && !utils.ContainsString(outputPaths, "stdout") {
outputPaths = append(outputPaths, "stdout")
}
// config format
encoderCfg := zap.NewProductionEncoderConfig()
encoderCfg.TimeKey = "timestamp"
encoderCfg.EncodeTime = zapcore.ISO8601TimeEncoder
if encode == Console {
encoderCfg.EncodeLevel = zapcore.CapitalColorLevelEncoder
}
cfg := zap.NewProductionConfig()
cfg.Encoding = string(encode)
cfg.EncoderConfig = encoderCfg
cfg.OutputPaths = outputPaths // set output paths
if zapLogger, err = cfg.Build(); err != nil {
// if build fail , set zapLogger as a default logger
// todo There may be a bug here
zapLogger, _ = zap.NewProduction()
}
return
}
// get the normal Logger without write span log
func Bg() Logger {
return logger{logger: zapLogger}
}
// if the context contains an span,
// return a spanLogger
func For(ctx context.Context) Logger {
if span := opentracing.SpanFromContext(ctx); span != nil {
return spanLogger{span: span, logger: zapLogger}
}
return Bg()
}