/
logger.go
141 lines (114 loc) · 3.59 KB
/
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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
Copyright @ 2021 bocloud <fushaosong@beyondcent.com>.
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 log
import (
"github.com/natefinch/lumberjack"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"os"
)
const logPath = "/var/log/carina/carina.log"
var sugareLogger *zap.SugaredLogger
// logPath 日志文件路径
// logLevel 日志级别 debug/info/warn/error
// maxSize 单个文件大小,MB
// maxBackups 保存的文件个数
// maxAge 保存的天数, 没有的话不删除
// compress 压缩
// jsonFormat 是否输出为json格式
// AddCaller 显示调用者
// logInConsole 是否同时输出到控制台
func init() {
hook := lumberjack.Logger{
Filename: logPath, // 日志文件路径
MaxSize: 30, // megabytes
MaxBackups: 3, // 最多保留300个备份
Compress: false, // 是否压缩 disabled by default
}
hook.MaxAge = 1
var syncer zapcore.WriteSyncer
syncer = zapcore.NewMultiWriteSyncer(zapcore.AddSync(os.Stdout), zapcore.AddSync(&hook))
//if logInConsole {
// syncer = zapcore.NewMultiWriteSyncer(zapcore.AddSync(os.Stdout), zapcore.AddSync(&hook))
//} else {
// syncer = zapcore.AddSync(&hook)
//}
encoderConfig := zapcore.EncoderConfig{
TimeKey: "time",
LevelKey: "level",
NameKey: "logger",
CallerKey: "line",
MessageKey: "msg",
StacktraceKey: "stacktrace",
LineEnding: zapcore.DefaultLineEnding,
EncodeLevel: zapcore.LowercaseLevelEncoder, // 小写编码器
EncodeTime: zapcore.ISO8601TimeEncoder, // ISO8601 UTC 时间格式
EncodeDuration: zapcore.SecondsDurationEncoder, //
EncodeCaller: zapcore.ShortCallerEncoder, // 全路径编码器
EncodeName: zapcore.FullNameEncoder,
}
//var encoder zapcore.Encoder
//if jsonFormat {
// encoder = zapcore.NewJSONEncoder(encoderConfig)
//} else {
// encoder = zapcore.NewConsoleEncoder(encoderConfig)
//}
encoder := zapcore.NewConsoleEncoder(encoderConfig)
level := zap.InfoLevel
if os.Getenv("DEBUG") != "" {
level = zap.DebugLevel
}
core := zapcore.NewCore(
encoder,
syncer,
level,
)
log := zap.New(core, zap.AddCaller(), zap.AddCallerSkip(1))
sugareLogger = log.Sugar()
}
func Debug(args ...interface{}) {
sugareLogger.Debug(args...)
}
func Debugf(template string, args ...interface{}) {
sugareLogger.Debugf(template, args...)
}
func Info(args ...interface{}) {
sugareLogger.Info(args...)
}
func Infof(template string, args ...interface{}) {
sugareLogger.Infof(template, args...)
}
func Warn(args ...interface{}) {
sugareLogger.Warn(args...)
}
func Warnf(template string, args ...interface{}) {
sugareLogger.Warnf(template, args...)
}
func Error(args ...interface{}) {
sugareLogger.Error(args...)
}
func Errorf(template string, args ...interface{}) {
sugareLogger.Errorf(template, args...)
}
func Panic(args ...interface{}) {
sugareLogger.Panic(args...)
}
func Panicf(template string, args ...interface{}) {
sugareLogger.Panicf(template, args...)
}
func Fatal(args ...interface{}) {
sugareLogger.Fatal(args...)
}
func Fatalf(template string, args ...interface{}) {
sugareLogger.Fatalf(template, args...)
}