From d2d737764fcdecd8acac480b56085fa2dfc841d8 Mon Sep 17 00:00:00 2001 From: Virgil Date: Sat, 4 Apr 2026 17:31:21 +0000 Subject: [PATCH] refactor(log): clarify append-only logger fields Co-Authored-By: Virgil --- log/access.go | 18 +++++++++--------- log/share.go | 20 ++++++++++---------- proxy_logging_runtime.go | 26 +++++++++++++------------- 3 files changed, 32 insertions(+), 32 deletions(-) diff --git a/log/access.go b/log/access.go index bdee5e3..99eafc2 100644 --- a/log/access.go +++ b/log/access.go @@ -21,7 +21,7 @@ import ( type AccessLog struct { path string mu sync.Mutex - f *os.File + file *os.File closed bool } @@ -41,7 +41,7 @@ func (l *AccessLog) OnLogin(event proxy.Event) { } line := fmt.Sprintf("%s CONNECT %s %s %s\n", - timestamp(), + utcTimestamp(), event.Miner.IP(), event.Miner.User(), event.Miner.Agent(), @@ -58,7 +58,7 @@ func (l *AccessLog) OnClose(event proxy.Event) { } line := fmt.Sprintf("%s CLOSE %s %s rx=%d tx=%d\n", - timestamp(), + utcTimestamp(), event.Miner.IP(), event.Miner.User(), event.Miner.RX(), @@ -82,9 +82,9 @@ func (l *AccessLog) Close() { return } l.closed = true - if l.f != nil { - _ = l.f.Close() - l.f = nil + if l.file != nil { + _ = l.file.Close() + l.file = nil } } @@ -100,13 +100,13 @@ func (l *AccessLog) writeLine(line string) { return } - if l.f == nil { + if l.file == nil { file, errorValue := os.OpenFile(l.path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o644) if errorValue != nil { return } - l.f = file + l.file = file } - _, _ = l.f.WriteString(line) + _, _ = l.file.WriteString(line) } diff --git a/log/share.go b/log/share.go index cb8a1e6..53069b8 100644 --- a/log/share.go +++ b/log/share.go @@ -17,7 +17,7 @@ import ( type ShareLog struct { path string mu sync.Mutex - f *os.File + file *os.File closed bool } @@ -37,7 +37,7 @@ func (l *ShareLog) OnAccept(event proxy.Event) { } line := fmt.Sprintf("%s ACCEPT %s diff=%d latency=%dms\n", - timestamp(), + utcTimestamp(), event.Miner.User(), event.Diff, event.Latency, @@ -54,7 +54,7 @@ func (l *ShareLog) OnReject(event proxy.Event) { } line := fmt.Sprintf("%s REJECT %s reason=%q\n", - timestamp(), + utcTimestamp(), event.Miner.User(), event.Error, ) @@ -76,9 +76,9 @@ func (l *ShareLog) Close() { return } l.closed = true - if l.f != nil { - _ = l.f.Close() - l.f = nil + if l.file != nil { + _ = l.file.Close() + l.file = nil } } @@ -94,17 +94,17 @@ func (l *ShareLog) writeLine(line string) { return } - if l.f == nil { + if l.file == nil { file, errorValue := os.OpenFile(l.path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o644) if errorValue != nil { return } - l.f = file + l.file = file } - _, _ = l.f.WriteString(line) + _, _ = l.file.WriteString(line) } -func timestamp() string { +func utcTimestamp() string { return time.Now().UTC().Format(time.RFC3339) } diff --git a/proxy_logging_runtime.go b/proxy_logging_runtime.go index 4e93d4d..4d137d4 100644 --- a/proxy_logging_runtime.go +++ b/proxy_logging_runtime.go @@ -8,10 +8,10 @@ import ( ) type appendLineLogger struct { - path string - mu sync.Mutex - openFile *os.File - closed bool + path string + mu sync.Mutex + file *os.File + closed bool } func newAppendLineLogger(path string) *appendLineLogger { @@ -30,15 +30,15 @@ func (l *appendLineLogger) writeLine(line string) { return } - if l.openFile == nil { + if l.file == nil { file, errorValue := os.OpenFile(l.path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o644) if errorValue != nil { return } - l.openFile = file + l.file = file } - _, _ = l.openFile.WriteString(line) + _, _ = l.file.WriteString(line) } func (l *appendLineLogger) setPath(path string) { @@ -53,9 +53,9 @@ func (l *appendLineLogger) setPath(path string) { return } - if l.openFile != nil { - _ = l.openFile.Close() - l.openFile = nil + if l.file != nil { + _ = l.file.Close() + l.file = nil } l.closed = false l.path = path @@ -73,9 +73,9 @@ func (l *appendLineLogger) close() { return } l.closed = true - if l.openFile != nil { - _ = l.openFile.Close() - l.openFile = nil + if l.file != nil { + _ = l.file.Close() + l.file = nil } }