Added setting for LogLevel

This commit is contained in:
Pijus Kamandulis
2025-01-09 21:07:41 +02:00
parent c2c9dc03b3
commit 8b8b087aab
14 changed files with 200 additions and 84 deletions

View File

@@ -26,7 +26,8 @@ func ParseFlags() ServerConfig {
disableAuthentication := flag.Bool("DisableAuth", false, "Disable authentication")
disableTls := flag.Bool("DisableTls", false, "Disable TLS, serve over HTTP")
persistDataPath := flag.String("Persist", "", "Saves data to given path on application exit")
debug := flag.Bool("Debug", false, "Runs application in debug mode, this provides additional logging")
logLevel := NewEnumValue("info", []string{"debug", "info", "error", "silent"})
flag.Var(logLevel, "LogLevel", fmt.Sprintf("Sets the logging level %s", logLevel.AllowedValuesList()))
flag.Parse()
setFlagsFromEnvironment()
@@ -41,8 +42,8 @@ func ParseFlags() ServerConfig {
config.PersistDataFilePath = *persistDataPath
config.DisableAuth = *disableAuthentication
config.DisableTls = *disableTls
config.Debug = *debug
config.AccountKey = *accountKey
config.LogLevel = logLevel.value
config.PopulateCalculatedFields()
@@ -54,7 +55,19 @@ func (c *ServerConfig) PopulateCalculatedFields() {
c.DatabaseDomain = c.Host
c.DatabaseEndpoint = fmt.Sprintf("https://%s:%d/", c.Host, c.Port)
c.ExplorerBaseUrlLocation = ExplorerBaseUrlLocation
logger.EnableDebugOutput = c.Debug
switch c.LogLevel {
case "debug":
logger.LogLevel = logger.LogLevelDebug
case "info":
logger.LogLevel = logger.LogLevelInfo
case "error":
logger.LogLevel = logger.LogLevelError
case "silent":
logger.LogLevel = logger.LogLevelSilent
default:
logger.LogLevel = logger.LogLevelInfo
}
}
func (c *ServerConfig) ApplyDefaultsToEmptyFields() {

36
api/config/enumFlag.go Normal file
View File

@@ -0,0 +1,36 @@
package config
import (
"fmt"
"strings"
)
type EnumValue struct {
allowedValues []string
value string
}
func (e *EnumValue) String() string {
return e.value
}
func (e *EnumValue) Set(v string) error {
for _, allowed := range e.allowedValues {
if v == allowed {
e.value = v
return nil
}
}
return fmt.Errorf("invalid value %q, must be one of: %s", v, strings.Join(e.allowedValues, ", "))
}
func NewEnumValue(defaultValue string, allowedValues []string) *EnumValue {
return &EnumValue{
allowedValues: allowedValues,
value: defaultValue,
}
}
func (e *EnumValue) AllowedValuesList() string {
return fmt.Sprintf("(one of: %s)", strings.Join(e.allowedValues, ", "))
}

View File

@@ -15,6 +15,6 @@ type ServerConfig struct {
PersistDataFilePath string `json:"persistDataFilePath"`
DisableAuth bool `json:"disableAuth"`
DisableTls bool `json:"disableTls"`
Debug bool `json:"debug"`
LogLevel string `json:"logLevel"`
ExplorerBaseUrlLocation string `json:"explorerBaseUrlLocation"`
}