mirror of
https://github.com/drakkan/sftpgo.git
synced 2024-11-25 09:00:27 +00:00
parent
cc2ccb3dd7
commit
3441b75a58
5 changed files with 53 additions and 23 deletions
10
README.md
10
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-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.
|
- `--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-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.
|
- `--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.
|
- `--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.
|
- `--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.
|
- `--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`).
|
- `--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).
|
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).
|
||||||
|
|
14
cmd/serve.go
14
cmd/serve.go
|
@ -82,32 +82,36 @@ func init() {
|
||||||
viper.SetDefault(logFilePathKey, "sftpgo.log")
|
viper.SetDefault(logFilePathKey, "sftpgo.log")
|
||||||
viper.BindEnv(logFilePathKey, "SFTPGO_LOG_FILE_PATH")
|
viper.BindEnv(logFilePathKey, "SFTPGO_LOG_FILE_PATH")
|
||||||
serveCmd.Flags().StringVarP(&logFilePath, logFilePathFlag, "l", viper.GetString(logFilePathKey),
|
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.BindPFlag(logFilePathKey, serveCmd.Flags().Lookup(logFilePathFlag))
|
||||||
|
|
||||||
viper.SetDefault(logMaxSizeKey, 10)
|
viper.SetDefault(logMaxSizeKey, 10)
|
||||||
viper.BindEnv(logMaxSizeKey, "SFTPGO_LOG_MAX_SIZE")
|
viper.BindEnv(logMaxSizeKey, "SFTPGO_LOG_MAX_SIZE")
|
||||||
serveCmd.Flags().IntVarP(&logMaxSize, logMaxSizeFlag, "s", viper.GetInt(logMaxSizeKey),
|
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 "+
|
"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.BindPFlag(logMaxSizeKey, serveCmd.Flags().Lookup(logMaxSizeFlag))
|
||||||
|
|
||||||
viper.SetDefault(logMaxBackupKey, 5)
|
viper.SetDefault(logMaxBackupKey, 5)
|
||||||
viper.BindEnv(logMaxBackupKey, "SFTPGO_LOG_MAX_BACKUPS")
|
viper.BindEnv(logMaxBackupKey, "SFTPGO_LOG_MAX_BACKUPS")
|
||||||
serveCmd.Flags().IntVarP(&logMaxBackups, "log-max-backups", "b", viper.GetInt(logMaxBackupKey),
|
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.BindPFlag(logMaxBackupKey, serveCmd.Flags().Lookup(logMaxBackupFlag))
|
||||||
|
|
||||||
viper.SetDefault(logMaxAgeKey, 28)
|
viper.SetDefault(logMaxAgeKey, 28)
|
||||||
viper.BindEnv(logMaxAgeKey, "SFTPGO_LOG_MAX_AGE")
|
viper.BindEnv(logMaxAgeKey, "SFTPGO_LOG_MAX_AGE")
|
||||||
serveCmd.Flags().IntVarP(&logMaxAge, "log-max-age", "a", viper.GetInt(logMaxAgeKey),
|
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.BindPFlag(logMaxAgeKey, serveCmd.Flags().Lookup(logMaxAgeFlag))
|
||||||
|
|
||||||
viper.SetDefault(logCompressKey, false)
|
viper.SetDefault(logCompressKey, false)
|
||||||
viper.BindEnv(logCompressKey, "SFTPGO_LOG_COMPRESS")
|
viper.BindEnv(logCompressKey, "SFTPGO_LOG_COMPRESS")
|
||||||
serveCmd.Flags().BoolVarP(&logCompress, logCompressFlag, "z", viper.GetBool(logCompressKey), "Determine if the rotated "+
|
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.BindPFlag(logCompressKey, serveCmd.Flags().Lookup(logCompressFlag))
|
||||||
|
|
||||||
viper.SetDefault(logVerboseKey, true)
|
viper.SetDefault(logVerboseKey, true)
|
||||||
|
|
|
@ -86,6 +86,7 @@ func init() {
|
||||||
setViperAdditionalConfigPaths()
|
setViperAdditionalConfigPaths()
|
||||||
viper.AddConfigPath(".")
|
viper.AddConfigPath(".")
|
||||||
viper.AutomaticEnv()
|
viper.AutomaticEnv()
|
||||||
|
viper.AllowEmptyEnv(true)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetSFTPDConfig returns the configuration for the SFTP server
|
// GetSFTPDConfig returns the configuration for the SFTP server
|
||||||
|
|
|
@ -12,6 +12,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"runtime"
|
"runtime"
|
||||||
|
"sync"
|
||||||
|
|
||||||
"github.com/rs/zerolog"
|
"github.com/rs/zerolog"
|
||||||
lumberjack "gopkg.in/natefinch/lumberjack.v2"
|
lumberjack "gopkg.in/natefinch/lumberjack.v2"
|
||||||
|
@ -34,20 +35,27 @@ func GetLogger() *zerolog.Logger {
|
||||||
// InitLogger configures the logger using the given parameters
|
// InitLogger configures the logger using the given parameters
|
||||||
func InitLogger(logFilePath string, logMaxSize int, logMaxBackups int, logMaxAge int, logCompress bool, level zerolog.Level) {
|
func InitLogger(logFilePath string, logMaxSize int, logMaxBackups int, logMaxAge int, logCompress bool, level zerolog.Level) {
|
||||||
zerolog.TimeFieldFormat = dateFormat
|
zerolog.TimeFieldFormat = dateFormat
|
||||||
logger = zerolog.New(&lumberjack.Logger{
|
if len(logFilePath) > 0 {
|
||||||
Filename: logFilePath,
|
logger = zerolog.New(&lumberjack.Logger{
|
||||||
MaxSize: logMaxSize,
|
Filename: logFilePath,
|
||||||
MaxBackups: logMaxBackups,
|
MaxSize: logMaxSize,
|
||||||
MaxAge: logMaxAge,
|
MaxBackups: logMaxBackups,
|
||||||
Compress: logCompress,
|
MaxAge: logMaxAge,
|
||||||
}).With().Timestamp().Logger().Level(level)
|
Compress: logCompress,
|
||||||
|
})
|
||||||
consoleOutput := zerolog.ConsoleWriter{
|
consoleOutput := zerolog.ConsoleWriter{
|
||||||
Out: os.Stdout,
|
Out: os.Stdout,
|
||||||
TimeFormat: dateFormat,
|
TimeFormat: dateFormat,
|
||||||
NoColor: runtime.GOOS == "windows",
|
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
|
// Debug logs at debug level for the specified sender
|
||||||
|
|
17
logger/sync_wrapper.go
Normal file
17
logger/sync_wrapper.go
Normal file
|
@ -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)
|
||||||
|
}
|
Loading…
Reference in a new issue