forked from cockroachdb/cockroach
/
file.go
438 lines (386 loc) · 12.5 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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
// Copyright 2013 Google Inc. All Rights Reserved.
//
// Go support for leveled logs, analogous to https://code.google.com/p/google-clog/
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// File I/O for logs.
// Author: Bram Gruneir (bram@cockroachlabs.com)
package log
import (
"flag"
"fmt"
"io"
"io/ioutil"
"os"
"os/user"
"path/filepath"
"regexp"
"sort"
"strconv"
"strings"
"time"
"github.com/cockroachdb/cockroach/pkg/util/syncutil"
"github.com/pkg/errors"
)
// MaxSize is the maximum size of a log file in bytes.
var MaxSize uint64 = 1024 * 1024 * 10
// If non-empty, overrides the choice of directory in which to write logs. See
// createLogDirs for the full list of possible destinations. Note that the
// default is to log to stderr independent of this setting. See --logtostderr.
type logDirName struct {
syncutil.Mutex
name string
}
var _ flag.Value = &logDirName{}
var logDir logDirName
// Set implements the flag.Value interface.
func (l *logDirName) Set(dir string) error {
l.Lock()
defer l.Unlock()
l.name = dir
return nil
}
// Type implements the flag.Value interface.
func (l *logDirName) Type() string {
return "string"
}
// String implements the flag.Value interface.
func (l *logDirName) String() string {
l.Lock()
defer l.Unlock()
return l.name
}
func (l *logDirName) clear() {
// For testing only.
l.Lock()
defer l.Unlock()
l.name = ""
}
func (l *logDirName) get() (string, error) {
l.Lock()
defer l.Unlock()
if len(l.name) == 0 {
return "", errDirectoryNotSet
}
return l.name, nil
}
func (l *logDirName) isSet() bool {
l.Lock()
res := l.name != ""
l.Unlock()
return res
}
// DirSet returns true of the log directory has been changed from its default.
func DirSet() bool { return logDir.isSet() }
// logFileRE matches log files to avoid exposing non-log files accidentally
// and it splits the details of the filename into groups for easy parsing.
// The log file format is {process}.{host}.{username}.{timestamp}.{pid}.{severity}.log
// cockroach.Brams-MacBook-Pro.bram.2015-06-09T16-10-48Z.30209.WARNING.log
// All underscore in process, host and username are escaped to double
// underscores and all periods are escaped to an underscore.
// For compatibility with Windows filenames, all colons from the timestamp
// (RFC3339) are converted from underscores.
var logFileRE = regexp.MustCompile(`([^\.]+)\.([^\.]+)\.([^\.]+)\.([^\.]+)\.(\d+)\.(ERROR|WARNING|INFO)\.log`)
var (
pid = os.Getpid()
program = filepath.Base(os.Args[0])
host = "unknownhost"
userName = "unknownuser"
)
func init() {
h, err := os.Hostname()
if err == nil {
host = shortHostname(h)
}
current, err := user.Current()
if err == nil {
userName = current.Username
}
// Sanitize userName since it may contain filepath separators on Windows.
userName = strings.Replace(userName, `\`, "_", -1)
}
// shortHostname returns its argument, truncating at the first period.
// For instance, given "www.google.com" it returns "www".
func shortHostname(hostname string) string {
if i := strings.Index(hostname, "."); i >= 0 {
return hostname[:i]
}
return hostname
}
// removePeriods removes all extraneous periods. This is required to ensure that
// the only periods in the filename are the ones added by logName so it can
// be easily parsed.
func removePeriods(s string) string {
return strings.Replace(s, ".", "", -1)
}
// logName returns a new log file name containing the severity, with start time
// t, and the name for the symlink for the severity.
func logName(severity Severity, t time.Time) (name, link string) {
// Replace the ':'s in the time format with '_'s to allow for log files in
// Windows.
tFormatted := strings.Replace(t.Format(time.RFC3339), ":", "_", -1)
name = fmt.Sprintf("%s.%s.%s.%s.%06d.%s.log",
removePeriods(program),
removePeriods(host),
removePeriods(userName),
tFormatted,
pid,
severity.Name())
return name, removePeriods(program) + "." + severity.Name()
}
var errMalformedName = errors.New("malformed log filename")
var errMalformedSev = errors.New("malformed severity")
func parseLogFilename(filename string) (FileDetails, error) {
matches := logFileRE.FindStringSubmatch(filename)
if matches == nil || len(matches) != 7 {
return FileDetails{}, errMalformedName
}
// Replace the '_'s with ':'s to restore the correct time format.
fixTime := strings.Replace(matches[4], "_", ":", -1)
time, err := time.Parse(time.RFC3339, fixTime)
if err != nil {
return FileDetails{}, err
}
pid, err := strconv.ParseInt(matches[5], 10, 0)
if err != nil {
return FileDetails{}, err
}
sev, sevFound := SeverityByName(matches[6])
if !sevFound {
return FileDetails{}, errMalformedSev
}
return FileDetails{
Program: matches[1],
Host: matches[2],
UserName: matches[3],
Severity: sev,
Time: time.UnixNano(),
PID: pid,
}, nil
}
var errDirectoryNotSet = errors.New("log: log directory not set")
// create creates a new log file and returns the file and its filename, which
// contains severity ("INFO", "FATAL", etc.) and t. If the file is created
// successfully, create also attempts to update the symlink for that tag, ignoring
// errors.
func create(
severity Severity, t time.Time, lastRotation int64,
) (f *os.File, updatedRotation int64, filename string, err error) {
dir, err := logDir.get()
if err != nil {
return nil, lastRotation, "", err
}
// Ensure that the timestamp of the new file name is greater than
// the timestamp of the previous generated file name.
unix := t.Unix()
if unix <= lastRotation {
unix = lastRotation + 1
}
updatedRotation = unix
t = time.Unix(unix, 0)
// Generate the file name.
name, link := logName(severity, t)
fname := filepath.Join(dir, name)
// Open the file os.O_APPEND|os.O_CREATE rather than use os.Create.
// Append is almost always more efficient than O_RDRW on most modern file systems.
f, err = os.OpenFile(fname, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0664)
if err == nil {
symlink := filepath.Join(dir, link)
_ = os.Remove(symlink) // ignore err
err = os.Symlink(fname, symlink)
}
return f, updatedRotation, fname, errors.Wrapf(err, "log: cannot create log")
}
var errNotAFile = errors.New("not a regular file")
// getFileDetails verifies that the file specified by filename is a
// regular file and filename matches the expected filename pattern.
// Returns the log file details success; otherwise error.
func getFileDetails(info os.FileInfo) (FileDetails, error) {
if info.Mode()&os.ModeType != 0 {
return FileDetails{}, errNotAFile
}
details, err := parseLogFilename(info.Name())
if err != nil {
return FileDetails{}, err
}
return details, nil
}
func verifyFile(filename string) error {
info, err := os.Stat(filename)
if err != nil {
return err
}
_, err = getFileDetails(info)
return err
}
// ListLogFiles returns a slice of FileInfo structs for each log file
// on the local node, in any of the configured log directories.
func ListLogFiles() ([]FileInfo, error) {
var results []FileInfo
dir, err := logDir.get()
if err != nil {
// No log directory configured: simply indicate that there are no
// log files.
return nil, nil
}
infos, err := ioutil.ReadDir(dir)
if err != nil {
return results, err
}
for _, info := range infos {
details, err := getFileDetails(info)
if err == nil {
results = append(results, FileInfo{
Name: info.Name(),
SizeBytes: info.Size(),
ModTimeNanos: info.ModTime().UnixNano(),
Details: details,
})
}
}
return results, nil
}
// GetLogReader returns a reader for the specified filename. In
// restricted mode, the filename must be the base name of a file in
// this process's log directory (this is safe for cases when the
// filename comes from external sources, such as the admin UI via
// HTTP). In unrestricted mode any path is allowed, with the added
// feature that relative paths will be searched in both the current
// directory and this process's log directory.
func GetLogReader(filename string, restricted bool) (io.ReadCloser, error) {
// Verify there are no path separators in a restricted-mode pathname.
if restricted && filepath.Base(filename) != filename {
return nil, errors.Errorf("pathnames must be basenames only: %s", filename)
}
if !filepath.IsAbs(filename) {
dir, err := logDir.get()
if err != nil {
return nil, err
}
filename = filepath.Join(dir, filename)
}
if !restricted {
var err error
filename, err = filepath.EvalSymlinks(filename)
if err != nil {
return nil, err
}
}
if err := verifyFile(filename); err != nil {
return nil, err
}
return os.Open(filename)
}
// sortableFileInfoSlice is required so we can sort FileInfos.
type sortableFileInfoSlice []FileInfo
func (a sortableFileInfoSlice) Len() int { return len(a) }
func (a sortableFileInfoSlice) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a sortableFileInfoSlice) Less(i, j int) bool {
return a[i].Details.Time < a[j].Details.Time
}
// selectFiles selects all log files that have an timestamp before the endTime and
// the correct severity. It then sorts them in decreasing order, with the most
// recent as the first one.
func selectFiles(logFiles []FileInfo, severity Severity, endTimestamp int64) []FileInfo {
files := sortableFileInfoSlice{}
for _, logFile := range logFiles {
if logFile.Details.Severity == severity && logFile.Details.Time <= endTimestamp {
files = append(files, logFile)
}
}
// Sort the files in reverse order so we will fetch the newest first.
sort.Sort(sort.Reverse(files))
return files
}
// FetchEntriesFromFiles fetches all available log entries on disk that match
// the log 'severity' (or worse) and are between the 'startTimestamp' and
// 'endTimestamp'. It will stop reading new files if the number of entries
// exceeds 'maxEntries'. Log entries are further filtered by the regexp
// 'pattern' if provided. The logs entries are returned in reverse chronological
// order.
func FetchEntriesFromFiles(
severity Severity, startTimestamp, endTimestamp int64, maxEntries int, pattern *regexp.Regexp,
) ([]Entry, error) {
logFiles, err := ListLogFiles()
if err != nil {
return nil, err
}
selectedFiles := selectFiles(logFiles, severity, endTimestamp)
entries := []Entry{}
for _, file := range selectedFiles {
newEntries, entryBeforeStart, err := readAllEntriesFromFile(
file,
startTimestamp,
endTimestamp,
maxEntries-len(entries),
pattern)
if err != nil {
return nil, err
}
entries = append(entries, newEntries...)
if len(entries) >= maxEntries {
break
}
if entryBeforeStart {
// Stop processing files that won't have any timestamps after
// startTime.
break
}
}
return entries, nil
}
// readAllEntriesFromFile reads in all log entries from a given file that are
// between the 'startTimestamp' and 'endTimestamp' and match the 'pattern' if it
// exists. It returns the entries in the reverse chronological order. It also
// returns a flag that denotes if any timestamp occurred before the
// 'startTimestamp' to inform the caller that no more log files need to be
// processed. If the number of entries returned exceeds 'maxEntries' then
// processing of new entries is stopped immediately.
func readAllEntriesFromFile(
file FileInfo, startTimestamp, endTimestamp int64, maxEntries int, pattern *regexp.Regexp,
) ([]Entry, bool, error) {
reader, err := GetLogReader(file.Name, true /* restricted */)
if reader == nil || err != nil {
return nil, false, err
}
defer reader.Close()
entries := []Entry{}
decoder := NewEntryDecoder(reader)
entryBeforeStart := false
for {
entry := Entry{}
if err := decoder.Decode(&entry); err != nil {
if err == io.EOF {
break
}
return nil, false, err
}
var match bool
if pattern == nil {
match = true
} else {
match = pattern.MatchString(entry.Message) ||
pattern.MatchString(entry.File)
}
if match && entry.Time >= startTimestamp && entry.Time <= endTimestamp {
entries = append([]Entry{entry}, entries...)
if len(entries) >= maxEntries {
break
}
}
if entry.Time < startTimestamp {
entryBeforeStart = true
}
}
return entries, entryBeforeStart, nil
}