-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbasic_logger.go
88 lines (80 loc) · 2.41 KB
/
basic_logger.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
package logbus
import (
"time"
prometheusClient "github.com/prometheus/client_golang/prometheus"
"github.com/sandwich-go/logbus/debug"
"github.com/sandwich-go/logbus/monitor"
"github.com/sandwich-go/logbus/monitor/noop"
"github.com/sandwich-go/logbus/monitor/prometheus"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
var (
gBasicZLogger *zap.Logger // 用来产生新stdLogger, 更多用在自定义tag的时候
Setting = newDefaultConf() // logBus的全局配置
)
func initBasics(c *Conf) {
initZapSetting()
resetLogBus()
// init logBus global setting
Setting = c
// init EncodeConfig
if c.Dev {
EncodeConfig.EncodeLevel = zapcore.LowercaseColorLevelEncoder
EncodeConfig.CallerKey = "caller"
EncodeConfig.EncodeDuration = zapcore.StringDurationEncoder
if err := debug.InitAppModuleFilePath(); err == nil {
EncodeConfig.EncodeCaller = debug.RelativePathCallerEncoder
} else {
// fixme 无法使用相对路径的caller模式,退化到short模式
EncodeConfig.EncodeCaller = zapcore.ShortCallerEncoder
}
} else {
EncodeConfig.EncodeDuration = DurationEncoder
}
if c.EncodeCaller != nil {
EncodeConfig.EncodeCaller = c.EncodeCaller
}
// init gBasicZLogger
var err error
ZapConf.Level = zap.NewAtomicLevelAt(c.LogLevel)
ZapConf.EncoderConfig = EncodeConfig
if c.Dev {
ZapConf.Development = true
}
var clock zapcore.Clock
clock = localClock{}
if c.UseSystemClock {
clock = zapcore.DefaultClock
}
gBasicZLogger, err = ZapConf.Build(
zap.AddCallerSkip(c.CallerSkip),
zap.AddStacktrace(c.StackLogLevel),
zap.WithClock(clock),
zap.WithCaller(ZapConf.EncoderConfig.CallerKey != ""),
)
if err != nil {
panic(err)
}
}
func setDefaultMetricsReporter(
monitorOutput MonitorOutput,
defaultPrometheusListenAddress string,
defaultPrometheusPath string,
defaultPercentiles []float64,
defaultLabel prometheusClient.Labels,
timingMaxAge time.Duration) {
switch monitorOutput {
case Noop:
monitor.DefaultMetricsReporter = noop.New()
case Logbus:
monitor.DefaultMetricsReporter = newLogReporter()
case Prometheus:
var err error
monitor.DefaultMetricsReporter, err = prometheus.New(defaultPrometheusListenAddress, defaultPrometheusPath, defaultPercentiles, defaultLabel, timingMaxAge)
if err != nil {
panic(err)
}
DebugWithChannel(Monitor, "", String("prometheus [http] listening on", defaultPrometheusListenAddress), String("path", defaultPrometheusPath))
}
}