Skip to content

Commit f0c7c22

Browse files
committed
update: 文件close
1 parent a9321ad commit f0c7c22

File tree

2 files changed

+19
-26
lines changed

2 files changed

+19
-26
lines changed

Diff for: cleaner.go

-23
This file was deleted.

Diff for: rotatelogs.go

+19-3
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ import (
1414
// Rotate represents a log file that gets
1515
// automatically rotated as you write to it.
1616
type Rotate struct {
17-
filename string // 当前文件名称
1817
clock Clock // 时间
1918
out *os.File // 文件句柄
19+
business *os.File // 文件句柄
2020
mutex *sync.RWMutex // 读写锁
2121
maxAge time.Duration // 最大保存时间
2222
pattern *strftime.Strftime // 时间格式
@@ -48,7 +48,10 @@ func New(p string, options ...Option) (*Rotate, error) {
4848
// automatically rotated, and also purged if necessary.
4949
func (r *Rotate) Write(bytes []byte) (n int, err error) {
5050
r.mutex.Lock() // Guard against concurrent writes
51-
defer r.mutex.Unlock()
51+
defer func() {
52+
r.mutex.Unlock()
53+
r.Close()
54+
}()
5255
var out io.Writer
5356
if strings.Contains(string(bytes), "business") {
5457
var compile *regexp.Regexp
@@ -101,6 +104,7 @@ func (r *Rotate) getBusinessWriter(business string) (io.Writer, error) {
101104
if err != nil {
102105
return nil, err
103106
}
107+
r.business = out
104108
return out, nil
105109
}
106110

@@ -113,6 +117,18 @@ func (r *Rotate) getWriter() (io.Writer, error) {
113117
}
114118
_ = r.out.Close()
115119
r.out = out
116-
r.filename = filename
117120
return out, nil
118121
}
122+
123+
func (r *Rotate) Close() {
124+
r.mutex.Lock() // Guard against concurrent writes
125+
defer r.mutex.Unlock()
126+
if r.out != nil {
127+
_ = r.out.Close()
128+
r.out = nil
129+
}
130+
if r.business != nil {
131+
_ = r.business.Close()
132+
r.business = nil
133+
}
134+
}

0 commit comments

Comments
 (0)