mirror of
https://github.com/wonli/aqi.git
synced 2025-12-24 10:40:58 +08:00
feat(client): add recent logs ring buffer for tracking last 100 log entries
Implement a circular buffer to store recent logs with thread-safe access Add GetRecentLogs method to retrieve logs in chronological order
This commit is contained in:
32
ws/client.go
32
ws/client.go
@@ -59,6 +59,11 @@ type Client struct {
|
||||
|
||||
mu sync.RWMutex
|
||||
Keys map[string]any
|
||||
|
||||
// recent logs ring buffer (last 100 items)
|
||||
recentLogs [100]string
|
||||
recentIdx int
|
||||
recentCount int
|
||||
}
|
||||
|
||||
// Reader 读取
|
||||
@@ -159,6 +164,14 @@ func (c *Client) Log(symbol string, msg ...string) {
|
||||
}
|
||||
|
||||
logger.SugarLog.Info(s)
|
||||
|
||||
c.mu.Lock()
|
||||
c.recentLogs[c.recentIdx] = fmt.Sprintf("%s %s", time.Now().Format(time.RFC3339), s)
|
||||
c.recentIdx = (c.recentIdx + 1) % 100
|
||||
if c.recentCount < 100 {
|
||||
c.recentCount++
|
||||
}
|
||||
c.mu.Unlock()
|
||||
}
|
||||
|
||||
// SendMsg 把消息加入发送队列
|
||||
@@ -201,3 +214,22 @@ func (c *Client) Close() {
|
||||
c.Log("xx", fmt.Sprintf("Close client -> %s", c.IpAddressPort))
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Client) GetRecentLogs() []string {
|
||||
c.mu.RLock()
|
||||
defer c.mu.RUnlock()
|
||||
|
||||
count := c.recentCount
|
||||
if count == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
res := make([]string, count)
|
||||
oldest := (c.recentIdx - count + 100) % 100
|
||||
for i := 0; i < count; i++ {
|
||||
idx := (oldest + i) % 100
|
||||
res[i] = c.recentLogs[idx]
|
||||
}
|
||||
|
||||
return res
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user