diff --git a/cmd/server/config.go b/cmd/server/config.go index 3fcd9eb..ecde6f6 100644 --- a/cmd/server/config.go +++ b/cmd/server/config.go @@ -24,9 +24,10 @@ import ( ) type Config struct { - Host string - Plain PlainConfig - TLS TLSConfig + LogLevel string + Host string + Plain PlainConfig + TLS TLSConfig } type PlainConfig struct { diff --git a/cmd/server/main.go b/cmd/server/main.go index 50e570c..2d558fd 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -19,28 +19,69 @@ package main import ( - "log" "flag" - "fmt" "crypto/tls" + "os" + "time" + + "github.com/rs/zerolog" + "github.com/rs/zerolog/log" "forge.monodon.me/Gnarwhal/diodemail/smtp" ) +const Version = "1.0.0" + func main() { - var cert_path string - var version bool - flag.StringVar(&cert_path, "config", "/etc/diodemail/config.json", "Path to config file") - flag.BoolVar(&version, "version", false, "Print version") + log.Logger = zerolog. + New(zerolog.ConsoleWriter{Out: os.Stderr, TimeFormat: time.RFC3339}). + With(). + Timestamp(). + Logger(). + Level(zerolog.ErrorLevel) + + var config_path string + var log_level string + + flag.StringVar( + &config_path, + "config-file", + "/etc/diodemail/config.json", + "Path to config file", + ) + flag.StringVar( + &log_level, + "log-level", + "", + "Maximum log level to log", + ) + flag.Parse() - if version { - fmt.Println("diodemail version 1.0.0") - return - } - config, err := LoadConfig(cert_path) + config, err := LoadConfig(config_path) if err != nil { - log.Fatal(err) + log.Fatal().Msgf("%v", err) } + if log_level == "" { + if config.LogLevel != "" { + log_level = config.LogLevel + } else { + log_level = "info" + } + } + + parsed_log_level, err := zerolog.ParseLevel(log_level) + if err != nil { + log.Fatal().Msgf("%v", err) + } + + log.Logger = zerolog. + New(zerolog.ConsoleWriter{Out: os.Stderr, TimeFormat: time.RFC3339}). + With(). + Timestamp(). + Logger(). + Level(parsed_log_level) + log.Info().Msgf("Starting diodemail v%v", Version) + log.Info().Msgf("Loaded config from: %v", config_path) var plain_config *smtp.PlainConfig if config.Plain.Enabled { @@ -56,7 +97,7 @@ func main() { config.TLS.PrivateKeyPath, ) if err != nil { - log.Println(err) + log.Error().Msgf("Failed to load TLS config: %v", err) } else { tls_config = &smtp.TLSConfig { config.TLS.Port, @@ -71,6 +112,6 @@ func main() { tls_config, ) if err != nil { - log.Fatal(err) + log.Fatal().Msgf("%v", err) } } diff --git a/smtp/server.go b/smtp/server.go index a1e997a..22822f5 100644 --- a/smtp/server.go +++ b/smtp/server.go @@ -22,10 +22,8 @@ import ( "fmt" "net" "crypto/tls" - "os" "sync" - "github.com/rs/zerolog" "github.com/rs/zerolog/log" ) @@ -62,13 +60,6 @@ func handle(connection net.Conn, host string) { } func Run(host string, plain_config *PlainConfig, tls_config *TLSConfig) error { - log.Logger = zerolog. - New(zerolog.ConsoleWriter{Out: os.Stderr}). - With(). - Timestamp(). - Logger(). - Level(zerolog.TraceLevel) - var wait_group sync.WaitGroup if plain_config != nil { listener, err := net.Listen("tcp", fmt.Sprintf("%v:%v", host, plain_config.Port))