Skip to content

Commit

Permalink
Allow log level to be set at runtime (#267)
Browse files Browse the repository at this point in the history
I noticed we never allow overriding of the log level from Info, even
though we do log at lower levels. This provides a mechanism to fix this
- I thought it might be potentially helpful in debugging population
errors (as #264 might be).

This partially depends on, and therefore has some similar diffs to,
#266.

Default output:

![image](https://github.com/user-attachments/assets/289cbb49-fe0d-42aa-badf-534f4f0f50f4)

With level=Debug (and the ideal Cadence dev music folder):

![image](https://github.com/user-attachments/assets/04e7d23f-6960-4efc-afef-94e4edf29bfe)

Commandline that sets Debug:

![image](https://github.com/user-attachments/assets/1961cb28-d4a1-4cca-8b53-498e8aba6955)
  • Loading branch information
kenellorando authored Aug 6, 2024
2 parents 179d93a + f6df315 commit 5fc54d0
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
3 changes: 2 additions & 1 deletion config/cadence.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ CSERVER_REQRATELIMIT=CADENCE_RATE_EXAMPLE
POSTGRES_PASSWORD=CADENCE_PASS_EXAMPLE

# ####################################################
# If you are running Cadence through Docker simply as a user,
# If you are running Cadence through Docker simply as a user,
# you are unlikely to ever need to change anything below.

# Development
CSERVER_DEVMODE=0
CSERVER_VERSION=5.4.4
CSERVER_ROOTPATH=/cadence/server/
CSERVER_LOGLEVEL=info

# Service Addresses
CSERVER_PORT=:8080
Expand Down
25 changes: 25 additions & 0 deletions src/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net/http"
"os"
"strconv"
"strings"
)

var c = ServerConfig{}
Expand All @@ -31,6 +32,27 @@ type ServerConfig struct {
RedisPort string
WhitelistPath string
DevMode bool
LogLevel string
}

func parseLogLevel(level string) slog.Level {
if level == "" {
return slog.LevelInfo
}

switch strings.ToLower(level) {
case "debug":
return slog.LevelDebug
case "info":
return slog.LevelInfo
case "warn":
return slog.LevelWarn
case "error":
return slog.LevelError
default:
slog.Warn(fmt.Sprintf("Unrecognized log level %s!", level), "func", "parseLogLevel")
return slog.LevelInfo
}
}

func main() {
Expand All @@ -54,6 +76,9 @@ func main() {
c.RedisPort = os.Getenv("CSERVER_REDISPORT")
c.WhitelistPath = os.Getenv("CSERVER_WHITELIST_PATH")
c.DevMode, _ = strconv.ParseBool(os.Getenv("CSERVER_DEVMODE"))
c.LogLevel = os.Getenv("CSERVER_LOGLEVEL")

slog.SetLogLoggerLevel(parseLogLevel(c.LogLevel))

if postgresInit() == nil {
if postgresPopulate() != nil {
Expand Down

0 comments on commit 5fc54d0

Please sign in to comment.