feat: expose backend debug log endpoints

This commit is contained in:
2026-04-02 23:51:44 +08:00
parent 9ec25b94f1
commit 98839e9782
5 changed files with 36 additions and 2 deletions

View File

@@ -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、出站请求和应用日志。

View File

@@ -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 {

View File

@@ -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,

View File

@@ -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)
}
}