mirror of
https://github.com/langgenius/dify-plugin-daemon.git
synced 2026-07-22 01:35:24 -04:00
ca3d00229e
* use slog instead of log package and format to new log schema * update the environment name to LOG_OUTPUT_FORMAT * add the env to .env.example * fix log reference error * change the order of milldlewares * delete unused code * fix the concurrently session potential race condition * fix the log format in tests * update the duplicate code * refactor: convert log functions to slog structured format - Change log.Error/Info/Warn/Debug/Panic to accept msg + key-value pairs - Remove printf-style formatting from log functions - Update log calls in internal/cluster, internal/db, internal/core/session_manager - Remove unused 'initialized' variable from log package - Remaining files will be updated in follow-up commits 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * refactor: update all log call sites to use slog structured format Convert all log.Error, log.Info, log.Warn, log.Debug, and log.Panic calls from printf-style formatting to slog key-value pairs. Before: log.Error("failed to do something: %s", err.Error()) After: log.Error("failed to do something", "error", err) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * refactor: update cmd/ log calls to use slog structured format 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * feat: implement GnetLogger for structured logging in gnet * refactor: remove deprecated log visibility functions and related calls * feat: enhance session management with trace and identity context propagation * feat: implement serverless transaction handler and writer for plugin runtime * refactor: rename context field to traceCtx in RealBackwardsInvocation --------- Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com> Co-authored-by: Yeuoly <admin@srmxy.cn>
122 lines
2.3 KiB
Go
122 lines
2.3 KiB
Go
package log
|
|
|
|
import (
|
|
"fmt"
|
|
"log/slog"
|
|
"net/http"
|
|
"runtime"
|
|
"time"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
func TraceMiddleware() gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
ctx := c.Request.Context()
|
|
|
|
traceparent := c.GetHeader("traceparent")
|
|
traceID, spanID, ok := ParseTraceparent(traceparent)
|
|
if !ok {
|
|
traceID = GenerateTraceID()
|
|
spanID = GenerateSpanID()
|
|
}
|
|
ctx = WithTrace(ctx, TraceContext{
|
|
TraceID: traceID,
|
|
SpanID: spanID,
|
|
})
|
|
|
|
identity := Identity{
|
|
TenantID: c.Param("tenant_id"),
|
|
UserID: c.GetHeader("X-User-ID"),
|
|
UserType: c.GetHeader("X-User-Type"),
|
|
}
|
|
ctx = WithIdentity(ctx, identity)
|
|
|
|
c.Request = c.Request.WithContext(ctx)
|
|
c.Next()
|
|
}
|
|
}
|
|
|
|
type LoggerConfig struct {
|
|
SkipPaths []string
|
|
}
|
|
|
|
func LoggerMiddleware() gin.HandlerFunc {
|
|
return LoggerMiddlewareWithConfig(LoggerConfig{})
|
|
}
|
|
|
|
func LoggerMiddlewareWithConfig(config LoggerConfig) gin.HandlerFunc {
|
|
skipPaths := make(map[string]bool)
|
|
for _, path := range config.SkipPaths {
|
|
skipPaths[path] = true
|
|
}
|
|
|
|
return func(c *gin.Context) {
|
|
if skipPaths[c.Request.URL.Path] {
|
|
c.Next()
|
|
return
|
|
}
|
|
|
|
start := time.Now()
|
|
path := c.Request.URL.Path
|
|
query := c.Request.URL.RawQuery
|
|
|
|
c.Next()
|
|
|
|
latency := time.Since(start)
|
|
status := c.Writer.Status()
|
|
method := c.Request.Method
|
|
clientIP := c.ClientIP()
|
|
|
|
if query != "" {
|
|
path = path + "?" + query
|
|
}
|
|
|
|
ctx := c.Request.Context()
|
|
level := slog.LevelInfo
|
|
if status >= 500 {
|
|
level = slog.LevelError
|
|
} else if status >= 400 {
|
|
level = slog.LevelWarn
|
|
}
|
|
|
|
slog.Log(ctx, level, "HTTP request",
|
|
"method", method,
|
|
"path", path,
|
|
"status", status,
|
|
"latency_ms", latency.Milliseconds(),
|
|
"client_ip", clientIP,
|
|
)
|
|
}
|
|
}
|
|
|
|
func RecoveryMiddleware() gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
defer func() {
|
|
if err := recover(); err != nil {
|
|
stack := captureFullStack()
|
|
|
|
ctx := c.Request.Context()
|
|
slog.ErrorContext(ctx, "panic recovered",
|
|
"error", fmt.Sprintf("%v", err),
|
|
"stack_trace", stack,
|
|
)
|
|
|
|
c.AbortWithStatus(http.StatusInternalServerError)
|
|
}
|
|
}()
|
|
c.Next()
|
|
}
|
|
}
|
|
|
|
func captureFullStack() string {
|
|
buf := make([]byte, 4096)
|
|
for {
|
|
n := runtime.Stack(buf, false)
|
|
if n < len(buf) {
|
|
return string(buf[:n])
|
|
}
|
|
buf = make([]byte, len(buf)*2)
|
|
}
|
|
}
|