Files
loggerx/filePath.go
T

108 lines
2.5 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
2024-05-08 16:33:58 +08:00
prefix := ""
switch l.option.fileSplit {
case FileSplitTimeA:
// (年/月/日/时)
2024-06-27 21:08:49 +08:00
prefix = fmt.Sprint(time.Now().In(l.option.timeZone).Format("2006/01/02/15")) // 2006-01-02 15:04:05
2024-05-08 16:33:58 +08:00
case FileSplitTimeB:
// (年/月/日)
2024-06-27 21:08:49 +08:00
prefix = fmt.Sprint(time.Now().In(l.option.timeZone).Format("2006/01/02")) // 2006-01-02 15:04:05
2024-05-08 16:33:58 +08:00
case FileSplitTimeC:
// (年/月-日)
2024-06-27 21:08:49 +08:00
prefix = fmt.Sprint(time.Now().In(l.option.timeZone).Format("2006/01-02")) // 2006-01-02 15:04:05
2024-05-08 16:33:58 +08:00
case FileSplitTimeD:
// (年-月-日-时)
2024-06-27 21:08:49 +08:00
prefix = fmt.Sprint(time.Now().In(l.option.timeZone).Format("2006-01-02-15")) // 2006-01-02 15:04:05
2024-05-08 16:33:58 +08:00
case FileSplitTimeE:
// (年-月-日)
2024-06-27 21:08:49 +08:00
prefix = fmt.Sprint(time.Now().In(l.option.timeZone).Format("2006-01-02")) // 2006-01-02 15:04:05
2024-05-08 16:33:58 +08:00
}
if prefix != "" {
prefix = prefix + "_"
}
// 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 != "" {
2024-05-08 16:33:58 +08:00
prefix = l.channel + "/" + prefix
2024-01-23 00:12:08 +08:00
}
2024-05-08 16:33:58 +08:00
path := l.option.dir + "/" + prefix + event + ".log"
2024-01-22 18:05:51 +08:00
// fmt.Println(filepath.Abs(path))
return path
}
// 新建文件
2024-02-03 01:57:56 +08:00
func (l *Logger) getFile(event string, isRefresh bool) (*os.File, error) {
2024-01-23 00:12:08 +08:00
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) // 创建多层目录,如果存在不会报错
// 打开该文件,如果不存在则创建
2024-02-03 01:57:56 +08:00
file, err := os.OpenFile(fileName, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644)
2024-01-22 18:05:51 +08:00
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
}