From 27dd190fbd0cc4159a7fd8347dc00d7d7ffd55e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=AF=BB=E6=AC=A2?= Date: Wed, 18 May 2022 17:43:19 +0800 Subject: [PATCH] =?UTF-8?q?:art:=20=E4=BC=98=E5=8C=96=E6=97=A5=E5=BF=97?= =?UTF-8?q?=E7=94=A8=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 3 +- log/say.go | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++ logger.go | 2 +- logger_test.go | 6 ++-- 4 files changed, 80 insertions(+), 6 deletions(-) create mode 100644 log/say.go diff --git a/.gitignore b/.gitignore index c6f9005..7d5048b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,4 @@ .idea vendor logs -cache -log \ No newline at end of file +cache \ No newline at end of file diff --git a/log/say.go b/log/say.go new file mode 100644 index 0000000..ee76012 --- /dev/null +++ b/log/say.go @@ -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...) +} diff --git a/logger.go b/logger.go index 587f8e3..4a656a8 100644 --- a/logger.go +++ b/logger.go @@ -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) } diff --git a/logger_test.go b/logger_test.go index 3caf023..0613aa3 100644 --- a/logger_test.go +++ b/logger_test.go @@ -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) }