Create the logger only after reading the config
This commit is contained in:
parent
b5bd054bbe
commit
e43cb05755
1 changed files with 7 additions and 9 deletions
|
@ -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)
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue