Files
goclaw/internal/gateway/methods/logs.go
T
viettranx bdb60de7ae chore: upgrade Go 1.25 → 1.26 and apply go fix modernizations
- Update go.mod and Dockerfile to Go 1.26
- Apply `go fix ./...` stdlib modernizations across 170+ files
- Add `go fix` to post-implementation checklist in CLAUDE.md
- Fix go fix misapplied rewrite in loop_history.go
2026-03-10 00:09:15 +07:00

71 lines
1.7 KiB
Go

package methods
import (
"context"
"encoding/json"
"log/slog"
"github.com/nextlevelbuilder/goclaw/internal/gateway"
"github.com/nextlevelbuilder/goclaw/internal/i18n"
"github.com/nextlevelbuilder/goclaw/internal/store"
"github.com/nextlevelbuilder/goclaw/pkg/protocol"
)
// LogsMethods handles logs.tail (start/stop live log tailing).
type LogsMethods struct {
logTee *gateway.LogTee
}
func NewLogsMethods(logTee *gateway.LogTee) *LogsMethods {
return &LogsMethods{logTee: logTee}
}
func (m *LogsMethods) Register(router *gateway.MethodRouter) {
router.Register(protocol.MethodLogsTail, m.handleTail)
}
func (m *LogsMethods) handleTail(ctx context.Context, client *gateway.Client, req *protocol.RequestFrame) {
locale := store.LocaleFromContext(ctx)
var params struct {
Action string `json:"action"`
Level string `json:"level"` // "debug", "info", "warn", "error" (default: "info")
}
if req.Params != nil {
json.Unmarshal(req.Params, &params)
}
switch params.Action {
case "start":
level := parseLogLevel(params.Level)
m.logTee.Subscribe(client, level)
client.SendResponse(protocol.NewOKResponse(req.ID, map[string]any{
"status": "tailing",
"level": params.Level,
}))
case "stop":
m.logTee.Unsubscribe(client.ID())
client.SendResponse(protocol.NewOKResponse(req.ID, map[string]any{
"status": "stopped",
}))
default:
client.SendResponse(protocol.NewErrorResponse(
req.ID,
protocol.ErrInvalidRequest,
i18n.T(locale, i18n.MsgInvalidLogAction),
))
}
}
func parseLogLevel(s string) slog.Level {
switch s {
case "debug":
return slog.LevelDebug
case "warn":
return slog.LevelWarn
case "error":
return slog.LevelError
default:
return slog.LevelInfo
}
}