added env variable LOG_LEVEL

* this environment variable allows to change the log level of the
  application
This commit is contained in:
Bernhard B
2025-04-22 23:33:53 +02:00
parent 0f7a247f2e
commit 40bbdc63f1
4 changed files with 32 additions and 8 deletions

View File

@@ -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`.

View File

@@ -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
}
}

View File

@@ -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 {

View File

@@ -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
}