-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfile.go
56 lines (48 loc) · 1.58 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
package zslog
import (
"os"
"path/filepath"
"github.com/rs/zerolog"
"gopkg.in/natefinch/lumberjack.v2"
)
type FileConfig struct {
// MaxSize is the maximum size in megabytes of the log file before it gets
// rotated. It defaults to 100 megabytes.
MaxSize int
// MaxAge is the maximum number of days to retain old log files based on the
// timestamp encoded in their filename. Note that a day is defined as 24
// hours and may not exactly correspond to calendar days due to daylight
// savings, leap seconds, etc. The default is not to remove old log files
// based on age.
MaxAge int
// MaxBackups is the maximum number of old log files to retain. The default
// is to retain all old log files (though MaxAge may still cause them to get
// deleted.)
MaxBackups int
// LocalTime determines if the time used for formatting the timestamps in
// backup files is the computer's local time. The default is to use UTC
// time.
LocalTime bool
// Compress determines if the rotated log files should be compressed
// using gzip. The default is not to perform compression.
Compress bool
}
func File(filename string, c FileConfig, l levels) zerolog.LevelWriter {
if err := os.MkdirAll(filepath.Dir(filename), 0744); err != nil {
logger.Error().Err(err).Str("path", filepath.Dir(filename)).Msg("can't create log directory")
return nil
}
file := &lumberjack.Logger{
Filename: filename,
MaxSize: c.MaxSize,
MaxBackups: c.MaxBackups,
MaxAge: c.MaxAge,
Compress: c.Compress,
LocalTime: c.LocalTime,
}
lw := &LevelWriter{
writer: file,
levels: l,
}
return lw
}