Repository navigation
Expand file tree
/
Copy pathlogwriter.go
More file actions
178 lines (158 loc) · 3.76 KB
/
Copy pathlogwriter.go
File metadata and controls
178 lines (158 loc) · 3.76 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// Copyright (c) 2024 Tencent Inc.
// SPDX-License-Identifier: Apache-2.0
//
package CubeLog
import (
"errors"
"fmt"
"os"
"path/filepath"
"strconv"
"time"
)
const (
DAY DateType = iota
HOUR
)
type ConsoleWriter struct {
}
type RollFileWriter struct {
logpath string
name string
num int
size int64
currSize int64
currFile *os.File
openTime int64
}
type DateWriter struct {
logpath string
name string
dateType DateType
num int
currDate string
currFile *os.File
openTime int64
hasPrefix bool
}
type HourWriter struct {
}
type DateType uint8
func reOpenFile(path string, currFile **os.File, openTime *int64) {
*openTime = currUnixTime
if *currFile != nil {
(*currFile).Close()
}
of, err := os.OpenFile(path, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0666)
if err == nil {
*currFile = of
} else {
fmt.Println("open log file error", err)
}
}
func (w *ConsoleWriter) Write(v []byte) (int, error) {
return os.Stdout.Write(v)
}
func (w *RollFileWriter) Write(v []byte) (int, error) {
if w.currFile == nil || w.openTime+10 < currUnixTime {
fullPath := filepath.Join(w.logpath, fmt.Sprintf("%s.log", w.name))
reOpenFile(fullPath, &w.currFile, &w.openTime)
}
if w.currFile == nil {
return 0, errors.New("w.currFile was nil")
}
n, _ := w.currFile.Write(v)
w.currSize += int64(n)
if w.currSize >= w.size {
w.currSize = 0
for i := w.num; i >= 1; i-- {
var n1, n2 string
if i > 1 {
n1 = strconv.Itoa(i - 1)
}
n2 = strconv.Itoa(i)
p1 := filepath.Join(w.logpath, fmt.Sprintf("%s.log.%s", w.name, n1))
p2 := filepath.Join(w.logpath, fmt.Sprintf("%s.log.%s", w.name, n2))
if n1 == "" {
p1 = filepath.Join(w.logpath, fmt.Sprintf("%s.log", w.name))
}
if _, err := os.Stat(p1); !os.IsNotExist(err) {
os.Rename(p1, p2)
}
}
go func() {
os.Remove(filepath.Join(w.logpath, fmt.Sprintf("%s.log.%d", w.name, w.num)))
}()
fullPath := filepath.Join(w.logpath, fmt.Sprintf("%s.log", w.name))
reOpenFile(fullPath, &w.currFile, &w.openTime)
}
return n, nil
}
func NewRollFileWriter(logpath, name string, num, sizeMB int) *RollFileWriter {
w := &RollFileWriter{
logpath: logpath,
name: name,
num: num,
size: int64(sizeMB) * 1024 * 1024,
}
fullPath := filepath.Join(logpath, name+".log")
st, _ := os.Stat(fullPath)
if st != nil {
w.currSize = st.Size()
}
return w
}
func (w *DateWriter) Write(v []byte) (int, error) {
if w.currFile == nil || w.openTime+10 < currUnixTime {
fullPath := filepath.Join(w.logpath, fmt.Sprintf("%s.log.%s", w.name, w.currDate))
reOpenFile(fullPath, &w.currFile, &w.openTime)
}
if w.currFile == nil {
return 0, errors.New("w.currFile was nil")
}
currDate := w.getCurrDate()
if w.currDate != currDate {
w.currDate = currDate
w.cleanOldLogs()
fullPath := filepath.Join(w.logpath, fmt.Sprintf("%s.log.%s", w.name, w.currDate))
reOpenFile(fullPath, &w.currFile, &w.openTime)
}
n, _ := w.currFile.Write(v)
return n, nil
}
func NewDateWriter(logpath, name string, dateType DateType, num int) *DateWriter {
w := &DateWriter{
logpath: logpath,
name: name,
num: num,
dateType: dateType,
hasPrefix: true,
}
w.currDate = w.getCurrDate()
return w
}
func (w *DateWriter) cleanOldLogs() {
format := "20060102"
duration := -time.Hour * 24
if w.dateType == HOUR {
format = "2006010215"
duration = -time.Hour
}
t := time.Now()
t = t.Add(duration * time.Duration(w.num))
for i := 0; i < 30; i++ {
t = t.Add(duration)
k := t.Format(format)
fullPath := filepath.Join(w.logpath, fmt.Sprintf("%s.log.%s", w.name, k))
if _, err := os.Stat(fullPath); !os.IsNotExist(err) {
os.Remove(fullPath)
}
}
return
}
func (w *DateWriter) getCurrDate() string {
if w.dateType == HOUR {
return currDateHour
}
return currDateDay
}