From 3441b75a582350f3756a101d73dba02e4b7e44a7 Mon Sep 17 00:00:00 2001 From: Nicola Murino Date: Tue, 3 Sep 2019 23:13:33 +0200 Subject: [PATCH] allow empty log file, use the standard output in this case Fixes #34 --- README.md | 10 +++++----- cmd/serve.go | 14 +++++++++----- config/config.go | 1 + logger/logger.go | 34 +++++++++++++++++++++------------- logger/sync_wrapper.go | 17 +++++++++++++++++ 5 files changed, 53 insertions(+), 23 deletions(-) create mode 100644 logger/sync_wrapper.go diff --git a/README.md b/README.md index 770bc04a..f03649a3 100644 --- a/README.md +++ b/README.md @@ -94,11 +94,11 @@ The `serve` subcommand supports the following flags: - `--config-dir` string. Location of the config dir. This directory should contain the `sftpgo` configuration file and is used as the base for files with a relative path (eg. the private keys for the SFTP server, the SQLite or bblot database if you use SQLite or bbolt as data provider). The default value is "." or the value of `SFTPGO_CONFIG_DIR` environment variable. - `--config-file` string. Name of the configuration file. It must be the name of a file stored in config-dir not the absolute path to the configuration file. The specified file name must have no extension we automatically load JSON, YAML, TOML, HCL and Java properties. The default value is "sftpgo" (and therefore `sftpgo.json`, `sftpgo.yaml` and so on are searched) or the value of `SFTPGO_CONFIG_FILE` environment variable. -- `--log-compress` boolean. Determine if the rotated log files should be compressed using gzip. Default `false` or the value of `SFTPGO_LOG_COMPRESS` environment variable (1 or `true`, 0 or `false`). -- `--log-file-path` string. Location for the log file, default "sftpgo.log" or the value of `SFTPGO_LOG_FILE_PATH` environment variable. -- `--log-max-age` int. Maximum number of days to retain old log files. Default 28 or the value of `SFTPGO_LOG_MAX_AGE` environment variable. -- `--log-max-backups` int. Maximum number of old log files to retain. Default 5 or the value of `SFTPGO_LOG_MAX_BACKUPS` environment variable. -- `--log-max-size` int. Maximum size in megabytes of the log file before it gets rotated. Default 10 or the value of `SFTPGO_LOG_MAX_SIZE` environment variable. +- `--log-compress` boolean. Determine if the rotated log files should be compressed using gzip. Default `false` or the value of `SFTPGO_LOG_COMPRESS` environment variable (1 or `true`, 0 or `false`). It is unused if `log-file-path` is empty. +- `--log-file-path` string. Location for the log file, default "sftpgo.log" or the value of `SFTPGO_LOG_FILE_PATH` environment variable. Leave empty to write logs to the standard error. +- `--log-max-age` int. Maximum number of days to retain old log files. Default 28 or the value of `SFTPGO_LOG_MAX_AGE` environment variable. It is unused if `log-file-path` is empty. +- `--log-max-backups` int. Maximum number of old log files to retain. Default 5 or the value of `SFTPGO_LOG_MAX_BACKUPS` environment variable. It is unused if `log-file-path` is empty. +- `--log-max-size` int. Maximum size in megabytes of the log file before it gets rotated. Default 10 or the value of `SFTPGO_LOG_MAX_SIZE` environment variable. It is unused if `log-file-path` is empty. - `--log-verbose` boolean. Enable verbose logs. Default `true` or the value of `SFTPGO_LOG_VERBOSE` environment variable (1 or `true`, 0 or `false`). If you don't configure any private host keys, the daemon will use `id_rsa` in the configuration directory. If that file doesn't exist, the daemon will attempt to autogenerate it (if the user that executes SFTPGo has write access to the config-dir). The server supports any private key format supported by [`crypto/ssh`](https://github.com/golang/crypto/blob/master/ssh/keys.go#L32). diff --git a/cmd/serve.go b/cmd/serve.go index e3ed0d42..b6fb69f5 100644 --- a/cmd/serve.go +++ b/cmd/serve.go @@ -82,32 +82,36 @@ func init() { viper.SetDefault(logFilePathKey, "sftpgo.log") viper.BindEnv(logFilePathKey, "SFTPGO_LOG_FILE_PATH") serveCmd.Flags().StringVarP(&logFilePath, logFilePathFlag, "l", viper.GetString(logFilePathKey), - "Location for the log file. This flag can be set using SFTPGO_LOG_FILE_PATH env var too.") + "Location for the log file. Leave empty to write logs to the standard output. This flag can be set using SFTPGO_LOG_FILE_PATH "+ + "env var too.") viper.BindPFlag(logFilePathKey, serveCmd.Flags().Lookup(logFilePathFlag)) viper.SetDefault(logMaxSizeKey, 10) viper.BindEnv(logMaxSizeKey, "SFTPGO_LOG_MAX_SIZE") serveCmd.Flags().IntVarP(&logMaxSize, logMaxSizeFlag, "s", viper.GetInt(logMaxSizeKey), "Maximum size in megabytes of the log file before it gets rotated. This flag can be set using SFTPGO_LOG_MAX_SIZE "+ - "env var too.") + "env var too. It is unused if log-file-path is empty.") viper.BindPFlag(logMaxSizeKey, serveCmd.Flags().Lookup(logMaxSizeFlag)) viper.SetDefault(logMaxBackupKey, 5) viper.BindEnv(logMaxBackupKey, "SFTPGO_LOG_MAX_BACKUPS") serveCmd.Flags().IntVarP(&logMaxBackups, "log-max-backups", "b", viper.GetInt(logMaxBackupKey), - "Maximum number of old log files to retain. This flag can be set using SFTPGO_LOG_MAX_BACKUPS env var too.") + "Maximum number of old log files to retain. This flag can be set using SFTPGO_LOG_MAX_BACKUPS env var too. "+ + "It is unused if log-file-path is empty.") viper.BindPFlag(logMaxBackupKey, serveCmd.Flags().Lookup(logMaxBackupFlag)) viper.SetDefault(logMaxAgeKey, 28) viper.BindEnv(logMaxAgeKey, "SFTPGO_LOG_MAX_AGE") serveCmd.Flags().IntVarP(&logMaxAge, "log-max-age", "a", viper.GetInt(logMaxAgeKey), - "Maximum number of days to retain old log files. This flag can be set using SFTPGO_LOG_MAX_AGE env var too.") + "Maximum number of days to retain old log files. This flag can be set using SFTPGO_LOG_MAX_AGE env var too. "+ + "It is unused if log-file-path is empty.") viper.BindPFlag(logMaxAgeKey, serveCmd.Flags().Lookup(logMaxAgeFlag)) viper.SetDefault(logCompressKey, false) viper.BindEnv(logCompressKey, "SFTPGO_LOG_COMPRESS") serveCmd.Flags().BoolVarP(&logCompress, logCompressFlag, "z", viper.GetBool(logCompressKey), "Determine if the rotated "+ - "log files should be compressed using gzip. This flag can be set using SFTPGO_LOG_COMPRESS env var too.") + "log files should be compressed using gzip. This flag can be set using SFTPGO_LOG_COMPRESS env var too. "+ + "It is unused if log-file-path is empty.") viper.BindPFlag(logCompressKey, serveCmd.Flags().Lookup(logCompressFlag)) viper.SetDefault(logVerboseKey, true) diff --git a/config/config.go b/config/config.go index 630e2255..a99eeb97 100644 --- a/config/config.go +++ b/config/config.go @@ -86,6 +86,7 @@ func init() { setViperAdditionalConfigPaths() viper.AddConfigPath(".") viper.AutomaticEnv() + viper.AllowEmptyEnv(true) } // GetSFTPDConfig returns the configuration for the SFTP server diff --git a/logger/logger.go b/logger/logger.go index a385d972..9ad4222c 100644 --- a/logger/logger.go +++ b/logger/logger.go @@ -12,6 +12,7 @@ import ( "fmt" "os" "runtime" + "sync" "github.com/rs/zerolog" lumberjack "gopkg.in/natefinch/lumberjack.v2" @@ -34,20 +35,27 @@ func GetLogger() *zerolog.Logger { // 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: logCompress, - }).With().Timestamp().Logger().Level(level) - - consoleOutput := zerolog.ConsoleWriter{ - Out: os.Stdout, - TimeFormat: dateFormat, - NoColor: runtime.GOOS == "windows", + if len(logFilePath) > 0 { + logger = zerolog.New(&lumberjack.Logger{ + Filename: logFilePath, + MaxSize: logMaxSize, + MaxBackups: logMaxBackups, + MaxAge: logMaxAge, + Compress: logCompress, + }) + consoleOutput := zerolog.ConsoleWriter{ + Out: os.Stdout, + TimeFormat: dateFormat, + NoColor: runtime.GOOS == "windows", + } + consoleLogger = zerolog.New(consoleOutput).With().Timestamp().Logger().Level(level) + } else { + logger = zerolog.New(logSyncWrapper{ + output: os.Stdout, + lock: new(sync.Mutex)}) + consoleLogger = zerolog.Nop() } - consoleLogger = zerolog.New(consoleOutput).With().Timestamp().Logger().Level(level) + logger = logger.With().Timestamp().Logger().Level(level) } // Debug logs at debug level for the specified sender diff --git a/logger/sync_wrapper.go b/logger/sync_wrapper.go new file mode 100644 index 00000000..77d7deaf --- /dev/null +++ b/logger/sync_wrapper.go @@ -0,0 +1,17 @@ +package logger + +import ( + "os" + "sync" +) + +type logSyncWrapper struct { + output *os.File + lock *sync.Mutex +} + +func (l logSyncWrapper) Write(b []byte) (n int, err error) { + l.lock.Lock() + defer l.lock.Unlock() + return l.output.Write(b) +}