Modern Docker Management, Designed for Everyone
Go composecontainer-managementcontainersdockerdocker-composedocker-managementgoself-hostedsveltekittypescriptweb-ui
refactor: cleanup logging
Kyle Mendell2a3fb4ed0b39
diff --git a/.env.example b/.env.exampleindex dfe112db2..4fd1cb693 100644--- a/.env.example+++ b/.env.example@@ -1,13 +1,13 @@# Server ConfigurationAPP_URL="http://localhost:3552"ENVIRONMENT=production-# Encryption key for sensitive data (must be exactly 32 characters)-# Generate a secure key for production: openssl rand -base64 32 | head -c 32-ENCRYPTION_KEY=f00V7L3pqj+yMtfUkpFAyaRTxoGo+600+ENCRYPTION_KEY=f00V7L3pqj+yMtfUkpFAyaRTxoGo+600 #openssl rand -base64 32 | head -c 32JWT_SECRET=your-super-secret-jwt-key-change-this-in-productionOIDC_ENABLED=trueOIDC_CLIENT_ID=""OIDC_CLIENT_SECRET=""OIDC_ISSUER_URL="https://id.example.com"-OIDC_SCOPES="openid email profile"\ No newline at end of file+OIDC_SCOPES="openid email profile"++LOG_JSON=true\ No newline at end of filediff --git a/.github/workflows/backend-tests.yml b/.github/workflows/backend-tests.ymlindex 1c10e6370..7d6257631 100644--- a/.github/workflows/backend-tests.yml+++ b/.github/workflows/backend-tests.yml@@ -19,7 +19,7 @@ jobs:steps:- name: Checkout- uses: actions/checkout@v4+ uses: actions/checkout@v5- name: Setup Gouses: actions/setup-go@v5diff --git a/.github/workflows/build-next.yml b/.github/workflows/build-next.ymlindex 01b9fe6d5..307b315bb 100644--- a/.github/workflows/build-next.yml+++ b/.github/workflows/build-next.yml@@ -19,7 +19,7 @@ jobs:attestations: writesteps:- name: Checkout code- uses: actions/checkout@v4+ uses: actions/checkout@v5- name: Setup pnpmuses: pnpm/action-setup@v4diff --git a/.github/workflows/e2e-tests.yml b/.github/workflows/e2e-tests.ymlindex 35f229b0e..556d0e340 100644--- a/.github/workflows/e2e-tests.yml+++ b/.github/workflows/e2e-tests.yml@@ -69,7 +69,7 @@ jobs:port: 3001name: E2E Tests (${{ matrix.database.name }})steps:- - uses: actions/checkout@v4+ - uses: actions/checkout@v5- name: Setup pnpmuses: pnpm/action-setup@v4diff --git a/.github/workflows/release.yml b/.github/workflows/release.ymlindex 53d8c9bc1..00edd2ca0 100644--- a/.github/workflows/release.yml+++ b/.github/workflows/release.yml@@ -20,7 +20,7 @@ jobs:steps:- name: Checkout- uses: actions/checkout@v4+ uses: actions/checkout@v5with:fetch-depth: 1diff --git a/backend/go.mod b/backend/go.modindex b1dcbb24d..26e1c7741 100644--- a/backend/go.mod+++ b/backend/go.mod@@ -16,6 +16,8 @@ require (github.com/google/uuid v1.6.0github.com/jinzhu/copier v0.4.0github.com/joho/godotenv v1.5.1+ github.com/lmittmann/tint v1.1.2+ github.com/samber/slog-gin v1.17.1github.com/shirou/gopsutil/v3 v3.24.5golang.org/x/crypto v0.38.0gopkg.in/yaml.v3 v3.0.1diff --git a/backend/go.sum b/backend/go.sumindex 1cd04875b..7aae1d8af 100644--- a/backend/go.sum+++ b/backend/go.sum@@ -127,6 +127,8 @@ github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ=github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI=github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=+github.com/lmittmann/tint v1.1.2 h1:2CQzrL6rslrsyjqLDwD11bZ5OpLBPU+g3G/r5LSfS8w=+github.com/lmittmann/tint v1.1.2/go.mod h1:HIS3gSy7qNwGCj+5oRjAutErFBl4BzdQP6cJZ0NfMwE=github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 h1:6E+4a0GO5zZEnZ81pIr0yLvtUWk2if982qA3F3QD6H4=github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0/go.mod h1:zJYVVT2jmtg6P3p1VtQj7WsuWi/y4VnjVBn7F8KPB3I=github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=@@ -169,6 +171,8 @@ github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs=github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=github.com/rogpeppe/go-internal v1.13.1 h1:KvO1DLK/DRN07sQ1LQKScxyZJuNnedQ5/wKSR38lUII=github.com/rogpeppe/go-internal v1.13.1/go.mod h1:uMEvuHeurkdAXX61udpOXGD/AzZDWNMNyH2VO9fmH0o=+github.com/samber/slog-gin v1.17.1 h1:CsnUxjcbFEs0zpO7sZz/ZLOtO1Vfoc291jlYZXS4ZLs=+github.com/samber/slog-gin v1.17.1/go.mod h1:7R4VMQGENllRLLnwGyoB5nUSB+qzxThpGe5G02xla6o=github.com/shirou/gopsutil/v3 v3.24.5 h1:i0t8kL+kQTvpAYToeuiVk3TgDeKOFioZO3Ztz/iZ9pI=github.com/shirou/gopsutil/v3 v3.24.5/go.mod h1:bsoOS1aStSs9ErQ1WWfxllSeS1K5D+U30r2NfcubMVk=github.com/shoenig/go-m1cpu v0.1.6 h1:nxdKQNcEB6vzgA2E2bvzKIYRuNj7XNJ4S/aRSwKzFtM=diff --git a/backend/internal/bootstrap/bootstrap.go b/backend/internal/bootstrap/bootstrap.goindex ebaee656f..3dd1eae17 100644--- a/backend/internal/bootstrap/bootstrap.go+++ b/backend/internal/bootstrap/bootstrap.go@@ -13,6 +13,7 @@ import ("github.com/gin-gonic/gin""github.com/joho/godotenv"+ "github.com/lmittmann/tint""github.com/ofkm/arcane-backend/internal/api""github.com/ofkm/arcane-backend/internal/config"@@ -34,18 +35,30 @@ type App struct {func InitializeApp() (*App, error) {ctx := context.Background()+ loadErr := godotenv.Load()++ cfg := config.Load()+{level := new(slog.LevelVar)level.Set(slog.LevelInfo)- h := slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{Level: level})++ var h slog.Handler+ if cfg.LogJson {+ h = slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{Level: level})+ } else {+ h = tint.NewHandler(os.Stdout, &tint.Options{+ Level: level,+ TimeFormat: "Jan 02 15:04:05.000",+ })+ }slog.SetDefault(slog.New(h))}- if err := godotenv.Load(); err != nil {+ if loadErr != nil {slog.InfoContext(ctx, "No .env file found, using environment variables")}- cfg := config.Load()appCtx, cancelApp := context.WithCancel(ctx)utils.InitEncryption(cfg)diff --git a/backend/internal/bootstrap/router_bootstrap.go b/backend/internal/bootstrap/router_bootstrap.goindex 23fe7e509..9ee1df302 100644--- a/backend/internal/bootstrap/router_bootstrap.go+++ b/backend/internal/bootstrap/router_bootstrap.go@@ -1,9 +1,14 @@package bootstrapimport (+ "log/slog""net/http"+ "path"+ "strings""github.com/gin-gonic/gin"+ sloggin "github.com/samber/slog-gin"+"github.com/ofkm/arcane-backend/frontend""github.com/ofkm/arcane-backend/internal/api""github.com/ofkm/arcane-backend/internal/config"@@ -21,13 +26,37 @@ func setupRouter(cfg *config.Config, appServices *api.Services) *gin.Engine {router := gin.New()router.Use(gin.Recovery())+ loggerSkipPatterns := []string{+ "GET /api/containers/*/stats/stream",+ "GET /api/containers/*/logs/stream",+ "GET /_app",+ "GET /img",+ "GET /fonts",+ "GET /api/system/stats",+ "GET /health",+ "HEAD /health",+ }+ router.Use(sloggin.NewWithConfig(slog.Default(), sloggin.Config{+ Filters: []sloggin.Filter{+ func(c *gin.Context) bool {+ mp := c.Request.Method + " " + c.Request.URL.Path+ for _, pat := range loggerSkipPatterns {+ if pat == mp {+ return false+ }+ if ok, _ := path.Match(pat, mp); ok {+ return false+ }+ if strings.HasSuffix(pat, "/") && strings.HasPrefix(mp, pat) {+ return false+ }+ }+ return true+ },+ },+ }))+router.Use(middleware.SetupCORS(cfg))- loggingMiddleware := middleware.LoggingMiddleware(- "/api/containers/*/stats/stream",- "/api/containers/*/logs/stream",- "/api/system/stats",- )- router.Use(loggingMiddleware)router.GET("/health", func(c *gin.Context) {c.JSON(http.StatusOK, gin.H{"status": "UP"})diff --git a/backend/internal/config/config.go b/backend/internal/config/config.goindex 6ce7cbc72..6d34ff2ae 100644--- a/backend/internal/config/config.go+++ b/backend/internal/config/config.go@@ -24,10 +24,11 @@ type Config struct {OidcScopes stringEncryptionKey string+ LogJson bool}func Load() *Config {- oidcEnabled, _ := strconv.ParseBool(os.Getenv("OIDC_ENABLED"))+ oidcEnabled := getBoolEnvOrDefault("OIDC_ENABLED", false)return &Config{AppUrl: getEnvOrDefault("APP_URL", "http://localhost:3552"),@@ -42,6 +43,8 @@ func Load() *Config {OidcClientSecret: os.Getenv("OIDC_CLIENT_SECRET"),OidcIssuerURL: os.Getenv("OIDC_ISSUER_URL"),OidcScopes: getEnvOrDefault("OIDC_SCOPES", "openid email profile"),++ LogJson: getBoolEnvOrDefault("LOG_JSON", false),}}@@ -56,3 +59,12 @@ func getEnvOrDefault(key, defaultValue string) string {}return defaultValue}++func getBoolEnvOrDefault(key string, defaultValue bool) bool {+ if v, ok := os.LookupEnv(key); ok && v != "" {+ if b, err := strconv.ParseBool(v); err == nil {+ return b+ }+ }+ return defaultValue+}diff --git a/backend/internal/database/gorm_slog_logger.go b/backend/internal/database/gorm_slog_logger.goindex 2edae0a8f..283483028 100644--- a/backend/internal/database/gorm_slog_logger.go+++ b/backend/internal/database/gorm_slog_logger.go@@ -11,7 +11,6 @@ import ("gorm.io/gorm/logger")-// slogGormLogger implements gorm/logger.Interface using Go's slog for JSON logs.type slogGormLogger struct {level logger.LogLevelslowThreshold time.Durationdiff --git a/backend/internal/dto/oidc.go b/backend/internal/dto/oidc.goindex 14ca725a9..8de48dad5 100644--- a/backend/internal/dto/oidc.go+++ b/backend/internal/dto/oidc.go@@ -27,7 +27,6 @@ type OidcTokenResponse struct {IDToken string `json:"id_token,omitempty"`}-// Used by GET /auth/oidc/statustype OidcStatusInfo struct {EnvForced bool `json:"envForced"`EnvConfigured bool `json:"envConfigured"`diff --git a/backend/internal/middleware/logging.go b/backend/internal/middleware/logging.godeleted file mode 100644index d0312d67b..000000000--- a/backend/internal/middleware/logging.go+++ /dev/null@@ -1,81 +0,0 @@-package middleware--import (- "log/slog"- "path"- "time"-- "github.com/gin-gonic/gin"-)--// matchesExcluded checks if the given request path matches any of the exclude patterns.-// Patterns can include '*' wildcards for path segments (e.g., "/api/containers/*/stats/stream").-func matchesExcluded(p string, patterns []string) bool {- for _, pat := range patterns {- if pat == "" {- continue- }- if pat == p {- return true- }- if ok, err := path.Match(pat, p); err == nil && ok {- return true- }- }- return false-}--// LoggingMiddleware is a JSON slog-based request logger with robust path ignoring.-func LoggingMiddleware(excludePatterns ...string) gin.HandlerFunc {- return func(c *gin.Context) {- reqPath := c.Request.URL.Path- if matchesExcluded(reqPath, excludePatterns) {- // Skip logging entirely for excluded paths- c.Next()- return- }-- start := time.Now()- c.Next()-- latency := time.Since(start)- status := c.Writer.Status()- size := c.Writer.Size()- method := c.Request.Method- ip := c.ClientIP()- ua := c.Request.UserAgent()- query := c.Request.URL.RawQuery- msg := "HTTP request"-- // Log errors as error level if any were recorded in the context- if len(c.Errors) > 0 || status >= 500 {- slog.ErrorContext(- c.Request.Context(),- msg,- slog.Int("status", status),- slog.String("method", method),- slog.String("path", reqPath),- slog.String("query", query),- slog.String("ip", ip),- slog.String("user_agent", ua),- slog.Duration("latency", latency),- slog.Int("bytes_out", size),- slog.String("errors", c.Errors.String()),- )- return- }-- slog.InfoContext(- c.Request.Context(),- msg,- slog.Int("status", status),- slog.String("method", method),- slog.String("path", reqPath),- slog.String("query", query),- slog.String("ip", ip),- slog.String("user_agent", ua),- slog.Duration("latency", latency),- slog.Int("bytes_out", size),- )- }-}