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>
115 lines
2.5 KiB
Go
115 lines
2.5 KiB
Go
package log
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"log/slog"
|
|
"os"
|
|
"runtime"
|
|
"time"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
const ServiceName = "dify-plugin-daemon"
|
|
|
|
func Init(json bool) {
|
|
handler := NewHandler(Options{
|
|
Level: slog.LevelInfo,
|
|
Service: ServiceName,
|
|
JSON: json,
|
|
Out: os.Stdout,
|
|
})
|
|
slog.SetDefault(slog.New(handler))
|
|
setupGinDebug()
|
|
}
|
|
|
|
func setupGinDebug() {
|
|
gin.DebugPrintRouteFunc = func(httpMethod, absolutePath, handlerName string, nuHandlers int) {
|
|
slog.Debug("gin route registered",
|
|
"method", httpMethod,
|
|
"path", absolutePath,
|
|
"handler", handlerName,
|
|
"handlers_count", nuHandlers,
|
|
)
|
|
}
|
|
}
|
|
|
|
func logWithCaller(ctx context.Context, level slog.Level, msg string, args ...any) {
|
|
logger := slog.Default()
|
|
if !logger.Enabled(ctx, level) {
|
|
return
|
|
}
|
|
var pc uintptr
|
|
var pcs [1]uintptr
|
|
runtime.Callers(3, pcs[:])
|
|
pc = pcs[0]
|
|
r := slog.NewRecord(time.Now(), level, msg, pc)
|
|
r.Add(args...)
|
|
_ = logger.Handler().Handle(ctx, r)
|
|
}
|
|
|
|
func Debug(msg string, args ...any) {
|
|
logWithCaller(context.Background(), slog.LevelDebug, msg, args...)
|
|
}
|
|
|
|
func Info(msg string, args ...any) {
|
|
logWithCaller(context.Background(), slog.LevelInfo, msg, args...)
|
|
}
|
|
|
|
func Warn(msg string, args ...any) {
|
|
logWithCaller(context.Background(), slog.LevelWarn, msg, args...)
|
|
}
|
|
|
|
func Error(msg string, args ...any) {
|
|
logWithCaller(context.Background(), slog.LevelError, msg, args...)
|
|
}
|
|
|
|
func Panic(msg string, args ...any) {
|
|
logWithCaller(context.Background(), slog.LevelError, msg, args...)
|
|
panic(msg)
|
|
}
|
|
|
|
func DebugContext(ctx context.Context, msg string, args ...any) {
|
|
logWithCaller(ctx, slog.LevelDebug, msg, args...)
|
|
}
|
|
|
|
func InfoContext(ctx context.Context, msg string, args ...any) {
|
|
logWithCaller(ctx, slog.LevelInfo, msg, args...)
|
|
}
|
|
|
|
func WarnContext(ctx context.Context, msg string, args ...any) {
|
|
logWithCaller(ctx, slog.LevelWarn, msg, args...)
|
|
}
|
|
|
|
func ErrorContext(ctx context.Context, msg string, args ...any) {
|
|
logWithCaller(ctx, slog.LevelError, msg, args...)
|
|
}
|
|
|
|
func PanicContext(ctx context.Context, msg string, args ...any) {
|
|
logWithCaller(ctx, slog.LevelError, msg, args...)
|
|
panic(msg)
|
|
}
|
|
|
|
func RecoverAndExit() {
|
|
if err := recover(); err != nil {
|
|
stack := captureFullPanicStack()
|
|
slog.Error("panic recovered",
|
|
"error", fmt.Sprintf("%v", err),
|
|
"stack_trace", stack,
|
|
)
|
|
os.Exit(1)
|
|
}
|
|
}
|
|
|
|
func captureFullPanicStack() 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)
|
|
}
|
|
}
|