初始化日志目录
This commit is contained in:
+20
-1
@@ -1,7 +1,7 @@
|
|||||||
package loggerx
|
package loggerx
|
||||||
|
|
||||||
// author:黄新云
|
// author:黄新云
|
||||||
// lastTime:2023年6月30日21:28:04
|
// lastTime:2024年4月3日21:18:05
|
||||||
// desc: 日志封装类
|
// desc: 日志封装类
|
||||||
|
|
||||||
import (
|
import (
|
||||||
@@ -38,6 +38,11 @@ func NewLogger(opts ...Option) *Logger {
|
|||||||
apply(&opt)
|
apply(&opt)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 验证文件夹权限
|
||||||
|
if !checkDir(opt.dir) {
|
||||||
|
panic("文件夹权限不足")
|
||||||
|
}
|
||||||
|
|
||||||
l := &Logger{
|
l := &Logger{
|
||||||
filePath: &sync.Map{},
|
filePath: &sync.Map{},
|
||||||
mu: &sync.Mutex{},
|
mu: &sync.Mutex{},
|
||||||
@@ -112,3 +117,17 @@ func getGID() string {
|
|||||||
b = b[:bytes.IndexByte(b, ' ')]
|
b = b[:bytes.IndexByte(b, ' ')]
|
||||||
return string(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
|
||||||
|
}
|
||||||
|
|||||||
+2
-1
@@ -17,7 +17,8 @@ func TestLogger(t *testing.T) {
|
|||||||
|
|
||||||
l.Error(context.Background(), "test error")
|
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")
|
l.Info(context.Background(), "test info")
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user