Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ WEBHOOK_FORMAT=json
# WuzAPI Session Configuration
SESSION_DEVICE_NAME=WuzAPI

# Logging configuration
LOG_LEVEL=info # debug, info, warn, error

# Database configuration
DB_USER=wuzapi
DB_PASSWORD=wuzapi
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ you can use to alter behaviour
* -address : sets the IP address to bind the server to (default 0.0.0.0)
* -port : sets the port number (default 8080)
* -logtype : format for logs, either console (default) or json
* -loglevel : sets log level: debug, info (default), warn, error
* -color : enable colored output for console logs
* -osname : Connection OS Name in Whatsapp
* -skipmedia : Skip downloading media from messages
Expand Down
1 change: 1 addition & 0 deletions docker-compose-swarm.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ services:
- TZ=${TZ:-America/Sao_Paulo}
- WEBHOOK_FORMAT=${WEBHOOK_FORMAT:-json}
- SESSION_DEVICE_NAME=${SESSION_DEVICE_NAME:-WuzAPI}
- LOG_LEVEL=info
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The LOG_LEVEL is hardcoded. It should use variable substitution to allow configuration from the shell environment, which is the standard practice for swarm deployments as noted in the README. This provides flexibility for different environments.

      - LOG_LEVEL=${LOG_LEVEL:-info}

# RabbitMQ configuration Optional
- RABBITMQ_URL=amqp://wuzapi:wuzapi@rabbitmq:5672/
- RABBITMQ_QUEUE=whatsapp_events
Expand Down
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ services:
- TZ=${TZ:-America/Sao_Paulo}
- WEBHOOK_FORMAT=${WEBHOOK_FORMAT:-json}
- SESSION_DEVICE_NAME=${SESSION_DEVICE_NAME:-WuzAPI}
- LOG_LEVEL=info
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The LOG_LEVEL is hardcoded to info. To allow for easier configuration via the .env file, it should use variable substitution with a default value, similar to other environment variables in this file. This improves configurability.

      - LOG_LEVEL=${LOG_LEVEL:-info}

# RabbitMQ configuration Optional
- RABBITMQ_URL=amqp://wuzapi:wuzapi@rabbitmq:5672/
- RABBITMQ_QUEUE=whatsapp_events
Expand Down
23 changes: 23 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ var (
port = flag.String("port", "8080", "Listen Port")
waDebug = flag.String("wadebug", "", "Enable whatsmeow debug (INFO or DEBUG)")
logType = flag.String("logtype", "console", "Type of log output (console or json)")
logLevel = flag.String("loglevel", "info", "Log level (debug, info, warn, error)")
skipMedia = flag.Bool("skipmedia", false, "Do not attempt to download media in messages")
osName = flag.String("osname", "Mac OS 10", "Connection OSName in Whatsapp")
colorOutput = flag.Bool("color", false, "Enable colored output for console logs")
Expand Down Expand Up @@ -180,6 +181,28 @@ func main() {

flag.Parse()

// Configure log level from environment variable if set
if v := os.Getenv("LOG_LEVEL"); v != "" {
*logLevel = v
}
Comment on lines +185 to +187
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The current implementation for setting the log level from an environment variable gives it higher precedence than the command-line flag. This is inconsistent with how other configurations like WUZAPI_ADDRESS and WUZAPI_PORT are handled in this file, where command-line flags override environment variables. To maintain consistency, the logic should be adjusted to only use the environment variable if the command-line flag has not been set (i.e., it has its default value of "info"). You might also want to update the comment on line 184 to reflect this change, for example: // Configure log level from environment variable if flag is default.

Suggested change
if v := os.Getenv("LOG_LEVEL"); v != "" {
*logLevel = v
}
if v := os.Getenv("LOG_LEVEL"); v != "" && *logLevel == "info" {
*logLevel = v
}


// Set global log level
switch strings.ToLower(*logLevel) {
case "debug":
zerolog.SetGlobalLevel(zerolog.DebugLevel)
log.Debug().Msg("Log level set to DEBUG")
case "info":
zerolog.SetGlobalLevel(zerolog.InfoLevel)
case "warn", "warning":
zerolog.SetGlobalLevel(zerolog.WarnLevel)
case "error":
zerolog.SetGlobalLevel(zerolog.ErrorLevel)
default:
zerolog.SetGlobalLevel(zerolog.InfoLevel)
log.Warn().Str("invalid_level", *logLevel).Msg("Invalid log level, defaulting to INFO")
*logLevel = "info"
}

// Check for address in environment variable if flag is default or empty
if *address == "0.0.0.0" || *address == "" {
if v := os.Getenv("WUZAPI_ADDRESS"); v != "" {
Expand Down
Loading