Skip to content

Commit

Permalink
Support log rotation
Browse files Browse the repository at this point in the history
  • Loading branch information
dop251 committed Apr 17, 2020
1 parent 6a11db7 commit f7d0f0b
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 12 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,15 @@ vimeo = "{VIMEO_API_TOKEN}"

[database]
badger = { truncate = true, file_io = true } # See https://github.com/dgraph-io/badger#memory-usage

# Optional log config. If not specified logs to the stdout
[log]
filename = "podsync.log"
max_size = 50 # MB
max_age = 30 # days
max_backups = 7
compress = true

```

Episodes files will be kept at: `/path/to/data/directory/ID1`, feed will be accessible from: `http://localhost/ID1.xml`
Expand Down
26 changes: 19 additions & 7 deletions cmd/podsync/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import (
"github.com/mxpv/podsync/pkg/db"
"github.com/mxpv/podsync/pkg/fs"
"github.com/mxpv/podsync/pkg/ytdl"

"gopkg.in/natefinch/lumberjack.v2"
)

type Opts struct {
Expand Down Expand Up @@ -72,6 +74,23 @@ func main() {
log.Info(banner)
}

// Load TOML file
log.Debugf("loading configuration %q", opts.ConfigPath)
cfg, err := config.LoadConfig(opts.ConfigPath)
if err != nil {
log.WithError(err).Fatal("failed to load configuration file")
}

if cfg.Log.Filename != "" {
log.SetOutput(&lumberjack.Logger{
Filename: cfg.Log.Filename,
MaxSize: cfg.Log.MaxSize,
MaxBackups: cfg.Log.MaxBackups,
MaxAge: cfg.Log.MaxAge,
Compress: cfg.Log.Compress,
})
}

log.WithFields(log.Fields{
"version": version,
"commit": commit,
Expand All @@ -83,13 +102,6 @@ func main() {
log.WithError(err).Fatal("youtube-dl error")
}

// Load TOML file
log.Debugf("loading configuration %q", opts.ConfigPath)
cfg, err := config.LoadConfig(opts.ConfigPath)
if err != nil {
log.WithError(err).Fatal("failed to load configuration file")
}

database, err := db.NewBadger(&cfg.Database)
if err != nil {
log.WithError(err).Fatal("failed to open database")
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ require (
golang.org/x/sync v0.0.0-20190423024810-112230192c58
google.golang.org/api v0.0.0-20180718221112-efcb5f25ac56
google.golang.org/appengine v1.1.0 // indirect
gopkg.in/natefinch/lumberjack.v2 v2.0.0
)

go 1.13
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,11 @@ google.golang.org/api v0.0.0-20180718221112-efcb5f25ac56 h1:iDRbkenn0VZEo05mHiCt
google.golang.org/api v0.0.0-20180718221112-efcb5f25ac56/go.mod h1:4mhQ8q/RsB7i+udVvVy5NUi08OU8ZlA0gRVgrF7VFY0=
google.golang.org/appengine v1.1.0 h1:igQkv0AAhEIvTEpD5LIpAfav2eeVO9HBTjvKHVJPRSs=
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/natefinch/lumberjack.v2 v2.0.0 h1:1Lc07Kr7qY4U2YPouBjpCLxpiyxIVoxqXgkXLknAOE8=
gopkg.in/natefinch/lumberjack.v2 v2.0.0/go.mod h1:l0ndWWf7gzL7RNwBG7wST/UCcT4T24xpD6X8LsfU/+k=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0=
rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=
27 changes: 27 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,24 @@ type Cleanup struct {
KeepLast int `toml:"keep_last"`
}

type Log struct {
// Filename to write the log to (instead of stdout)
Filename string `toml:"filename"`
// MaxSize is the maximum size of the log file in MB
MaxSize int `toml:"max_size"`
// MaxBackups is the maximum number of log file backups to keep after rotation
MaxBackups int `toml:"max_backups"`
// MaxAge is the maximum number of days to keep the logs for
MaxAge int `toml:"max_age"`
// Compress old backups
Compress bool `toml:"compress"`
}

type Config struct {
// Server is the web server configuration
Server Server `toml:"server"`
// Log is the optional logging configuration
Log Log `toml:"log"`
// Database configuration
Database Database `toml:"database"`
// Feeds is a list of feeds to host by this app.
Expand Down Expand Up @@ -152,6 +167,18 @@ func (c *Config) applyDefaults(configPath string) {
}
}

if c.Log.Filename != "" {
if c.Log.MaxSize == 0 {
c.Log.MaxSize = model.DefaultLogMaxSize
}
if c.Log.MaxAge == 0 {
c.Log.MaxAge = model.DefaultLogMaxAge
}
if c.Log.MaxBackups == 0 {
c.Log.MaxBackups = model.DefaultLogMaxBackups
}
}

if c.Database.Dir == "" {
c.Database.Dir = filepath.Join(filepath.Dir(configPath), "db")
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/db/badger.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func NewBadger(config *config.Database) (*Badger, error) {
}

opts := badger.DefaultOptions(dir).
WithLogger(log.New()).
WithLogger(log.StandardLogger()).
WithTruncate(true)

if config.Badger != nil {
Expand Down
11 changes: 7 additions & 4 deletions pkg/model/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ import (
)

const (
DefaultFormat = FormatVideo
DefaultQuality = QualityHigh
DefaultPageSize = 50
DefaultUpdatePeriod = 6 * time.Hour
DefaultFormat = FormatVideo
DefaultQuality = QualityHigh
DefaultPageSize = 50
DefaultUpdatePeriod = 6 * time.Hour
DefaultLogMaxSize = 50 // megabytes
DefaultLogMaxAge = 30 // days
DefaultLogMaxBackups = 7
)

0 comments on commit f7d0f0b

Please sign in to comment.