From 776152621e00f2fbed26a75ab003bb16b9625a77 Mon Sep 17 00:00:00 2001 From: sirkrypt0 <22522058+sirkrypt0@users.noreply.github.com> Date: Thu, 29 Apr 2021 12:43:07 +0200 Subject: [PATCH] Use new logger in config --- config/config.go | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/config/config.go b/config/config.go index 2c9370a..b3e1521 100644 --- a/config/config.go +++ b/config/config.go @@ -3,8 +3,8 @@ package config import ( "flag" "fmt" + "gitlab.hpi.de/codeocean/codemoon/poseidon/logging" "gopkg.in/yaml.v3" - "log" "os" "reflect" "strconv" @@ -26,6 +26,7 @@ var ( Level: "INFO", }, } + log = logging.GetLogger("config") ) type server struct { @@ -60,14 +61,14 @@ func readConfigFile() []byte { flag.StringVar(&configFilePath, "config", "./configuration.yaml", "path of the yaml config file") data, err := os.ReadFile(configFilePath) if err != nil { - log.Printf("Using default configuration... (%v)", err) + log.WithError(err).Info("Using default configuration...") } return data } func (c *configuration) mergeYaml(content []byte) { if err := yaml.Unmarshal(content, c); err != nil { - log.Fatalf("Could not parse configuration: %v", err) + log.WithError(err).Fatal("Could not parse configuration file") } } @@ -82,6 +83,11 @@ func readFromEnvironment(prefix string, value reflect.Value) { if value.Kind() != reflect.Struct { content, ok := os.LookupEnv(prefix) + + logEntry := log. + WithField("prefix", prefix). + WithField("content", content) + if !ok { return } @@ -91,20 +97,20 @@ func readFromEnvironment(prefix string, value reflect.Value) { case reflect.Int: integer, err := strconv.Atoi(content) if err != nil { - log.Printf("Could not parse environment variable %s with value '%q' as integer", prefix, content) + logEntry.Warn("Could not parse environment variable as integer") return } value.SetInt(int64(integer)) case reflect.Bool: boolean, err := strconv.ParseBool(content) if err != nil { - log.Printf("Could not parse environment variable %s with value '%q' as boolean", prefix, content) + logEntry.Warn("Could not parse environment variable as boolean") return } value.SetBool(boolean) default: // ignore this field - log.Printf("Setting configuration options of type %s via environment variables is not supported", value.Type().Name()) + logEntry.WithField("type", value.Type().Name()).Warn("Setting configuration option via environment variables is not supported") } } else { for i := 0; i < value.NumField(); i++ {