diff --git a/README.md b/README.md index d8cb89e1..a3abd94f 100644 --- a/README.md +++ b/README.md @@ -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` diff --git a/cmd/podsync/main.go b/cmd/podsync/main.go index 9c061e6f..e34f5608 100644 --- a/cmd/podsync/main.go +++ b/cmd/podsync/main.go @@ -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 { @@ -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, @@ -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") diff --git a/go.mod b/go.mod index 253370e7..7d26fe83 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 41b5f158..a2e7b5a1 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/pkg/config/config.go b/pkg/config/config.go index 9aaa7215..eecc9f6b 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -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. @@ -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") } diff --git a/pkg/db/badger.go b/pkg/db/badger.go index efebeddc..daef6d43 100644 --- a/pkg/db/badger.go +++ b/pkg/db/badger.go @@ -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 { diff --git a/pkg/model/defaults.go b/pkg/model/defaults.go index db0a938b..cd9fae96 100644 --- a/pkg/model/defaults.go +++ b/pkg/model/defaults.go @@ -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 )