-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconsole.go
53 lines (49 loc) · 1.5 KB
/
console.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
package golog
import (
"context"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"gopkg.in/natefinch/lumberjack.v2"
)
func NewConsoleFileHandler(c *FileConfig) *FileConsoleHandler {
hook := &lumberjack.Logger{
Filename: c.LogFilePath + "info.log",
MaxSize: c.MaxSize,
MaxAge: c.MaxAge,
MaxBackups: c.MaxBackups,
}
jsonInfo := zapcore.AddSync(hook)
infoPriority := zap.LevelEnablerFunc(func(lvl zapcore.Level) bool {
return true
})
encoderConfig := zap.NewProductionEncoderConfig()
encoderConfig.LevelKey = ""
encoderConfig.MessageKey = "message"
encoderConfig.TimeKey = ""
encoderConfig.EncodeCaller = zapcore.ShortCallerEncoder
encoderConfig.EncodeName = zapcore.FullNameEncoder
xLogEncoder := zapcore.NewConsoleEncoder(encoderConfig)
var allCore []zapcore.Core
infoCore := zapcore.NewCore(xLogEncoder, jsonInfo, infoPriority)
allCore = append(allCore, infoCore)
core := zapcore.NewTee(allCore...)
var opts []zap.Option
logger := zap.New(core).WithOptions(opts...)
defer logger.Sync()
return &FileConsoleHandler{logger: logger}
}
func consoleLog(logger *zap.Logger, msg string) {
logger.Info(msg)
}
func (fch *FileConsoleHandler) Log(ctx context.Context, l Level, msg string, args ...interface{}) {
logger := fch.logger
logger = logger.With()
consoleLog(logger, msg)
}
func (fch *FileConsoleHandler) LogWith(ctx context.Context, l Level, msg string, m map[string]interface{}) {
logger := fch.logger
consoleLog(logger, msg)
}
func (fch *FileConsoleHandler) Close() (err error) {
return
}