Skip to content

Commit a623e76

Browse files
authored
Merge pull request #126 from dop251/logrotate-pr
Support log rotation
2 parents 6a11db7 + f7d0f0b commit a623e76

File tree

7 files changed

+68
-12
lines changed

7 files changed

+68
-12
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,15 @@ vimeo = "{VIMEO_API_TOKEN}"
7777

7878
[database]
7979
badger = { truncate = true, file_io = true } # See https://github.com/dgraph-io/badger#memory-usage
80+
81+
# Optional log config. If not specified logs to the stdout
82+
[log]
83+
filename = "podsync.log"
84+
max_size = 50 # MB
85+
max_age = 30 # days
86+
max_backups = 7
87+
compress = true
88+
8089
```
8190

8291
Episodes files will be kept at: `/path/to/data/directory/ID1`, feed will be accessible from: `http://localhost/ID1.xml`

cmd/podsync/main.go

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import (
1818
"github.com/mxpv/podsync/pkg/db"
1919
"github.com/mxpv/podsync/pkg/fs"
2020
"github.com/mxpv/podsync/pkg/ytdl"
21+
22+
"gopkg.in/natefinch/lumberjack.v2"
2123
)
2224

2325
type Opts struct {
@@ -72,6 +74,23 @@ func main() {
7274
log.Info(banner)
7375
}
7476

77+
// Load TOML file
78+
log.Debugf("loading configuration %q", opts.ConfigPath)
79+
cfg, err := config.LoadConfig(opts.ConfigPath)
80+
if err != nil {
81+
log.WithError(err).Fatal("failed to load configuration file")
82+
}
83+
84+
if cfg.Log.Filename != "" {
85+
log.SetOutput(&lumberjack.Logger{
86+
Filename: cfg.Log.Filename,
87+
MaxSize: cfg.Log.MaxSize,
88+
MaxBackups: cfg.Log.MaxBackups,
89+
MaxAge: cfg.Log.MaxAge,
90+
Compress: cfg.Log.Compress,
91+
})
92+
}
93+
7594
log.WithFields(log.Fields{
7695
"version": version,
7796
"commit": commit,
@@ -83,13 +102,6 @@ func main() {
83102
log.WithError(err).Fatal("youtube-dl error")
84103
}
85104

86-
// Load TOML file
87-
log.Debugf("loading configuration %q", opts.ConfigPath)
88-
cfg, err := config.LoadConfig(opts.ConfigPath)
89-
if err != nil {
90-
log.WithError(err).Fatal("failed to load configuration file")
91-
}
92-
93105
database, err := db.NewBadger(&cfg.Database)
94106
if err != nil {
95107
log.WithError(err).Fatal("failed to open database")

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ require (
1919
golang.org/x/sync v0.0.0-20190423024810-112230192c58
2020
google.golang.org/api v0.0.0-20180718221112-efcb5f25ac56
2121
google.golang.org/appengine v1.1.0 // indirect
22+
gopkg.in/natefinch/lumberjack.v2 v2.0.0
2223
)
2324

2425
go 1.13

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,11 @@ google.golang.org/api v0.0.0-20180718221112-efcb5f25ac56 h1:iDRbkenn0VZEo05mHiCt
9191
google.golang.org/api v0.0.0-20180718221112-efcb5f25ac56/go.mod h1:4mhQ8q/RsB7i+udVvVy5NUi08OU8ZlA0gRVgrF7VFY0=
9292
google.golang.org/appengine v1.1.0 h1:igQkv0AAhEIvTEpD5LIpAfav2eeVO9HBTjvKHVJPRSs=
9393
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
94+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
9495
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
96+
gopkg.in/natefinch/lumberjack.v2 v2.0.0 h1:1Lc07Kr7qY4U2YPouBjpCLxpiyxIVoxqXgkXLknAOE8=
97+
gopkg.in/natefinch/lumberjack.v2 v2.0.0/go.mod h1:l0ndWWf7gzL7RNwBG7wST/UCcT4T24xpD6X8LsfU/+k=
98+
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
9599
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
96100
rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0=
97101
rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=

pkg/config/config.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,24 @@ type Cleanup struct {
9090
KeepLast int `toml:"keep_last"`
9191
}
9292

93+
type Log struct {
94+
// Filename to write the log to (instead of stdout)
95+
Filename string `toml:"filename"`
96+
// MaxSize is the maximum size of the log file in MB
97+
MaxSize int `toml:"max_size"`
98+
// MaxBackups is the maximum number of log file backups to keep after rotation
99+
MaxBackups int `toml:"max_backups"`
100+
// MaxAge is the maximum number of days to keep the logs for
101+
MaxAge int `toml:"max_age"`
102+
// Compress old backups
103+
Compress bool `toml:"compress"`
104+
}
105+
93106
type Config struct {
94107
// Server is the web server configuration
95108
Server Server `toml:"server"`
109+
// Log is the optional logging configuration
110+
Log Log `toml:"log"`
96111
// Database configuration
97112
Database Database `toml:"database"`
98113
// Feeds is a list of feeds to host by this app.
@@ -152,6 +167,18 @@ func (c *Config) applyDefaults(configPath string) {
152167
}
153168
}
154169

170+
if c.Log.Filename != "" {
171+
if c.Log.MaxSize == 0 {
172+
c.Log.MaxSize = model.DefaultLogMaxSize
173+
}
174+
if c.Log.MaxAge == 0 {
175+
c.Log.MaxAge = model.DefaultLogMaxAge
176+
}
177+
if c.Log.MaxBackups == 0 {
178+
c.Log.MaxBackups = model.DefaultLogMaxBackups
179+
}
180+
}
181+
155182
if c.Database.Dir == "" {
156183
c.Database.Dir = filepath.Join(filepath.Dir(configPath), "db")
157184
}

pkg/db/badger.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func NewBadger(config *config.Database) (*Badger, error) {
4242
}
4343

4444
opts := badger.DefaultOptions(dir).
45-
WithLogger(log.New()).
45+
WithLogger(log.StandardLogger()).
4646
WithTruncate(true)
4747

4848
if config.Badger != nil {

pkg/model/defaults.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@ import (
55
)
66

77
const (
8-
DefaultFormat = FormatVideo
9-
DefaultQuality = QualityHigh
10-
DefaultPageSize = 50
11-
DefaultUpdatePeriod = 6 * time.Hour
8+
DefaultFormat = FormatVideo
9+
DefaultQuality = QualityHigh
10+
DefaultPageSize = 50
11+
DefaultUpdatePeriod = 6 * time.Hour
12+
DefaultLogMaxSize = 50 // megabytes
13+
DefaultLogMaxAge = 30 // days
14+
DefaultLogMaxBackups = 7
1215
)

0 commit comments

Comments
 (0)