mirror of
https://github.com/drakkan/sftpgo.git
synced 2025-11-23 22:04:50 +02:00
Print initialization errors to standard output
This way debugging configuration and initialization issues is simpler
This commit is contained in:
@@ -88,12 +88,14 @@ func LoadConfig(configPath string) error {
|
|||||||
file, err := os.Open(configPath)
|
file, err := os.Open(configPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Warn(logSender, "error loading configuration file: %v. Default configuration will be used: %+v", err, globalConf)
|
logger.Warn(logSender, "error loading configuration file: %v. Default configuration will be used: %+v", err, globalConf)
|
||||||
|
logger.WarnToConsole("error loading configuration file: %v. Default configuration will be used.", err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer file.Close()
|
defer file.Close()
|
||||||
err = json.NewDecoder(file).Decode(&globalConf)
|
err = json.NewDecoder(file).Decode(&globalConf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Warn(logSender, "error parsing config file: %v. Default configuration will be used: %+v", err, globalConf)
|
logger.Warn(logSender, "error parsing configuration file: %v. Default configuration will be used: %+v", err, globalConf)
|
||||||
|
logger.WarnToConsole("error parsing configuration file: %v. Default configuration will be used.", err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if strings.TrimSpace(globalConf.SFTPD.Banner) == "" {
|
if strings.TrimSpace(globalConf.SFTPD.Banner) == "" {
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
// Package logger provides logging capabilities.
|
// Package logger provides logging capabilities.
|
||||||
// It is a wrapper around zerolog for logging and lumberjack for log rotation.
|
// It is a wrapper around zerolog for logging and lumberjack for log rotation.
|
||||||
// It provides a request logger to log the HTTP requests for REST API too.
|
// Logs are written to the specified log file.
|
||||||
|
// Logging on the console is provided to print initialization info, errors and warnings.
|
||||||
|
// The package provides a request logger to log the HTTP requests for REST API too.
|
||||||
// The request logger uses chi.middleware.RequestLogger,
|
// The request logger uses chi.middleware.RequestLogger,
|
||||||
// chi.middleware.LogFormatter and chi.middleware.LogEntry to build a structured
|
// chi.middleware.LogFormatter and chi.middleware.LogEntry to build a structured
|
||||||
// logger using zerlog
|
// logger using zerlog
|
||||||
@@ -8,6 +10,7 @@ package logger
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"os"
|
||||||
|
|
||||||
"github.com/rs/zerolog"
|
"github.com/rs/zerolog"
|
||||||
lumberjack "gopkg.in/natefinch/lumberjack.v2"
|
lumberjack "gopkg.in/natefinch/lumberjack.v2"
|
||||||
@@ -18,7 +21,8 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
logger zerolog.Logger
|
logger zerolog.Logger
|
||||||
|
consoleLogger zerolog.Logger
|
||||||
)
|
)
|
||||||
|
|
||||||
// GetLogger get the configured logger instance
|
// GetLogger get the configured logger instance
|
||||||
@@ -41,6 +45,13 @@ func InitLogger(logFilePath string, level zerolog.Level) {
|
|||||||
MaxAge: logMaxAge,
|
MaxAge: logMaxAge,
|
||||||
Compress: false,
|
Compress: false,
|
||||||
}).With().Timestamp().Logger().Level(level)
|
}).With().Timestamp().Logger().Level(level)
|
||||||
|
|
||||||
|
consoleOutput := zerolog.ConsoleWriter{
|
||||||
|
Out: os.Stdout,
|
||||||
|
TimeFormat: dateFormat,
|
||||||
|
NoColor: true,
|
||||||
|
}
|
||||||
|
consoleLogger = zerolog.New(consoleOutput).With().Timestamp().Logger()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Debug logs at debug level for the specified sender
|
// Debug logs at debug level for the specified sender
|
||||||
@@ -63,6 +74,26 @@ func Error(sender string, format string, v ...interface{}) {
|
|||||||
logger.Error().Str("sender", sender).Msg(fmt.Sprintf(format, v...))
|
logger.Error().Str("sender", sender).Msg(fmt.Sprintf(format, v...))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DebugToConsole logs at debug level to stdout
|
||||||
|
func DebugToConsole(format string, v ...interface{}) {
|
||||||
|
consoleLogger.Debug().Msg(fmt.Sprintf(format, v...))
|
||||||
|
}
|
||||||
|
|
||||||
|
// InfoToConsole logs at info level to stdout
|
||||||
|
func InfoToConsole(format string, v ...interface{}) {
|
||||||
|
consoleLogger.Info().Msg(fmt.Sprintf(format, v...))
|
||||||
|
}
|
||||||
|
|
||||||
|
// WarnToConsole logs at info level to stdout
|
||||||
|
func WarnToConsole(format string, v ...interface{}) {
|
||||||
|
consoleLogger.Warn().Msg(fmt.Sprintf(format, v...))
|
||||||
|
}
|
||||||
|
|
||||||
|
// ErrorToConsole logs at error level to stdout
|
||||||
|
func ErrorToConsole(format string, v ...interface{}) {
|
||||||
|
consoleLogger.Error().Msg(fmt.Sprintf(format, v...))
|
||||||
|
}
|
||||||
|
|
||||||
// TransferLog logs an SFTP upload or download
|
// TransferLog logs an SFTP upload or download
|
||||||
func TransferLog(operation string, path string, elapsed int64, size int64, user string, connectionID string) {
|
func TransferLog(operation string, path string, elapsed int64, size int64, user string, connectionID string) {
|
||||||
logger.Info().
|
logger.Info().
|
||||||
|
|||||||
6
main.go
6
main.go
@@ -45,7 +45,8 @@ func main() {
|
|||||||
|
|
||||||
err := dataprovider.Initialize(providerConf, configDir)
|
err := dataprovider.Initialize(providerConf, configDir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Warn(logSender, "error initializing data provider: %v", err)
|
logger.Error(logSender, "error initializing data provider: %v", err)
|
||||||
|
logger.ErrorToConsole("error initializing data provider: %v", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -61,6 +62,7 @@ func main() {
|
|||||||
logger.Debug(logSender, "initializing SFTP server with config %+v", sftpdConf)
|
logger.Debug(logSender, "initializing SFTP server with config %+v", sftpdConf)
|
||||||
if err := sftpdConf.Initialize(configDir); err != nil {
|
if err := sftpdConf.Initialize(configDir); err != nil {
|
||||||
logger.Error(logSender, "could not start SFTP server: %v", err)
|
logger.Error(logSender, "could not start SFTP server: %v", err)
|
||||||
|
logger.ErrorToConsole("could not start SFTP server: %v", err)
|
||||||
}
|
}
|
||||||
shutdown <- true
|
shutdown <- true
|
||||||
}()
|
}()
|
||||||
@@ -80,11 +82,13 @@ func main() {
|
|||||||
}
|
}
|
||||||
if err := s.ListenAndServe(); err != nil {
|
if err := s.ListenAndServe(); err != nil {
|
||||||
logger.Error(logSender, "could not start HTTP server: %v", err)
|
logger.Error(logSender, "could not start HTTP server: %v", err)
|
||||||
|
logger.ErrorToConsole("could not start HTTP server: %v", err)
|
||||||
}
|
}
|
||||||
shutdown <- true
|
shutdown <- true
|
||||||
}()
|
}()
|
||||||
} else {
|
} else {
|
||||||
logger.Debug(logSender, "HTTP server not started, disabled in config file")
|
logger.Debug(logSender, "HTTP server not started, disabled in config file")
|
||||||
|
logger.DebugToConsole("HTTP server not started, disabled in config file")
|
||||||
}
|
}
|
||||||
|
|
||||||
<-shutdown
|
<-shutdown
|
||||||
|
|||||||
@@ -52,6 +52,7 @@ func (c Configuration) Initialize(configDir string) error {
|
|||||||
utils.SetUmask(int(umask), c.Umask)
|
utils.SetUmask(int(umask), c.Umask)
|
||||||
} else {
|
} else {
|
||||||
logger.Warn(logSender, "error reading umask, please fix your config file: %v", err)
|
logger.Warn(logSender, "error reading umask, please fix your config file: %v", err)
|
||||||
|
logger.WarnToConsole("error reading umask, please fix your config file: %v", err)
|
||||||
}
|
}
|
||||||
actions = c.Actions
|
actions = c.Actions
|
||||||
serverConfig := &ssh.ServerConfig{
|
serverConfig := &ssh.ServerConfig{
|
||||||
@@ -78,6 +79,7 @@ func (c Configuration) Initialize(configDir string) error {
|
|||||||
|
|
||||||
if _, err := os.Stat(filepath.Join(configDir, "id_rsa")); os.IsNotExist(err) {
|
if _, err := os.Stat(filepath.Join(configDir, "id_rsa")); os.IsNotExist(err) {
|
||||||
logger.Info(logSender, "creating new private key for server")
|
logger.Info(logSender, "creating new private key for server")
|
||||||
|
logger.InfoToConsole("id_rsa does not exist, creating new private key for server")
|
||||||
if err := c.generatePrivateKey(configDir); err != nil {
|
if err := c.generatePrivateKey(configDir); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user