feat: expose backend debug log endpoints
This commit is contained in:
@@ -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、出站请求和应用日志。
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
Submodule web/frontend updated: 7e102b3b76...1396592141
Reference in New Issue
Block a user