/
file.go
260 lines (235 loc) · 6.52 KB
/
file.go
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
// Copyright 2017 Keybase, Inc. All rights reserved. Use of
// this source code is governed by the included BSD license.
package logger
import (
"errors"
"fmt"
"os"
"path/filepath"
"regexp"
"sort"
"sync"
"time"
logging "github.com/keybase/go-logging"
)
// LogFileConfig is the config structure for new style log files with rotation.
type LogFileConfig struct {
// Path is the path of the log file to use
Path string
// MaxSize is the size of log file (in bytes) before rotation, 0 for infinite.
MaxSize int64
// MaxAge is the duration before log rotation, zero value for infinite.
MaxAge time.Duration
// MaxKeepFiles is maximum number of log files for this service, older
// files are deleted.
MaxKeepFiles int
}
// SetLogFileConfig sets the log file config to be used globally.
func SetLogFileConfig(lfc *LogFileConfig) error {
globalLock.Lock()
defer globalLock.Unlock()
first := true
var w = currentLogFileWriter
if w != nil {
first = false
w.lock.Lock()
defer w.lock.Unlock()
w.Close()
} else {
w = &logFileWriter{}
}
w.config = *lfc
err := w.Open(time.Now())
if err != nil {
return err
}
if first {
fileBackend := logging.NewLogBackend(w, "", 0)
logging.SetBackend(fileBackend)
stderrIsTerminal = false
currentLogFileWriter = w
}
return nil
}
type logFileWriter struct {
lock sync.Mutex
config LogFileConfig
file *os.File
currentSize int64
currentStart time.Time
}
func (lfw *logFileWriter) Open(at time.Time) error {
var err error
_, lfw.file, err = OpenLogFile(lfw.config.Path)
if err != nil {
return err
}
lfw.currentStart = at
lfw.currentSize = 0
fi, err := lfw.file.Stat()
if err != nil {
return err
}
lfw.currentSize = fi.Size()
tryRedirectStderrTo(lfw.file)
return nil
}
func (lfw *logFileWriter) Close() error {
if lfw == nil {
return nil
}
lfw.lock.Lock()
defer lfw.lock.Unlock()
if lfw.file == nil {
return nil
}
return lfw.file.Close()
}
const zeroDuration time.Duration = 0
const oldLogFileTimeRangeTimeLayout = "20060102T150405Z0700"
const oldLogFileTimeRangeTimeLayoutLegacy = "20060102T150405"
func (lfw *logFileWriter) Write(bs []byte) (int, error) {
lfw.lock.Lock()
defer lfw.lock.Unlock()
n, err := lfw.file.Write(bs)
if err != nil {
return n, err
}
needRotation := false
if lfw.config.MaxSize > 0 {
lfw.currentSize += int64(n)
needRotation = needRotation || lfw.currentSize > lfw.config.MaxSize
}
if lfw.config.MaxAge != zeroDuration {
elapsed := time.Since(lfw.currentStart)
needRotation = needRotation || elapsed > lfw.config.MaxAge
}
if !needRotation {
return n, nil
}
// Close first because some systems don't like to rename otherwise.
lfw.file.Close()
lfw.file = nil
now := time.Now()
start := lfw.currentStart.Format(oldLogFileTimeRangeTimeLayout)
end := now.Format(oldLogFileTimeRangeTimeLayout)
tgt := fmt.Sprintf("%s-%s-%s", lfw.config.Path, start, end)
// Handle the error further down
err = os.Rename(lfw.config.Path, tgt)
if err != nil {
return n, err
}
// Spawn old log deletion worker if we have a max-amount of log-files.
if lfw.config.MaxKeepFiles > 0 {
go deleteOldLogFilesIfNeeded(lfw.config)
}
err = lfw.Open(now)
return n, err
}
func deleteOldLogFilesIfNeeded(config LogFileConfig) {
err := deleteOldLogFilesIfNeededWorker(config)
if err != nil {
log := New("logger")
log.Warning("Deletion of old log files failed: %v", err)
}
}
func deleteOldLogFilesIfNeededWorker(config LogFileConfig) error {
// Returns list of old log files (not the current one) sorted.
// The oldest one is first in the list.
entries, err := scanOldLogFiles(config.Path)
if err != nil {
return err
}
// entries has only the old renamed log files, not the current
// log file. E.g. if MaxKeepFiles is 2 then we keep the current
// file and one archived log file. If there are 3 archived files
// then removeN = 1 + 3 - 2 = 2.
removeN := 1 + len(entries) - config.MaxKeepFiles
if config.MaxKeepFiles <= 0 || removeN <= 0 {
return nil
}
// Try to remove all old log files that we want to remove, and
// don't stop on the first error.
for i := 0; i < removeN; i++ {
err2 := os.Remove(entries[i])
if err == nil {
err = err2
}
}
return err
}
type logFilename struct {
fName string
start time.Time
}
type logFilenamesByTime []logFilename
func (a logFilenamesByTime) Len() int { return len(a) }
func (a logFilenamesByTime) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a logFilenamesByTime) Less(i, j int) bool {
return a[i].start.Before(a[j].start)
}
// getLogFilenamesOrderByTime filters fNames to return only old log files
// starting with baseName, followed by a timestamp-range suffix. It also sorts
// them by start time, in increasing order.
//
// Both baseName and fNames are base names not including dir names.
//
// This function supports both old (no timezone) and current (with timezone)
// format of log file names. TODO: simplify this when we don't care about old
// format any more.
func getLogFilenamesOrderByTime(
baseName string, fNames []string) (names []string, err error) {
re, err := regexp.Compile(`^` + regexp.QuoteMeta(baseName) +
`-(\d{8}T\d{6}(?:(?:[Z\+-]\d{4})|(?:Z))?)-\d{8}T\d{6}(?:(?:[Z\+-]\d{4})|(?:Z))?$`)
if err != nil {
return nil, err
}
var logFilenames []logFilename
for _, fName := range fNames {
match := re.FindStringSubmatch(fName)
if len(match) != 2 {
continue
}
t, err1 := time.ParseInLocation(oldLogFileTimeRangeTimeLayout, match[1], time.Local)
if err1 != nil {
var err2 error
t, err2 = time.ParseInLocation(oldLogFileTimeRangeTimeLayoutLegacy, match[1], time.Local)
if err2 != nil {
return nil, errors.New(err1.Error() + " | " + err2.Error())
}
}
logFilenames = append(logFilenames, logFilename{fName: fName, start: t})
}
sort.Sort(logFilenamesByTime(logFilenames))
names = make([]string, 0, len(logFilenames))
for _, f := range logFilenames {
names = append(names, f.fName)
}
return names, nil
}
// scanOldLogFiles finds old archived log files corresponding to the log file path.
// Returns the list of such log files sorted with the eldest one first.
func scanOldLogFiles(path string) ([]string, error) {
dname, fname := filepath.Split(path)
if dname == "" {
dname = "."
}
dir, err := os.Open(dname)
if err != nil {
return nil, err
}
defer dir.Close()
ns, err := dir.Readdirnames(-1)
if err != nil {
return nil, err
}
names, err := getLogFilenamesOrderByTime(fname, ns)
if err != nil {
return nil, err
}
var res []string
for _, name := range names {
res = append(res, filepath.Join(dname, name))
}
return res, nil
}