feat(logging): integrate slog logging with env control and request/sync tracing
MindSift CI & Docker Release / Build, Test in Container & Push (push) Successful in 39s

- Configure structured slog logger in cmd/server/main.go via LOG_LEVEL and DEBUG env vars
- Add HTTP API request logging middleware in internal/api/ with status and duration_ms
- Add WebSocket join/leave, event reception, and debounced SQLite flush tracing in docroom
- Document LOG_LEVEL and DEBUG settings in .env.example, compose.yaml, and docker-compose.yml
This commit is contained in:
sandrodach committed 2026-10-26 22:12:51 +01:00
1 parent 177e24fdc8
commit 48fe61fbce
9 files changed
+208 -16

No files matched your search

+25 -1
View File
@@ -1,6 +1,7 @@
package config
import (
"log/slog"
"os"
"path/filepath"
"strings"
@@ -14,6 +15,7 @@ type Config struct {
BaseURL string
AllowRegistration bool
LogLevel string
Debug bool
DemoMode bool
// Optional SMTP environment variables (can also be overridden/configured in DB system_settings)
@@ -40,7 +42,14 @@ func Load() *Config {
jwtSecret := getEnv("JWT_SECRET", "mindsift-default-selfhosted-jwt-secret-key-2026")
baseURL := getEnv("BASE_URL", "http://localhost:"+port)
allowReg := strings.ToLower(getEnv("ALLOW_REGISTRATION", "false")) == "true"
logLevel := getEnv("LOG_LEVEL", "info")
debugVal := strings.ToLower(getEnv("DEBUG", "false"))
debug := debugVal == "true" || debugVal == "1" || debugVal == "yes" || debugVal == "on"
logLevel := strings.ToLower(getEnv("LOG_LEVEL", "info"))
if debug {
logLevel = "debug"
}
demoVal := strings.ToLower(getEnv("MINDA_DEMO", getEnv("MINDSIFT_DEMO", getEnv("DEMO_MODE", "false"))))
demoMode := demoVal == "true" || demoVal == "1" || demoVal == "yes" || demoVal == "on"
@@ -60,6 +69,7 @@ func Load() *Config {
BaseURL: baseURL,
AllowRegistration: allowReg,
LogLevel: logLevel,
Debug: debug,
DemoMode: demoMode,
SMTPHost: smtpHost,
SMTPPort: smtpPort,
@@ -70,6 +80,20 @@ func Load() *Config {
}
}
// SlogLevel parses Config.LogLevel into a standard slog.Level.
func (c *Config) SlogLevel() slog.Level {
switch strings.ToLower(strings.TrimSpace(c.LogLevel)) {
case "debug":
return slog.LevelDebug
case "warn", "warning":
return slog.LevelWarn
case "error":
return slog.LevelError
default:
return slog.LevelInfo
}
}
func getEnv(key, fallback string) string {
if val, ok := os.LookupEnv(key); ok && strings.TrimSpace(val) != "" {
return strings.TrimSpace(val)
+58
View File
@@ -0,0 +1,58 @@
package config
import (
"log/slog"
"os"
"testing"
)
func TestConfigLogLevelAndDebug(t *testing.T) {
os.Setenv("PORT", "9090")
defer os.Unsetenv("PORT")
// 1. Default info
os.Unsetenv("LOG_LEVEL")
os.Unsetenv("DEBUG")
cfg := Load()
if cfg.SlogLevel() != slog.LevelInfo {
t.Fatalf("expected LevelInfo, got %v", cfg.SlogLevel())
}
// 2. LOG_LEVEL=debug
os.Setenv("LOG_LEVEL", "debug")
cfg = Load()
if cfg.SlogLevel() != slog.LevelDebug {
t.Fatalf("expected LevelDebug, got %v", cfg.SlogLevel())
}
// 3. LOG_LEVEL=warn
os.Setenv("LOG_LEVEL", "warn")
cfg = Load()
if cfg.SlogLevel() != slog.LevelWarn {
t.Fatalf("expected LevelWarn, got %v", cfg.SlogLevel())
}
// 4. LOG_LEVEL=error
os.Setenv("LOG_LEVEL", "error")
cfg = Load()
if cfg.SlogLevel() != slog.LevelError {
t.Fatalf("expected LevelError, got %v", cfg.SlogLevel())
}
// 5. DEBUG=true overrides LOG_LEVEL
os.Setenv("LOG_LEVEL", "error")
os.Setenv("DEBUG", "true")
cfg = Load()
if cfg.SlogLevel() != slog.LevelDebug {
t.Fatalf("expected LevelDebug when DEBUG=true, got %v", cfg.SlogLevel())
}
// 6. DEBUG=1 overrides LOG_LEVEL
os.Setenv("DEBUG", "1")
cfg = Load()
if cfg.SlogLevel() != slog.LevelDebug {
t.Fatalf("expected LevelDebug when DEBUG=1, got %v", cfg.SlogLevel())
}
os.Unsetenv("DEBUG")
os.Unsetenv("LOG_LEVEL")
}