Skip to content

Commit e7f05c8

Browse files
committed
refactor🎨:命名格式化
1 parent 1269a3a commit e7f05c8

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

debug/writer/file.go

+12-10
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ import (
1010
"time"
1111
)
1212

13-
// fileNameTimeFormat 文件名称格式
14-
const fileNameTimeFormat = "2006-01-02"
13+
// timeFormat 时间格式
14+
// 用于文件名称格式
15+
const timeFormat = "2006-01-02"
1516

1617
// FileWriter 文件写入结构体
1718
type FileWriter struct {
@@ -33,13 +34,13 @@ func NewFileWriter(opts ...Option) (*FileWriter, error) {
3334
var filename string
3435
var err error
3536
for {
36-
filename = p.getFilenameAccordingToTimestamp()
37+
filename = p.getFilename()
3738
_, err := os.Stat(filename)
3839
if err != nil {
3940
if os.IsNotExist(err) {
4041
if p.num > 0 {
4142
p.num--
42-
filename = p.getFilenameAccordingToTimestamp()
43+
filename = p.getFilename()
4344
}
4445
//文件不存在
4546
break
@@ -76,15 +77,15 @@ func (p *FileWriter) write() {
7677

7778
func (p *FileWriter) checkFile() {
7879
info, _ := p.file.Stat()
79-
if strings.Index(p.file.Name(), time.Now().Format(fileNameTimeFormat)) < 0 ||
80+
if strings.Index(p.file.Name(), time.Now().Format(timeFormat)) < 0 ||
8081
(p.opts.cap > 0 && uint(info.Size()) > p.opts.cap) {
8182
//生成新文件
8283
if uint(info.Size()) > p.opts.cap {
8384
p.num++
8485
} else {
8586
p.num = 0
8687
}
87-
filename := p.getFilenameAccordingToTimestamp()
88+
filename := p.getFilename()
8889
_ = p.file.Close()
8990
p.file, _ = os.OpenFile(filename, os.O_WRONLY|os.O_APPEND|os.O_CREATE|os.O_SYNC, 0600)
9091
}
@@ -105,20 +106,21 @@ func (p *FileWriter) Write(data []byte) (n int, err error) {
105106
return n, nil
106107
}
107108

108-
// getFilenameAccordingToTimestamp 通过日期命名log文件
109-
func (p *FileWriter) getFilenameAccordingToTimestamp() string {
109+
// getFilename 获取log文件名
110+
// 目前为:以日期格式命名,eg:2006-01-02.log or 2006-01-02.log
111+
func (p *FileWriter) getFilename() string {
110112
if p.FilenameFunc != nil {
111113
return p.FilenameFunc(p)
112114
}
113115
if p.opts.cap == 0 {
114116
return filepath.Join(p.opts.path,
115117
fmt.Sprintf("%s.%s",
116-
time.Now().Format(fileNameTimeFormat),
118+
time.Now().Format(timeFormat),
117119
p.opts.suffix))
118120
}
119121
return filepath.Join(p.opts.path,
120122
fmt.Sprintf("%s-[%d].%s",
121-
time.Now().Format(fileNameTimeFormat),
123+
time.Now().Format(timeFormat),
122124
p.num,
123125
p.opts.suffix))
124126
}

0 commit comments

Comments
 (0)