From 40bbdc63f16c20629d0545164b6a7a99edc507a6 Mon Sep 17 00:00:00 2001 From: Bernhard B Date: Tue, 22 Apr 2025 23:33:53 +0200 Subject: [PATCH] added env variable `LOG_LEVEL` * this environment variable allows to change the log level of the application --- README.md | 2 ++ src/api/api.go | 11 +++-------- src/main.go | 10 ++++++++++ src/utils/utils.go | 17 +++++++++++++++++ 4 files changed, 32 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index c0e2e34..cb948a4 100644 --- a/README.md +++ b/README.md @@ -147,3 +147,5 @@ There are a bunch of environmental variables that can be set inside the docker c * `PORT`: Defaults to port `8080` unless this env var is set to tell it otherwise. * `DEFAULT_SIGNAL_TEXT_MODE`: Allows to set the default text mode that should be used when sending a message (supported values: `normal`, `styled`). The setting is only used in case the `text_mode` is not explicitly set in the payload of the `send` method. + +* `LOG_LEVEL`: Allows to set the log level. Supported values: `debug`, `info`, `warn`, `error`. If nothing is specified, it defaults to `info`. diff --git a/src/api/api.go b/src/api/api.go index 4bb63ce..af114dd 100644 --- a/src/api/api.go +++ b/src/api/api.go @@ -1308,14 +1308,9 @@ func (a *Api) SetConfiguration(c *gin.Context) { } if req.Logging.Level != "" { - if req.Logging.Level == "debug" { - log.SetLevel(log.DebugLevel) - } else if req.Logging.Level == "info" { - log.SetLevel(log.InfoLevel) - } else if req.Logging.Level == "warn" { - log.SetLevel(log.WarnLevel) - } else { - c.JSON(400, Error{Msg: "Couldn't set log level - invalid log level"}) + err = utils.SetLogLevel(req.Logging.Level) + if err != nil { + c.JSON(400, Error{Msg: err.Error()}) return } } diff --git a/src/main.go b/src/main.go index 57f85a1..2c327b2 100644 --- a/src/main.go +++ b/src/main.go @@ -69,6 +69,16 @@ func main() { avatarTmpDir := flag.String("avatar-tmp-dir", "/tmp/", "Avatar tmp directory") flag.Parse() + + logLevel := utils.GetEnv("LOG_LEVEL", "") + if logLevel != "" { + err := utils.SetLogLevel(logLevel) + if err != nil { + log.Error("Couldn't set log level to '", logLevel, "'. Falling back to the info log level") + utils.SetLogLevel("info") + } + } + if utils.GetEnv("SWAGGER_USE_HTTPS_AS_PREFERRED_SCHEME", "false") == "false" { docs.SwaggerInfo.Schemes = []string{"http", "https"} } else { diff --git a/src/utils/utils.go b/src/utils/utils.go index f0e87c2..deec1e7 100644 --- a/src/utils/utils.go +++ b/src/utils/utils.go @@ -3,6 +3,8 @@ package utils import ( "os" "strconv" + "errors" + log "github.com/sirupsen/logrus" ) func GetEnv(key string, defaultVal string) string { @@ -46,3 +48,18 @@ func IsPhoneNumber(s string) bool { } return true } + +func SetLogLevel(logLevel string) error { + if logLevel == "debug" { + log.SetLevel(log.DebugLevel) + } else if logLevel == "info" { + log.SetLevel(log.InfoLevel) + } else if logLevel == "error" { + log.SetLevel(log.ErrorLevel) + } else if logLevel == "warn" { + log.SetLevel(log.WarnLevel) + } else { + return errors.New("Couldn't set log level - invalid log level") + } + return nil +}