🎨 优化日志用法
This commit is contained in:
parent
3ea919e06c
commit
27dd190fbd
1
.gitignore
vendored
1
.gitignore
vendored
@ -2,4 +2,3 @@
|
||||
vendor
|
||||
logs
|
||||
cache
|
||||
log
|
75
log/say.go
Normal file
75
log/say.go
Normal 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...)
|
||||
}
|
@ -47,7 +47,7 @@ func InitLogger(c LogConfig) {
|
||||
|
||||
// 增加 caller 信息
|
||||
// AddCallerSkip 输出的文件名和行号是调用封装函数的位置,而不是调用日志函数的位置
|
||||
logger := zap.New(zapcore.NewTee(cores...), zap.AddCaller())
|
||||
logger := zap.New(zapcore.NewTee(cores...), zap.AddCaller(), zap.AddCallerSkip(1))
|
||||
Say = logger.Sugar()
|
||||
zap.ReplaceGlobals(logger)
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
package logger
|
||||
|
||||
import (
|
||||
"go.uber.org/zap"
|
||||
"gitee.ltd/lxh/logger/log"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
@ -18,6 +18,6 @@ func TestLogger1(t *testing.T) {
|
||||
|
||||
func TestLogger2(t *testing.T) {
|
||||
InitLogger(LogConfig{Mode: Dev, LokiEnable: false, FileEnable: true})
|
||||
zap.S().Info("我是测试消息")
|
||||
time.Sleep(5 * time.Second)
|
||||
log.Info("我是测试消息")
|
||||
//time.Sleep(5 * time.Second)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user