Initialise logger in main not server

This commit is contained in:
Gnarwhal 2024-10-02 03:29:19 +00:00
parent 6c853bbaaa
commit 3c06b3c741
Signed by: Gnarwhal
GPG key ID: 0989A73D8C421174
3 changed files with 59 additions and 26 deletions

View file

@ -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 {

View file

@ -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)
}
}

View file

@ -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))