log
package log
import (
"acc/config"
"acc/utils"
"fmt"
"os"
"path/filepath"
"github.com/natefinch/lumberjack"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
var logger *zap.SugaredLogger
func init() {
const logDirPath = "./log"
if !utils.IsPathExist(logDirPath) {
utils.CreatDir(logDirPath)
}
logger = initLogger(filepath.Join(logDirPath, "acc.log"), "debug")
}
func initLogger(logPath string, loglevel string) *zap.SugaredLogger {
// 日志分割
hook := lumberjack.Logger{
Filename: logPath, // 日志文件路径,默认 os.TempDir()
MaxSize: 10, // 每个日志文件保存10M,默认 100M
MaxBackups: 30, // 保留30个备份,默认不限
MaxAge: 7, // 保留7天,默认不限
Compress: true, // 是否压缩,默认不压缩
}
write := zapcore.AddSync(&hook)
// 设置日志级别
// debug->info->warn->error
var level zapcore.Level
switch loglevel {
case "debug":
level = zap.DebugLevel
case "info":
level = zap.InfoLevel
case "warn":
level = zap.WarnLevel
case "error":
level = zap.ErrorLevel
default:
level = zap.InfoLevel
}
encoderConfig := zapcore.EncoderConfig{
TimeKey: "time",
LevelKey: "level",
NameKey: "logger",
CallerKey: "lineNum",
MessageKey: "msg",
StacktraceKey: "stacktrace",
LineEnding: zapcore.DefaultLineEnding,
EncodeLevel: func(level zapcore.Level, encoder zapcore.PrimitiveArrayEncoder) {
encoder.AppendString(fmt.Sprintf("[%s]", level.CapitalString()))
},
EncodeTime: zapcore.TimeEncoderOfLayout("2006-01-02 15:04:05.000000"),
EncodeDuration: zapcore.SecondsDurationEncoder,
EncodeCaller: zapcore.ShortCallerEncoder,
EncodeName: zapcore.FullNameEncoder,
}
// 设置日志级别
atomicLevel := zap.NewAtomicLevel()
atomicLevel.SetLevel(level)
core := zapcore.NewCore(
zapcore.NewConsoleEncoder(encoderConfig),
zapcore.NewMultiWriteSyncer(zapcore.AddSync(os.Stdout), zapcore.AddSync(write)),
level,
)
// 开启开发模式,堆栈跟踪
caller := zap.AddCaller()
// 开启文件及行号
development := zap.Development()
// 构造日志
logger := zap.New(core, caller, development, zap.AddCallerSkip(1))
return logger.Sugar()
}
func Debug(args ...interface{}) {
if config.IsDebug() {
logger.Debug(args)
}
}
func Info(args ...interface{}) {
logger.Info(args)
}
func Warn(args ...interface{}) {
logger.Warn(args)
}
func Error(args ...interface{}) {
logger.Error(args)
}
func Fatal(args ...interface{}) {
logger.Fatal(args)
}
func Debugf(template string, args ...interface{}) {
if config.IsDebug() {
logger.Debug(fmt.Sprintf(template, args...))
}
}
func Infof(template string, args ...interface{}) {
logger.Info(fmt.Sprintf(template, args...))
}
func Errorf(template string, args ...interface{}) {
logger.Error(fmt.Sprintf(template, args...))
}
本文来自博客园,作者:非空丶,转载请注明原文链接:https://www.cnblogs.com/wsshow/articles/18741888

浙公网安备 33010602011771号