-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzap.go
126 lines (111 loc) · 2.92 KB
/
zap.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
// Copyright 2022 huija
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package zap
import (
"github.com/taouniverse/tao"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"gopkg.in/natefinch/lumberjack.v2"
"os"
)
/**
import _ "github.com/taouniverse/tao-zap"
*/
// Z config of zap
var Z = new(Config)
func init() {
err := tao.Register(ConfigKey, Z, setup)
if err != nil {
panic(err.Error())
}
}
// Logger based on zap
var Logger *zap.SugaredLogger
type zapSetup struct {
enc zapcore.Encoder
level zapcore.Level
out zapcore.WriteSyncer
}
var setups []*zapSetup // core's setup
var ws []zapcore.WriteSyncer // core's root writers
var cores []zapcore.Core // cores
// setup with zap config
func setup() (err error) {
for t, c := range Z.Logs {
switch t {
case File:
setupFileLog(c)
case Console:
setupConsoleLog(c)
default:
return tao.NewErrorWrapped("config: log type invalid", nil)
}
}
// setup cores
for _, s := range setups {
cores = append(cores, zapcore.NewCore(s.enc, s.out, s.level))
}
// final logger
Logger = zap.New(
zapcore.NewTee(cores...),
zap.AddCaller(),
zap.AddCallerSkip(Z.CallDepth),
).Sugar()
// add zap to tao
err = tao.SetLogger(ConfigKey, Logger)
if err != nil {
return
}
err = tao.SetWriter(ConfigKey, zapcore.NewMultiWriteSyncer(ws...))
if !Z.Coexist {
_ = tao.DeleteLogger(tao.ConfigKey)
_ = tao.DeleteWriter(tao.ConfigKey)
}
return
}
func setupConsoleLog(c *config) {
// writerSync
ws = append(ws, zapcore.AddSync(os.Stdout))
// encoderConfig
encoderConfig := zap.NewProductionEncoderConfig()
encoderConfig.EncodeTime = zapcore.TimeEncoderOfLayout("2006/01/02 15:04:05.000")
encoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder
// setup
setups = append(setups, &zapSetup{
enc: zapcore.NewConsoleEncoder(encoderConfig),
level: c.Level,
out: ws[len(ws)-1],
})
}
func setupFileLog(c *config) {
sliceLogger := &lumberjack.Logger{
Filename: c.Store.Path,
MaxSize: c.Store.MaxSize,
MaxBackups: c.Store.MaxBackups,
MaxAge: c.Store.MaxAge,
Compress: c.Store.Compress,
LocalTime: c.Store.LocalZone,
}
// writerSync
ws = append(ws, zapcore.AddSync(sliceLogger))
// encoderConfig
encoderConfig := zap.NewProductionEncoderConfig()
encoderConfig.EncodeLevel = zapcore.CapitalLevelEncoder
// setup
setups = append(setups, &zapSetup{
enc: zapcore.NewJSONEncoder(encoderConfig),
level: c.Level,
out: ws[len(ws)-1],
})
}