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