🎨 优化日志用法

This commit is contained in:
李寻欢 2022-05-18 17:43:19 +08:00
parent 3ea919e06c
commit 27dd190fbd
4 changed files with 80 additions and 6 deletions

1
.gitignore vendored
View File

@ -2,4 +2,3 @@
vendor vendor
logs logs
cache cache
log

75
log/say.go Normal file
View File

@ -0,0 +1,75 @@
package log
import "go.uber.org/zap"
// Debug uses fmt.Sprint to construct and log a message.
func Debug(args ...interface{}) {
defer zap.S().Sync()
zap.S().Debug(args...)
}
// Info uses fmt.Sprint to construct and log a message.
func Info(args ...interface{}) {
defer zap.S().Sync()
zap.S().Info(args...)
}
// Warn uses fmt.Sprint to construct and log a message.
func Warn(args ...interface{}) {
defer zap.S().Sync()
zap.S().Warn(args...)
}
// Error uses fmt.Sprint to construct and log a message.
func Error(args ...interface{}) {
defer zap.S().Sync()
zap.S().Error(args...)
}
// Panic uses fmt.Sprint to construct and log a message, then panics.
func Panic(args ...interface{}) {
defer zap.S().Sync()
zap.S().Panic(args...)
}
// Fatal uses fmt.Sprint to construct and log a message, then calls os.Exit.
func Fatal(args ...interface{}) {
defer zap.S().Sync()
zap.S().Fatal(args...)
}
// Debugf uses fmt.Sprintf to log a templated message.
func Debugf(template string, args ...interface{}) {
defer zap.S().Sync()
zap.S().Debugf(template, args...)
}
// Infof uses fmt.Sprintf to log a templated message.
func Infof(template string, args ...interface{}) {
defer zap.S().Sync()
zap.S().Infof(template, args...)
}
// Warnf uses fmt.Sprintf to log a templated message.
func Warnf(template string, args ...interface{}) {
defer zap.S().Sync()
zap.S().Warnf(template, args...)
}
// Errorf uses fmt.Sprintf to log a templated message.
func Errorf(template string, args ...interface{}) {
defer zap.S().Sync()
zap.S().Errorf(template, args...)
}
// Panicf uses fmt.Sprintf to log a templated message, then panics.
func Panicf(template string, args ...interface{}) {
defer zap.S().Sync()
zap.S().Panicf(template, args...)
}
// Fatalf uses fmt.Sprintf to log a templated message, then calls os.Exit.
func Fatalf(template string, args ...interface{}) {
defer zap.S().Sync()
zap.S().Fatalf(template, args...)
}

View File

@ -47,7 +47,7 @@ func InitLogger(c LogConfig) {
// 增加 caller 信息 // 增加 caller 信息
// AddCallerSkip 输出的文件名和行号是调用封装函数的位置,而不是调用日志函数的位置 // AddCallerSkip 输出的文件名和行号是调用封装函数的位置,而不是调用日志函数的位置
logger := zap.New(zapcore.NewTee(cores...), zap.AddCaller()) logger := zap.New(zapcore.NewTee(cores...), zap.AddCaller(), zap.AddCallerSkip(1))
Say = logger.Sugar() Say = logger.Sugar()
zap.ReplaceGlobals(logger) zap.ReplaceGlobals(logger)
} }

View File

@ -1,7 +1,7 @@
package logger package logger
import ( import (
"go.uber.org/zap" "gitee.ltd/lxh/logger/log"
"testing" "testing"
"time" "time"
) )
@ -18,6 +18,6 @@ func TestLogger1(t *testing.T) {
func TestLogger2(t *testing.T) { func TestLogger2(t *testing.T) {
InitLogger(LogConfig{Mode: Dev, LokiEnable: false, FileEnable: true}) InitLogger(LogConfig{Mode: Dev, LokiEnable: false, FileEnable: true})
zap.S().Info("我是测试消息") log.Info("我是测试消息")
time.Sleep(5 * time.Second) //time.Sleep(5 * time.Second)
} }