ソースを参照

improve logger

log file size, rotation policy and compression are now configurable
Nicola Murino 6 年 前
コミット
2a8ab620f3
3 ファイル変更26 行追加12 行削除
  1. 5 0
      README.md
  2. 4 9
      logger/logger.go
  3. 17 3
      main.go

+ 5 - 0
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.
 

+ 4 - 9
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

+ 17 - 3
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()