Files
loggerx/filePath.go
T

83 lines
1.7 KiB
Go
Raw Normal View History

2024-01-22 18:05:51 +08:00
package loggerx
import (
"fmt"
"os"
"path/filepath"
"time"
)
2024-01-23 13:37:51 +08:00
// 文件操作
2024-01-22 18:05:51 +08:00
// 获取最新的文件名
2024-01-23 00:12:08 +08:00
func (l *Logger) nowFileName(event string) string {
2024-01-22 18:05:51 +08:00
// ioc, _ := time.LoadLocation("Asia/Shanghai")
// timeDir := fmt.Sprint(time.Now().In(ioc).Format("2006/01/02/15")) // 2006-01-02 15:04:05
timeDir := fmt.Sprint(time.Now().Local().Format("2006/01/02")) // 2006-01-02 15:04:05
2024-01-23 00:12:08 +08:00
if l.channel != "" {
timeDir = l.channel + "/" + timeDir
}
path := l.option.dir + "/" + timeDir + "_"+event + ".log"
2024-01-22 18:05:51 +08:00
// fmt.Println(filepath.Abs(path))
return path
}
// 新建文件
2024-01-23 00:12:08 +08:00
func (l *Logger) getFile(event string,isRefresh bool) (*os.File, error) {
f := l.loadFile(event)
if f != nil && !isRefresh {
return f, nil
}
2024-01-22 18:05:51 +08:00
l.mu.Lock()
defer l.mu.Unlock()
2024-01-23 00:12:08 +08:00
fileName := l.nowFileName(event)
2024-01-22 18:05:51 +08:00
dir, _ := filepath.Split(fileName) // 识别目录与文件
os.MkdirAll(dir, os.ModePerm) // 创建多层目录,如果存在不会报错
// 打开该文件,如果不存在则创建
file, err := os.OpenFile(fileName, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0644)
if err != nil {
// 打开失败,尝试创建
fmt.Println("打开日志文件失败")
2024-01-23 00:12:08 +08:00
return nil, err
}
// 关闭原来的
if f != nil {
closeFile(f)
}
l.filePath.Store(l.channel, &filePath{
file: file,
fileName: fileName,
})
return file, nil
}
// 加载文件
func (l *Logger) loadFile(event string) *os.File {
val, ok := l.filePath.Load(l.channel)
if !ok {
return nil
}
f := val.(*filePath)
if f == nil {
return nil
2024-01-22 18:05:51 +08:00
}
2024-01-23 00:12:08 +08:00
if f.fileName != l.nowFileName(event) {
// 原来的文件需关闭
closeFile(f.file)
return nil
2024-01-22 18:05:51 +08:00
}
2024-01-23 00:12:08 +08:00
return f.file
2024-01-22 18:05:51 +08:00
}
// 关闭文件
2024-01-23 00:12:08 +08:00
func closeFile(f *os.File) error {
f.Sync()
return f.Close()
2024-01-22 18:05:51 +08:00
}