forked from go-irain/logger
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlog_file.go
More file actions
113 lines (105 loc) · 2.33 KB
/
log_file.go
File metadata and controls
113 lines (105 loc) · 2.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package logger
import (
"fmt"
"os"
"path/filepath"
"sort"
"sync"
"time"
)
//LogFile 日子结构
type LogFile struct {
dir string
filename string
filesize int64
mu *sync.Mutex
logfile *os.File
_suffix int
_date *time.Time
}
func (f *LogFile) isMustRename() bool {
if dailyFlag {
t, _ := time.Parse(TimeDayFormat, time.Now().Format(TimeDayFormat))
if t.After(*f._date) {
return true
}
} else {
if maxFileSize > 0 && f.filesize > maxFileSize {
return true
}
}
return false
}
func (f *LogFile) rename() {
if dailyFlag {
fn := f.dir + "/" + f.filename + "." + f._date.Format(TimeDayFormat)
if !f.isExist(fn) && f.isMustRename() {
if f.logfile != nil {
f.logfile.Close()
}
err := os.Rename(f.dir+"/"+f.filename, fn)
if err != nil {
fmt.Println("rename err", err.Error())
}
t, _ := time.Parse(TimeDayFormat, time.Now().Format(TimeDayFormat))
f._date = &t
f.logfile, _ = os.Create(f.dir + "/" + f.filename)
}
} else {
if maxFileSize > 0 && f.filesize > maxFileSize {
err := f.rotate()
if err != nil {
fmt.Println("333", err.Error())
}
}
}
}
func (f *LogFile) write(data []byte) (int, error) {
n, err := f.logfile.Write(data)
if err != nil {
return n, err
}
f.filesize += int64(n)
f.rename()
return n, err
}
// 获取目录下指定前缀的所有日志文件
func (f *LogFile) removeFiles() {
fs, err := filepath.Glob(fmt.Sprintf("%s/%s.*", f.dir, f.filename))
if err != nil {
return
}
sort.Strings(fs)
x := len(fs) - (int(maxFileCount) - 1)
if maxFileCount > 0 && x > 0 {
dels := fs[:x]
for _, v := range dels {
os.Remove(v)
}
}
}
// 分割
func (f *LogFile) rotate() error {
f.removeFiles()
if f != nil && f.logfile != nil {
f.logfile.Sync()
f.logfile.Close()
os.Rename(f.dir+"/"+f.filename, f.dir+"/"+f.filename+time.Now().Format(".20060102150405"))
}
// 创建最新的日志文件
fd, err := os.OpenFile(f.dir+"/"+f.filename, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0666)
if err != nil {
return err
}
fi, err := fd.Stat()
if err != nil {
return err
}
f.logfile = fd
f.filesize = fi.Size()
return nil
}
func (f *LogFile) isExist(path string) bool {
_, err := os.Stat(path)
return err == nil || os.IsExist(err)
}