Files
aqi/ws/context_logs.go
2025-04-19 16:24:32 +08:00

37 lines
739 B
Go

package ws
import (
"fmt"
"github.com/wonli/aqi/logger"
"go.uber.org/zap"
"time"
)
func (c *Context) AddLog(log string, args ...any) {
c.logs = append(c.logs, fmt.Sprintf(log, args...))
}
func (c *Context) FlushLog() {
if c.logs == nil || len(c.logs) == 0 {
return
}
clientInfo := fmt.Sprintf("action(%s), reqIP(%s), reqAt(%s), connectAt(%s), clientId(%s)",
c.Action,
c.Client.IpAddressPort,
c.Client.LastRequestTime.Format(time.RFC3339),
c.Client.ConnectionTime.Format(time.RFC3339),
c.Client.ClientId,
)
runtimeLogs := []zap.Field{
zap.String("", clientInfo),
}
for _, log := range c.logs {
runtimeLogs = append(runtimeLogs, zap.String("", log))
}
logger.RuntimeLog.Info("runtime", runtimeLogs...)
}