Create the logger only after reading the config

This commit is contained in:
Miguel de la Cruz 2023-07-10 13:53:25 +02:00
parent b5bd054bbe
commit e43cb05755

View file

@ -2,6 +2,7 @@ package main
import (
"flag"
"fmt"
"os"
"os/signal"
"syscall"
@ -11,35 +12,32 @@ import (
"github.com/charmbracelet/log"
)
func configureLogger(config *model.Config, logger *log.Logger) {
func loggerFromConfig(config *model.Config) *log.Logger {
// ToDo: apply config to logger
return log.New(os.Stderr)
}
func main() {
configFlag := flag.String("config", "birthdaybot.yml", "path to the configuration file")
flag.Parse()
logger := log.New(os.Stderr)
config, err := model.ReadConfig(*configFlag)
if err != nil {
logger.Error("error reading config", "configPath", *configFlag, "error", err)
fmt.Fprintf(os.Stderr, "ERROR: error reading config %q: %s\n", *configFlag, err)
os.Exit(1)
}
if err := config.IsValid(); err != nil {
logger.Error("invalid config", "configPath", *configFlag, "error", err)
fmt.Fprintf(os.Stderr, "ERROR: invalid config %q: %s\n", *configFlag, err)
os.Exit(1)
}
configureLogger(config, logger)
srv, err := server.New(
server.WithLogger(logger),
server.WithLogger(loggerFromConfig(config)),
server.WithConfig(config),
)
if err != nil {
logger.Error("error creating server", "error", err)
fmt.Fprintf(os.Stderr, "ERROR: error creating server: %s\n", err)
os.Exit(1)
}