refactor(log): unify Go logging to zap with rotation, strip per-package levels (#16261)

Refactor the Go agent port's logging so every log line — gin access,
agent canvas events, harness warnings, fatal boot errors — flows through
a single common.Logger (zap) backed by a rotated file, with structured
fields, level filtering, and configurable rotation.

---------

Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
Zhichang Yu
2026-06-23 16:21:46 +08:00
committed by GitHub
parent 11e14a8353
commit 2362210caf
29 changed files with 618 additions and 364 deletions

View File

@@ -162,22 +162,38 @@ func (h *SystemHandler) GetVersion(c *gin.Context) {
})
}
// GetLogLevel returns the current log level
// GetLogLevel returns the current log level. The response uses the
// {"level": <value>} shape — the same shape the admin handler's
// /admin/log_level endpoint returns — so the two log endpoints stay
// in lockstep. Per-package level entries that the old pkgLevels
// table carried (e.g. "peewee", "pdfminer") were inert for the Go
// side and are no longer returned.
func (h *SystemHandler) GetLogLevel(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"code": 0,
"message": "success",
"data": common.GetLogLevels(),
"data": gin.H{"level": common.GetLevel()},
})
}
// SetLogLevelRequest set log level request
// SetLogLevelRequest set log level request. PkgName is accepted for
// backward compatibility with clients that previously targeted
// per-package levels; it is silently ignored. Only the global level
// can be set on the Go side.
type SetLogLevelRequest struct {
PkgName string `json:"pkg_name" binding:"required"`
PkgName string `json:"pkg_name"`
Level string `json:"level" binding:"required"`
}
// SetLogLevel sets the log level at runtime
// SetLogLevel sets the log level at runtime.
//
// The "pkg_name and level are required" error message is preserved
// verbatim from the pre-Go-port handler so existing clients that
// inspect `message` on the missing-field path keep working. On the
// Go side `pkg_name` is no longer required (per-package filtering
// is gone), but the message wording is unchanged for backward
// compatibility — only `level` is enforced by binding; `pkg_name`
// is accepted but ignored.
func (h *SystemHandler) SetLogLevel(c *gin.Context) {
var req SetLogLevelRequest
if err := c.ShouldBindJSON(&req); err != nil {
@@ -188,7 +204,7 @@ func (h *SystemHandler) SetLogLevel(c *gin.Context) {
return
}
if err := common.SetPackageLogLevel(req.PkgName, req.Level); err != nil {
if err := common.SetLevel(req.Level); err != nil {
c.JSON(http.StatusOK, gin.H{
"code": common.CodeDataError,
"message": "Invalid log level: " + req.Level,
@@ -196,14 +212,13 @@ func (h *SystemHandler) SetLogLevel(c *gin.Context) {
return
}
config := server.GetConfig()
if req.PkgName == "root" && config != nil {
if config := server.GetConfig(); config != nil {
config.Log.Level = common.GetLevel()
}
c.JSON(http.StatusOK, gin.H{
"code": 0,
"message": "success",
"data": gin.H{"pkg_name": req.PkgName, "level": req.Level},
"data": gin.H{"level": req.Level},
})
}