/
zap.go
executable file
·69 lines (64 loc) · 1.63 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
package di
import (
"fmt"
"github.com/mix-go/xcli"
"github.com/mix-go/xdi"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"gopkg.in/natefinch/lumberjack.v2"
"os"
"time"
)
func init() {
obj := xdi.Object{
Name: "zap",
New: func() (i interface{}, e error) {
filename := fmt.Sprintf("%s/../logs/mix-plus.log", xcli.App().BasePath)
fileRotate := &lumberjack.Logger{
Filename: filename,
MaxBackups: 7,
}
atomicLevel := zap.NewAtomicLevelAt(zap.InfoLevel)
core := zapcore.NewCore(
zapcore.NewConsoleEncoder(zapcore.EncoderConfig{
TimeKey: "T",
LevelKey: "L",
NameKey: "N",
CallerKey: "C",
MessageKey: "M",
StacktraceKey: "S",
LineEnding: zapcore.DefaultLineEnding,
EncodeLevel: zapcore.CapitalLevelEncoder,
EncodeTime: func(t time.Time, enc zapcore.PrimitiveArrayEncoder) {
enc.AppendString(t.Format("2006-01-02 15:04:05.000"))
},
EncodeDuration: zapcore.StringDurationEncoder,
EncodeCaller: zapcore.ShortCallerEncoder,
}),
zapcore.NewMultiWriteSyncer(zapcore.AddSync(os.Stdout), zapcore.AddSync(fileRotate)),
atomicLevel,
)
logger := zap.New(core, zap.AddCaller())
if xcli.App().Debug {
atomicLevel.SetLevel(zap.DebugLevel)
}
return logger.Sugar(), nil
},
}
if err := xdi.Provide(&obj); err != nil {
panic(err)
}
}
func Zap() (logger *zap.SugaredLogger) {
if err := xdi.Populate("zap", &logger); err != nil {
panic(err)
}
return
}
type ZapOutput struct {
Logger *zap.SugaredLogger
}
func (t *ZapOutput) Write(p []byte) (n int, err error) {
t.Logger.Info(string(p))
return len(p), nil
}