Use new logger in config

This commit is contained in:
sirkrypt0
2021-04-29 12:43:07 +02:00
parent 5e336d4d4a
commit 776152621e

View File

@@ -3,8 +3,8 @@ package config
import ( import (
"flag" "flag"
"fmt" "fmt"
"gitlab.hpi.de/codeocean/codemoon/poseidon/logging"
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
"log"
"os" "os"
"reflect" "reflect"
"strconv" "strconv"
@@ -26,6 +26,7 @@ var (
Level: "INFO", Level: "INFO",
}, },
} }
log = logging.GetLogger("config")
) )
type server struct { type server struct {
@@ -60,14 +61,14 @@ func readConfigFile() []byte {
flag.StringVar(&configFilePath, "config", "./configuration.yaml", "path of the yaml config file") flag.StringVar(&configFilePath, "config", "./configuration.yaml", "path of the yaml config file")
data, err := os.ReadFile(configFilePath) data, err := os.ReadFile(configFilePath)
if err != nil { if err != nil {
log.Printf("Using default configuration... (%v)", err) log.WithError(err).Info("Using default configuration...")
} }
return data return data
} }
func (c *configuration) mergeYaml(content []byte) { func (c *configuration) mergeYaml(content []byte) {
if err := yaml.Unmarshal(content, c); err != nil { 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 { if value.Kind() != reflect.Struct {
content, ok := os.LookupEnv(prefix) content, ok := os.LookupEnv(prefix)
logEntry := log.
WithField("prefix", prefix).
WithField("content", content)
if !ok { if !ok {
return return
} }
@@ -91,20 +97,20 @@ func readFromEnvironment(prefix string, value reflect.Value) {
case reflect.Int: case reflect.Int:
integer, err := strconv.Atoi(content) integer, err := strconv.Atoi(content)
if err != nil { 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 return
} }
value.SetInt(int64(integer)) value.SetInt(int64(integer))
case reflect.Bool: case reflect.Bool:
boolean, err := strconv.ParseBool(content) boolean, err := strconv.ParseBool(content)
if err != nil { 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 return
} }
value.SetBool(boolean) value.SetBool(boolean)
default: default:
// ignore this field // 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 { } else {
for i := 0; i < value.NumField(); i++ { for i := 0; i < value.NumField(); i++ {