-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathoption.go
142 lines (121 loc) · 3.42 KB
/
option.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
package logrotate
import (
"errors"
"os"
"path/filepath"
"time"
)
type period string
const (
// PeriodHourly rotates log every hour
PeriodHourly period = "hourly"
// PeriodDaily rotates log by every day
PeriodDaily period = "daily"
// PeriodWeekly rotates log by every week
PeriodWeekly period = "weekly"
// PeriodMonthly rotates log by every month
PeriodMonthly period = "monthly"
)
var (
DefaultArchiveTimeFormat = "2006-01-02_15:04:05.000"
DefaultMaxArchives = 100
DefaultMaxArchiveDays = 14
)
type Options struct {
// File is the file to write logs to.
// It uses <process name>.log in os.TempDir() if empty.
File string `json:"file" toml:"file" yaml:"file"`
// RotatePeriod is time period for rotate log.
// It supports hourly, daily, weekly, monthly.
RotatePeriod string `json:"rotate_period" toml:"rotate_period" yaml:"rotate_period"`
// RotateSize is the maximum size of the log file before it gets rotated
RotateSize string `json:"rotate_size" toml:"rotate_size" yaml:"rotate_size"`
// MaxArchives is the maximum number of old log files to retain
MaxArchives int `json:"max_archives" toml:"max_archives" yaml:"max_archives"`
// MaxArchiveDays is the maximum number of days to archived files
MaxArchiveDays int `json:"max_archive_days" toml:"max_archive_days" yaml:"max_archive_days"`
// ArchiveTimeFormat is the format of the archived files
ArchiveTimeFormat string `json:"archive_time_format" toml:"archive_time_format" yaml:"archive_time_format"`
// Compress determines if the rotated log files should be compressed
// using gzip. The default is not to perform compression.
Compress bool `json:"compress" toml:"compress" yaml:"compress"`
rotateSize int64
cron string
maxArchiveDuration time.Duration
}
func (o *Options) Apply() error {
if o.RotateSize != "" {
size, err := stringToBytes(o.RotateSize)
if err != nil {
return err
}
o.rotateSize = size
}
if o.RotatePeriod != "" {
switch period(o.RotatePeriod) {
case PeriodHourly:
o.cron = "0 * * * *"
case PeriodDaily:
o.cron = "0 0 * * *"
case PeriodWeekly:
o.cron = "0 0 * * 0"
case PeriodMonthly:
o.cron = "0 0 1 * *"
default:
return errors.New("invalid rotate period")
}
}
if o.RotateSize != "" || o.RotatePeriod != "" {
if o.ArchiveTimeFormat == "" {
o.ArchiveTimeFormat = DefaultArchiveTimeFormat
}
if o.MaxArchives <= 0 {
o.MaxArchives = DefaultMaxArchives
}
if o.MaxArchiveDays <= 0 {
o.MaxArchiveDays = DefaultMaxArchiveDays
}
o.maxArchiveDuration = time.Duration(int64(24*time.Hour) * int64(o.MaxArchiveDays))
}
if o.File == "" {
name := filepath.Base(os.Args[0]) + ".log"
o.File = filepath.Join(os.TempDir(), name)
}
return nil
}
type Option func(*Options)
func File(name string) Option {
return func(opts *Options) {
opts.File = name
}
}
func RotatePeriod(p period) Option {
return func(opts *Options) {
opts.RotatePeriod = string(p)
}
}
func RotateSize(size string) Option {
return func(opts *Options) {
opts.RotateSize = size
}
}
func ArchiveTimeFormat(format string) Option {
return func(opts *Options) {
opts.ArchiveTimeFormat = format
}
}
func MaxArchives(number int) Option {
return func(opts *Options) {
opts.MaxArchives = number
}
}
func MaxArchiveDays(days int) Option {
return func(opts *Options) {
opts.MaxArchiveDays = days
}
}
func Compress(compress bool) Option {
return func(opts *Options) {
opts.Compress = compress
}
}