refactor(log): clarify append-only logger fields
Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
parent
8225649394
commit
d2d737764f
3 changed files with 32 additions and 32 deletions
|
|
@ -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)
|
||||
}
|
||||
|
|
|
|||
20
log/share.go
20
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)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue