@@ -103,23 +103,17 @@ func (w *Writer) Write(p []byte) (n int, err error) {
103
103
}
104
104
105
105
t := time .Now ()
106
- var b []byte
107
- year , month , day := t .Date ()
108
- b = appendInt (b , year , 4 )
109
- b = append (b , '-' )
110
- b = appendInt (b , int (month ), 2 )
111
- b = append (b , '-' )
112
- b = appendInt (b , day , 2 )
106
+ b := t .AppendFormat (nil , time .RFC3339 )
113
107
114
108
// 按天切割
115
- if ! bytes .Equal (w .creates [:10 ], b ) { //2023-04-05
109
+ if ! bytes .Equal (w .creates [:10 ], b [: 10 ] ) { //2023-04-05
116
110
go w .delete () // 每天检测一次旧文件
117
111
if err := w .rotate (); err != nil {
118
112
return 0 , err
119
113
}
120
114
}
121
- // 按大小切割
122
- if w .size + int64 (len (p )) >= w .maxSize {
115
+ // 按大小切割,需要考虑bw缓冲区中未写入的数据
116
+ if w .size + int64 (len (p ))+ int64 ( w . bw . Buffered ()) >= w .maxSize {
123
117
if err := w .rotate (); err != nil {
124
118
return 0 , err
125
119
}
@@ -194,13 +188,15 @@ func (w *Writer) delete() {
194
188
}
195
189
}
196
190
}
191
+
197
192
func (w * Writer ) name2time (name string ) (time.Time , error ) {
198
193
name = strings .TrimPrefix (name , filepath .Base (w .fname ))
199
194
name = strings .TrimSuffix (name , w .zipsuffix )
200
- return time .Parse (".2006-01-02-150405" , name )
195
+ // 改为微秒级别的文件后缀,避免1s内大量写入造成多次rotate,而覆盖丢失之前的日志文件
196
+ return time .Parse (".2006-01-02-150405.000000" , name )
201
197
}
202
198
func (w * Writer ) time2name (t time.Time ) string {
203
- return t .Format (".2006-01-02-150405" )
199
+ return t .Format (".2006-01-02-150405.000000 " )
204
200
}
205
201
206
202
func (w * Writer ) Close () error {
@@ -230,58 +226,6 @@ func (w *Writer) flush() error {
230
226
return w .bw .Flush ()
231
227
}
232
228
233
- // appendInt appends the decimal form of x to b and returns the result.
234
- // If the decimal form (excluding sign) is shorter than width, the result is padded with leading 0's.
235
- // Duplicates functionality in strconv, but avoids dependency.
236
- func appendInt (b []byte , x int , width int ) []byte {
237
- u := uint (x )
238
- if x < 0 {
239
- b = append (b , '-' )
240
- u = uint (- x )
241
- }
242
-
243
- // 2-digit and 4-digit fields are the most common in time formats.
244
- utod := func (u uint ) byte { return '0' + byte (u ) }
245
- switch {
246
- case width == 2 && u < 1e2 :
247
- return append (b , utod (u / 1e1 ), utod (u % 1e1 ))
248
- case width == 4 && u < 1e4 :
249
- return append (b , utod (u / 1e3 ), utod (u / 1e2 % 1e1 ), utod (u / 1e1 % 1e1 ), utod (u % 1e1 ))
250
- }
251
-
252
- // Compute the number of decimal digits.
253
- var n int
254
- if u == 0 {
255
- n = 1
256
- }
257
- for u2 := u ; u2 > 0 ; u2 /= 10 {
258
- n ++
259
- }
260
-
261
- // Add 0-padding.
262
- for pad := width - n ; pad > 0 ; pad -- {
263
- b = append (b , '0' )
264
- }
265
-
266
- // Ensure capacity.
267
- if len (b )+ n <= cap (b ) {
268
- b = b [:len (b )+ n ]
269
- } else {
270
- b = append (b , make ([]byte , n )... )
271
- }
272
-
273
- // Assemble decimal in reverse order.
274
- i := len (b ) - 1
275
- for u >= 10 && i > 0 {
276
- q := u / 10
277
- b [i ] = utod (u - q * 10 )
278
- u = q
279
- i --
280
- }
281
- b [i ] = utod (u )
282
- return b
283
- }
284
-
285
229
// ZipToFile 压缩至文件
286
230
// @params dst string 压缩文件目标路径
287
231
// @params src string 待压缩源文件/目录路径
0 commit comments