Skip to content

Commit 77d27b0

Browse files
committed
feat(logging): add runtime log capturing and retrieval functionality
1 parent 28d4c7f commit 77d27b0

4 files changed

Lines changed: 27 additions & 0 deletions

File tree

backend/xray/core.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,13 @@ type Core struct {
3232
startupLogSize int
3333
startupFailure string
3434
startupDiagnosticsEnabled bool
35+
runtimeLogs *startupLogRing
3536
unixSocketPaths []string
3637
logger *nodeLogger.Logger
3738
cancelFunc context.CancelFunc
3839
mu sync.Mutex
3940
startupMu sync.RWMutex
41+
runtimeMu sync.RWMutex
4042
}
4143

4244
func NewXRayCore(executablePath, assetsPath, configPath string, logBufferSize, startupLogTailSize int) (*Core, error) {
@@ -51,6 +53,7 @@ func NewXRayCore(executablePath, assetsPath, configPath string, logBufferSize, s
5153
logsChan: make(chan string, logBufferSize),
5254
logPhase: logPhaseRuntime,
5355
startupLogSize: startupLogTailSize,
56+
runtimeLogs: newStartupLogRing(10),
5457
}
5558

5659
version, err := core.refreshVersion()
@@ -190,6 +193,10 @@ func (c *Core) Start(xConfig *Config, debugMode bool) error {
190193
return errors.New("xray is started already")
191194
}
192195

196+
c.runtimeMu.Lock()
197+
c.runtimeLogs.reset()
198+
c.runtimeMu.Unlock()
199+
193200
c.EnableStartupDiagnostics(c.startupLogSize)
194201
c.setStartupLogPhase()
195202

backend/xray/jobs.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"time"
1010
)
1111

12+
1213
func (x *Xray) startupLogTailSize() int {
1314
if x.cfg != nil && x.cfg.StartupLogTailSize > 0 {
1415
return x.cfg.StartupLogTailSize
@@ -100,6 +101,9 @@ func (x *Xray) checkXrayHealth(baseCtx context.Context) {
100101
// Only restart after multiple consecutive failures
101102
if consecutiveFailures >= maxFailures {
102103
log.Printf("xray health check failed %d times, restarting...", consecutiveFailures)
104+
if tail := x.core.RuntimeLogTail(10); len(tail) > 0 {
105+
log.Printf("last %d xray log lines before restart:\n%s", len(tail), strings.Join(tail, "\n"))
106+
}
103107
if err = x.Restart(); err != nil {
104108
log.Println(err.Error())
105109
} else {

backend/xray/log.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ func (c *Core) captureStartupLogLine(output string) {
5656
}
5757

5858
func (c *Core) captureRuntimeLogLine(output string) {
59+
c.RecordRuntimeLog(output)
5960
// Non-blocking send: skip if channel is full to prevent deadlock
6061
select {
6162
case c.logsChan <- output:

backend/xray/startup_diagnostics.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,21 @@ func (c *Core) StartupLogTail(n int) []string {
166166
return c.startupLogs.tail(n)
167167
}
168168

169+
func (c *Core) RecordRuntimeLog(line string) {
170+
c.runtimeMu.Lock()
171+
defer c.runtimeMu.Unlock()
172+
c.runtimeLogs.add(line)
173+
}
174+
175+
func (c *Core) RuntimeLogTail(n int) []string {
176+
c.runtimeMu.RLock()
177+
defer c.runtimeMu.RUnlock()
178+
if c.runtimeLogs == nil {
179+
return nil
180+
}
181+
return c.runtimeLogs.tail(n)
182+
}
183+
169184
func isStartupFailureLog(line string) bool {
170185
lower := strings.ToLower(line)
171186

0 commit comments

Comments
 (0)