diff --git a/README.md b/README.md index 9580e5b..afde4fb 100644 --- a/README.md +++ b/README.md @@ -45,7 +45,14 @@ CKWK_DEBUG_SKIP_SSL_VERIFY=true 调试日志 WS 仅在 `debug` 模式开启,连接地址: ```shell -ws://127.0.0.1:8080/api/debug/ws/logs +ws://127.0.0.1:8080/api/debug/logs/ws +``` + +调试日志快照和下载接口: + +```shell +GET /api/debug/logs +GET /api/debug/logs/download ``` 服务端会保留最近 1000 条内存日志,并持续推送新的入站 HTTP、出站请求和应用日志。 diff --git a/internal/handler/debug_log.go b/internal/handler/debug_log.go index aaa3a37..2ad7a72 100644 --- a/internal/handler/debug_log.go +++ b/internal/handler/debug_log.go @@ -1,9 +1,12 @@ package handler import ( + "encoding/json" + "fmt" "net/http" "time" + "ckwk/internal/dto" "ckwk/pkg/log" "github.com/gin-gonic/gin" @@ -16,6 +19,26 @@ var debugLogUpgrader = websocket.Upgrader{ }, } +func DebugLogs(ctx *gin.Context) { + ctx.JSON(http.StatusOK, dto.Success(map[string]any{ + "list": log.Entries(), + })) +} + +func DebugLogsDownload(ctx *gin.Context) { + entries := log.Entries() + content, err := json.MarshalIndent(entries, "", " ") + if err != nil { + ctx.JSON(http.StatusInternalServerError, dto.Error(500, "日志导出失败")) + return + } + + filename := fmt.Sprintf("wk-debug-logs-%s.json", time.Now().Format("20060102-150405")) + ctx.Header("Content-Type", "application/json; charset=utf-8") + ctx.Header("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s\"", filename)) + ctx.Data(http.StatusOK, "application/json; charset=utf-8", content) +} + func DebugLogWS(ctx *gin.Context) { conn, err := debugLogUpgrader.Upgrade(ctx.Writer, ctx.Request, nil) if err != nil { diff --git a/internal/handler/version.go b/internal/handler/version.go index fda5924..7a27a64 100644 --- a/internal/handler/version.go +++ b/internal/handler/version.go @@ -9,6 +9,7 @@ import ( func Version(ctx *gin.Context) { ctx.JSON(200, dto.Success(map[string]string{ + "Mode": conf.Mode, "Version": conf.Version, "BuildAt": conf.BuildAt, "GitAuthor": conf.GitAuthor, diff --git a/internal/router/router.go b/internal/router/router.go index 06aadff..e501da0 100644 --- a/internal/router/router.go +++ b/internal/router/router.go @@ -71,6 +71,9 @@ func SetupRouter() *gin.Engine { if conf.IsDebugMode() { debug := api.Group("/debug") { + debug.GET("/logs", handler.DebugLogs) + debug.GET("/logs/download", handler.DebugLogsDownload) + debug.GET("/logs/ws", handler.DebugLogWS) debug.GET("/ws/logs", handler.DebugLogWS) } } diff --git a/web/frontend b/web/frontend index 7e102b3..1396592 160000 --- a/web/frontend +++ b/web/frontend @@ -1 +1 @@ -Subproject commit 7e102b3b7647a9ef995ecffd3a1b66ebf737c89f +Subproject commit 13965921418b94d6f4a23c531539b2b067b542ec