添加自动文件压缩的实现
This commit is contained in:
@@ -12,6 +12,19 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
// basePath 进程工作目录(用于把绝对路径裁成相对路径)
|
||||
// 只算一次并缓存:filepath.Abs 每次都要走系统调用,而它每条日志都会被用到
|
||||
func (l *Logger) basePath() string {
|
||||
l.basePathOnce.Do(func() {
|
||||
p, err := filepath.Abs("./")
|
||||
if err != nil {
|
||||
p = ""
|
||||
}
|
||||
l.basePathVal = strings.ReplaceAll(p, "\\", "/")
|
||||
})
|
||||
return l.basePathVal
|
||||
}
|
||||
|
||||
func (l *Logger) logger(ctx context.Context, event string, v ...any) {
|
||||
// 调用方可能是 log 包(Logger.Write -> logger),所以这里取第 2 层
|
||||
pc, file, line, ok := runtime.Caller(2)
|
||||
@@ -21,10 +34,7 @@ func (l *Logger) logger(ctx context.Context, event string, v ...any) {
|
||||
if fn := runtime.FuncForPC(pc); fn != nil {
|
||||
funcName = strings.TrimPrefix(filepath.Ext(fn.Name()), ".")
|
||||
}
|
||||
if basePath, err := filepath.Abs("./"); err == nil {
|
||||
basePath = strings.ReplaceAll(basePath, "\\", "/")
|
||||
file = strings.TrimPrefix(strings.ReplaceAll(file, "\\", "/"), basePath)
|
||||
}
|
||||
file = strings.TrimPrefix(strings.ReplaceAll(file, "\\", "/"), l.basePath())
|
||||
}
|
||||
|
||||
nowTime := time.Now().In(l.option.timeZone).Format("2006-01-02 15:04:05.000000")
|
||||
|
||||
Reference in New Issue
Block a user