From 2a8ab620f314793e7682cd8ee03db05fd264b7c2 Mon Sep 17 00:00:00 2001 From: Nicola Murino Date: Wed, 31 Jul 2019 14:06:55 +0200 Subject: [PATCH] improve logger log file size, rotation policy and compression are now configurable --- README.md | 5 +++++ logger/logger.go | 13 ++++--------- main.go | 20 +++++++++++++++++--- 3 files changed, 26 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index da813496..b192d472 100644 --- a/README.md +++ b/README.md @@ -51,6 +51,11 @@ The `sftpgo` executable supports the following command line flags: - `-config-dir` string. Location of the config dir. This directory should contain the `sftpgo.conf` configuration file, the private key for the SFTP server (`id_rsa` file) and the SQLite database if you use SQLite as data provider. The server private key will be autogenerated if the user that executes SFTPGo has write access to the config-dir. The default value is "." - `-log-file-path` string. Location for the log file, default "sftpgo.log" +- `-log-max-size` int. Maximum size in megabytes of the log file before it gets rotated. Default 10 +- `-log-max-backups` int. Maximum number of old log files to retain. Default 5 +- `-log-max-age` int. Maximum number of days to retain old log files. Default 28 +- `-log-compress` boolean. Determine if the rotated log files should be compressed using gzip +- `-log-verbose` boolean. Enable verbose logs. Default `true` Before starting `sftpgo` a dataprovider must be configured. diff --git a/logger/logger.go b/logger/logger.go index f7e039f8..09b9e8c6 100644 --- a/logger/logger.go +++ b/logger/logger.go @@ -30,20 +30,15 @@ func GetLogger() *zerolog.Logger { return &logger } -// InitLogger configures the logger. -// It sets the log file path and the log level -func InitLogger(logFilePath string, level zerolog.Level) { - logMaxSize := 10 // MB - logMaxBackups := 5 - logMaxAge := 28 // days - +// InitLogger configures the logger using the given parameters +func InitLogger(logFilePath string, logMaxSize int, logMaxBackups int, logMaxAge int, logCompress bool, level zerolog.Level) { zerolog.TimeFieldFormat = dateFormat logger = zerolog.New(&lumberjack.Logger{ Filename: logFilePath, MaxSize: logMaxSize, MaxBackups: logMaxBackups, MaxAge: logMaxAge, - Compress: false, + Compress: logCompress, }).With().Timestamp().Logger().Level(level) consoleOutput := zerolog.ConsoleWriter{ @@ -51,7 +46,7 @@ func InitLogger(logFilePath string, level zerolog.Level) { TimeFormat: dateFormat, NoColor: true, } - consoleLogger = zerolog.New(consoleOutput).With().Timestamp().Logger() + consoleLogger = zerolog.New(consoleOutput).With().Timestamp().Logger().Level(level) } // Debug logs at debug level for the specified sender diff --git a/main.go b/main.go index 949fc93f..f5c4d6c4 100644 --- a/main.go +++ b/main.go @@ -28,17 +28,31 @@ func main() { confName := "sftpgo.conf" logSender := "main" var ( - configDir string - logFilePath string + configDir string + logFilePath string + logMaxSize int + logMaxBackups int + logMaxAge int + logCompress bool + logVerbose bool ) flag.StringVar(&configDir, "config-dir", ".", "Location for SFTPGo config dir. It must contain sftpgo.conf, "+ "the private key for the SFTP server (id_rsa file) and the SQLite database if you use SQLite as data provider. "+ "The server private key will be autogenerated if the user that executes SFTPGo has write access to the config-dir") flag.StringVar(&logFilePath, "log-file-path", "sftpgo.log", "Location for the log file") + flag.IntVar(&logMaxSize, "log-max-size", 10, "Maximum size in megabytes of the log file before it gets rotated.") + flag.IntVar(&logMaxBackups, "log-max-backups", 5, "Maximum number of old log files to retain") + flag.IntVar(&logMaxAge, "log-max-age", 28, "Maximum number of days to retain old log files") + flag.BoolVar(&logCompress, "log-compress", false, "Determine if the rotated log files should be compressed using gzip") + flag.BoolVar(&logVerbose, "log-verbose", true, "Enable verbose logs") flag.Parse() configFilePath := filepath.Join(configDir, confName) - logger.InitLogger(logFilePath, zerolog.DebugLevel) + logLevel := zerolog.DebugLevel + if !logVerbose { + logLevel = zerolog.InfoLevel + } + logger.InitLogger(logFilePath, logMaxSize, logMaxBackups, logMaxAge, logCompress, logLevel) logger.Info(logSender, "starting SFTPGo, config dir: %v", configDir) config.LoadConfig(configFilePath) providerConf := config.GetProviderConf()