feat(platform)!: log exclusively as structured json
Request access logs, panics, migration output and fatal errors are now emitted through the JSON slog handler. BREAKING CHANGE: UPTIME_LOG_FORMAT is removed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
This commit is contained in:
1 parent
8d1093cd2d
commit
e9c7394c8b
8 files changed
+108
-48
No files matched your search
@@ -18,7 +18,6 @@ const (
|
||||
type Common struct {
|
||||
Edition Edition `env:"UPTIME_EDITION" envDefault:"selfhosted"`
|
||||
LogLevel slog.Level `env:"UPTIME_LOG_LEVEL" envDefault:"info"`
|
||||
LogFormat string `env:"UPTIME_LOG_FORMAT" envDefault:"json"`
|
||||
DatabaseURL string `env:"UPTIME_DATABASE_URL"`
|
||||
RedisURL string `env:"UPTIME_REDIS_URL"`
|
||||
RedisSentinel Sentinel
|
||||
|
||||
+11
-10
@@ -5,16 +5,17 @@ import (
|
||||
"os"
|
||||
|
||||
"git.zb-server.de/ZB-Server/uptimesebi/internal/buildinfo"
|
||||
"git.zb-server.de/ZB-Server/uptimesebi/internal/config"
|
||||
)
|
||||
|
||||
func NewLogger(c config.Common, component string) *slog.Logger {
|
||||
opts := &slog.HandlerOptions{Level: c.LogLevel}
|
||||
var h slog.Handler
|
||||
if c.LogFormat == "text" {
|
||||
h = slog.NewTextHandler(os.Stderr, opts)
|
||||
} else {
|
||||
h = slog.NewJSONHandler(os.Stderr, opts)
|
||||
}
|
||||
return slog.New(h).With("component", component, "version", buildinfo.Version)
|
||||
var logLevel = new(slog.LevelVar)
|
||||
|
||||
// NewLogger installs a JSON logger as the process default so output from the
|
||||
// standard log package and dependencies ends up in the same format.
|
||||
func NewLogger(component string) *slog.Logger {
|
||||
l := slog.New(slog.NewJSONHandler(os.Stderr, &slog.HandlerOptions{Level: logLevel})).
|
||||
With("component", component, "app_version", buildinfo.Version)
|
||||
slog.SetDefault(l)
|
||||
return l
|
||||
}
|
||||
|
||||
func SetLogLevel(l slog.Level) { logLevel.Set(l) }
|
||||
@@ -0,0 +1,60 @@
|
||||
package platform
|
||||
|
||||
import (
|
||||
"io"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"runtime/debug"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
var quietPaths = map[string]bool{"/healthz": true, "/readyz": true}
|
||||
|
||||
func NewRouter(log *slog.Logger) *gin.Engine {
|
||||
gin.SetMode(gin.ReleaseMode)
|
||||
gin.DefaultWriter = io.Discard
|
||||
gin.DefaultErrorWriter = io.Discard
|
||||
r := gin.New()
|
||||
r.Use(accessLog(log), recovery(log))
|
||||
return r
|
||||
}
|
||||
|
||||
func accessLog(log *slog.Logger) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
start := time.Now()
|
||||
c.Next()
|
||||
if quietPaths[c.Request.URL.Path] && c.Writer.Status() < 500 {
|
||||
return
|
||||
}
|
||||
attrs := []any{
|
||||
"method", c.Request.Method,
|
||||
"path", c.Request.URL.Path,
|
||||
"route", c.FullPath(),
|
||||
"status", c.Writer.Status(),
|
||||
"duration_ms", time.Since(start).Milliseconds(),
|
||||
"bytes", c.Writer.Size(),
|
||||
"client_ip", c.ClientIP(),
|
||||
}
|
||||
if len(c.Errors) > 0 {
|
||||
attrs = append(attrs, "errors", c.Errors.Errors())
|
||||
}
|
||||
level := slog.LevelInfo
|
||||
switch {
|
||||
case c.Writer.Status() >= 500:
|
||||
level = slog.LevelError
|
||||
case c.Writer.Status() >= 400:
|
||||
level = slog.LevelWarn
|
||||
}
|
||||
log.Log(c.Request.Context(), level, "http request", attrs...)
|
||||
}
|
||||
}
|
||||
|
||||
func recovery(log *slog.Logger) gin.HandlerFunc {
|
||||
return gin.CustomRecoveryWithWriter(io.Discard, func(c *gin.Context, err any) {
|
||||
log.ErrorContext(c.Request.Context(), "panic",
|
||||
"error", err, "path", c.Request.URL.Path, "stack", string(debug.Stack()))
|
||||
c.AbortWithStatus(http.StatusInternalServerError)
|
||||
})
|
||||
}
|
||||
@@ -8,7 +8,6 @@ import (
|
||||
"os/signal"
|
||||
"syscall"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
"github.com/redis/go-redis/v9"
|
||||
|
||||
@@ -63,10 +62,3 @@ func Connect(ctx context.Context, c config.Common, log *slog.Logger) (*Deps, err
|
||||
}
|
||||
return d, nil
|
||||
}
|
||||
|
||||
func NewRouter() *gin.Engine {
|
||||
gin.SetMode(gin.ReleaseMode)
|
||||
r := gin.New()
|
||||
r.Use(gin.Recovery())
|
||||
return r
|
||||
}
|
||||
Reference in new issue
Block a user