From cb25b524b8df943e7a883b3086b75e4a3041389d Mon Sep 17 00:00:00 2001 From: Yun Date: Wed, 3 Apr 2024 21:18:44 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=9D=E5=A7=8B=E5=8C=96=E6=97=A5=E5=BF=97?= =?UTF-8?q?=E7=9B=AE=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- loggerx.go | 21 ++++++++++++++++++++- loggerx_test.go | 3 ++- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/loggerx.go b/loggerx.go index 8eddcbd..796a919 100644 --- a/loggerx.go +++ b/loggerx.go @@ -1,7 +1,7 @@ package loggerx // author:黄新云 -// lastTime:2023年6月30日21:28:04 +// lastTime:2024年4月3日21:18:05 // desc: 日志封装类 import ( @@ -38,6 +38,11 @@ func NewLogger(opts ...Option) *Logger { apply(&opt) } + // 验证文件夹权限 + if !checkDir(opt.dir) { + panic("文件夹权限不足") + } + l := &Logger{ filePath: &sync.Map{}, mu: &sync.Mutex{}, @@ -112,3 +117,17 @@ func getGID() string { b = b[:bytes.IndexByte(b, ' ')] return string(b) } + +// 验证文件夹权限 +// 根文件夹如果不存在则创建 +func checkDir(dir string) bool { + if _, err := os.Stat(dir); err != nil { + if os.IsNotExist(err) { + if err := os.MkdirAll(dir, os.ModePerm); err != nil { + log.Println("创建文件夹失败", err) + return false + } + } + } + return true +} diff --git a/loggerx_test.go b/loggerx_test.go index 830e01c..e6c1795 100644 --- a/loggerx_test.go +++ b/loggerx_test.go @@ -17,7 +17,8 @@ func TestLogger(t *testing.T) { l.Error(context.Background(), "test error") - l.Channel("channel").Error(context.Background(), "test error") + l.Channel("channel1").Error(context.Background(), "channel1 test error") + l.Channel("channel2").Error(context.Background(), "channel2 test error") l.Info(context.Background(), "test info")