From 47bfb5ed986db6770aa995ae64112eefcc93072c Mon Sep 17 00:00:00 2001 From: Tai Groot Date: Tue, 17 Feb 2026 08:30:52 +0000 Subject: [PATCH] fix: stderr namespace filter, fileInfo depth, panic guard, race safety - fix(stderr): CreateClient() with no args so stderr sees all namespaces, not just 'default'. Previously namespaced logs were invisible on stderr. - fix(logger): fileInfo depth offset to 2+FileInfoDepth so Logger methods report the actual caller, not the log package itself. Default depth (0) now correctly shows caller file:line. - fix(panic): guard args[0] access with len(args) > 0 instead of >= 0. Panic/Panicf/Panicln would index out of range when called with no args. - fix(createLog): nil/initialized check before channel send to prevent race with concurrent Destroy calls. - chore: bump go.mod to 1.25.6 --- go.mod | 2 +- log/log.go | 17 ++++++++++------- log/logger.go | 52 +++++++++++++++++++++++++-------------------------- 3 files changed, 37 insertions(+), 34 deletions(-) diff --git a/go.mod b/go.mod index dfc8bd3..ad3cf85 100644 --- a/go.mod +++ b/go.mod @@ -1,5 +1,5 @@ module github.com/taigrr/log-socket/v2 -go 1.25.4 +go 1.25.6 require github.com/gorilla/websocket v1.5.3 diff --git a/log/log.go b/log/log.go index 0256aca..3a82819 100644 --- a/log/log.go +++ b/log/log.go @@ -23,7 +23,7 @@ var ( func init() { namespaces = make(map[string]bool) initColorEnabled() - stderrClient = CreateClient(DefaultNamespace) + stderrClient = CreateClient() stderrClient.SetLogLevel(LTrace) stderrFinished = make(chan bool, 1) go stderrClient.logStdErr() @@ -74,19 +74,19 @@ func Flush() { } func (c *Client) Destroy() error { - var otherClients []*Client if !c.initialized { panic(errors.New("cannot delete uninitialized client, did you use CreateClient?")) } sliceTex.Lock() - c.writer = nil c.initialized = false + var otherClients []*Client for _, x := range clients { - if x.initialized { + if x != c && x.initialized { otherClients = append(otherClients, x) } } clients = otherClients + c.writer = nil sliceTex.Unlock() return nil } @@ -107,6 +107,9 @@ func createLog(e Entry) { sliceTex.Lock() for _, c := range clients { func(c *Client, e Entry) { + if c.writer == nil || !c.initialized { + return + } // Filter by namespace if client has filters specified if !c.matchesNamespace(e.Namespace) { return @@ -421,7 +424,7 @@ func Panic(args ...any) { Namespace: DefaultNamespace, } createLog(e) - if len(args) >= 0 { + if len(args) > 0 { switch args[0].(type) { case error: panic(args[0]) @@ -445,7 +448,7 @@ func Panicf(format string, args ...any) { Namespace: DefaultNamespace, } createLog(e) - if len(args) >= 0 { + if len(args) > 0 { switch args[0].(type) { case error: panic(args[0]) @@ -468,7 +471,7 @@ func Panicln(args ...any) { Namespace: DefaultNamespace, } createLog(e) - if len(args) >= 0 { + if len(args) > 0 { switch args[0].(type) { case error: panic(args[0]) diff --git a/log/logger.go b/log/logger.go index a027eaf..c64d8bc 100644 --- a/log/logger.go +++ b/log/logger.go @@ -28,7 +28,7 @@ func (l Logger) Trace(args ...any) { e := Entry{ Timestamp: time.Now(), Output: output, - File: fileInfo(l.FileInfoDepth), + File: fileInfo(2 + l.FileInfoDepth), Level: "TRACE", level: LTrace, Namespace: l.Namespace, @@ -42,7 +42,7 @@ func (l Logger) Tracef(format string, args ...any) { e := Entry{ Timestamp: time.Now(), Output: output, - File: fileInfo(l.FileInfoDepth), + File: fileInfo(2 + l.FileInfoDepth), Level: "TRACE", level: LTrace, Namespace: l.Namespace, @@ -56,7 +56,7 @@ func (l Logger) Traceln(args ...any) { e := Entry{ Timestamp: time.Now(), Output: output, - File: fileInfo(l.FileInfoDepth), + File: fileInfo(2 + l.FileInfoDepth), Level: "TRACE", level: LTrace, Namespace: l.Namespace, @@ -70,7 +70,7 @@ func (l Logger) Debug(args ...any) { e := Entry{ Timestamp: time.Now(), Output: output, - File: fileInfo(l.FileInfoDepth), + File: fileInfo(2 + l.FileInfoDepth), Level: "DEBUG", level: LDebug, Namespace: l.Namespace, @@ -84,7 +84,7 @@ func (l Logger) Debugf(format string, args ...any) { e := Entry{ Timestamp: time.Now(), Output: output, - File: fileInfo(l.FileInfoDepth), + File: fileInfo(2 + l.FileInfoDepth), Level: "DEBUG", level: LDebug, Namespace: l.Namespace, @@ -98,7 +98,7 @@ func (l Logger) Info(args ...any) { e := Entry{ Timestamp: time.Now(), Output: output, - File: fileInfo(l.FileInfoDepth), + File: fileInfo(2 + l.FileInfoDepth), Level: "INFO", level: LInfo, Namespace: l.Namespace, @@ -112,7 +112,7 @@ func (l Logger) Infof(format string, args ...any) { e := Entry{ Timestamp: time.Now(), Output: output, - File: fileInfo(l.FileInfoDepth), + File: fileInfo(2 + l.FileInfoDepth), Level: "INFO", level: LInfo, Namespace: l.Namespace, @@ -126,7 +126,7 @@ func (l Logger) Infoln(args ...any) { e := Entry{ Timestamp: time.Now(), Output: output, - File: fileInfo(l.FileInfoDepth), + File: fileInfo(2 + l.FileInfoDepth), Level: "INFO", level: LInfo, Namespace: l.Namespace, @@ -140,7 +140,7 @@ func (l Logger) Notice(args ...any) { e := Entry{ Timestamp: time.Now(), Output: output, - File: fileInfo(l.FileInfoDepth), + File: fileInfo(2 + l.FileInfoDepth), Level: "NOTICE", level: LNotice, Namespace: l.Namespace, @@ -154,7 +154,7 @@ func (l Logger) Noticef(format string, args ...any) { e := Entry{ Timestamp: time.Now(), Output: output, - File: fileInfo(l.FileInfoDepth), + File: fileInfo(2 + l.FileInfoDepth), Level: "NOTICE", level: LNotice, Namespace: l.Namespace, @@ -168,7 +168,7 @@ func (l Logger) Noticeln(args ...any) { e := Entry{ Timestamp: time.Now(), Output: output, - File: fileInfo(l.FileInfoDepth), + File: fileInfo(2 + l.FileInfoDepth), Level: "NOTICE", level: LNotice, Namespace: l.Namespace, @@ -182,7 +182,7 @@ func (l Logger) Warn(args ...any) { e := Entry{ Timestamp: time.Now(), Output: output, - File: fileInfo(l.FileInfoDepth), + File: fileInfo(2 + l.FileInfoDepth), Level: "WARN", level: LWarn, Namespace: l.Namespace, @@ -196,7 +196,7 @@ func (l Logger) Warnf(format string, args ...any) { e := Entry{ Timestamp: time.Now(), Output: output, - File: fileInfo(l.FileInfoDepth), + File: fileInfo(2 + l.FileInfoDepth), Level: "WARN", level: LWarn, Namespace: l.Namespace, @@ -210,7 +210,7 @@ func (l Logger) Warnln(args ...any) { e := Entry{ Timestamp: time.Now(), Output: output, - File: fileInfo(l.FileInfoDepth), + File: fileInfo(2 + l.FileInfoDepth), Level: "WARN", level: LWarn, Namespace: l.Namespace, @@ -224,7 +224,7 @@ func (l Logger) Error(args ...any) { e := Entry{ Timestamp: time.Now(), Output: output, - File: fileInfo(l.FileInfoDepth), + File: fileInfo(2 + l.FileInfoDepth), Level: "ERROR", level: LError, Namespace: l.Namespace, @@ -238,7 +238,7 @@ func (l Logger) Errorf(format string, args ...any) { e := Entry{ Timestamp: time.Now(), Output: output, - File: fileInfo(l.FileInfoDepth), + File: fileInfo(2 + l.FileInfoDepth), Level: "ERROR", level: LError, Namespace: l.Namespace, @@ -252,7 +252,7 @@ func (l Logger) Errorln(args ...any) { e := Entry{ Timestamp: time.Now(), Output: output, - File: fileInfo(l.FileInfoDepth), + File: fileInfo(2 + l.FileInfoDepth), Level: "ERROR", level: LError, Namespace: l.Namespace, @@ -266,13 +266,13 @@ func (l Logger) Panic(args ...any) { e := Entry{ Timestamp: time.Now(), Output: output, - File: fileInfo(l.FileInfoDepth), + File: fileInfo(2 + l.FileInfoDepth), Level: "PANIC", level: LPanic, Namespace: l.Namespace, } createLog(e) - if len(args) >= 0 { + if len(args) > 0 { switch args[0].(type) { case error: panic(args[0]) @@ -290,13 +290,13 @@ func (l Logger) Panicf(format string, args ...any) { e := Entry{ Timestamp: time.Now(), Output: output, - File: fileInfo(l.FileInfoDepth), + File: fileInfo(2 + l.FileInfoDepth), Level: "PANIC", level: LPanic, Namespace: l.Namespace, } createLog(e) - if len(args) >= 0 { + if len(args) > 0 { switch args[0].(type) { case error: panic(args[0]) @@ -314,13 +314,13 @@ func (l Logger) Panicln(args ...any) { e := Entry{ Timestamp: time.Now(), Output: output, - File: fileInfo(l.FileInfoDepth), + File: fileInfo(2 + l.FileInfoDepth), Level: "PANIC", level: LPanic, Namespace: l.Namespace, } createLog(e) - if len(args) >= 0 { + if len(args) > 0 { switch args[0].(type) { case error: panic(args[0]) @@ -338,7 +338,7 @@ func (l Logger) Fatal(args ...any) { e := Entry{ Timestamp: time.Now(), Output: output, - File: fileInfo(l.FileInfoDepth), + File: fileInfo(2 + l.FileInfoDepth), Level: "FATAL", level: LFatal, Namespace: l.Namespace, @@ -354,7 +354,7 @@ func (l Logger) Fatalf(format string, args ...any) { e := Entry{ Timestamp: time.Now(), Output: output, - File: fileInfo(l.FileInfoDepth), + File: fileInfo(2 + l.FileInfoDepth), Level: "FATAL", level: LFatal, Namespace: l.Namespace, @@ -370,7 +370,7 @@ func (l Logger) Fatalln(args ...any) { e := Entry{ Timestamp: time.Now(), Output: output, - File: fileInfo(l.FileInfoDepth), + File: fileInfo(2 + l.FileInfoDepth), Level: "FATAL", level: LFatal, Namespace: l.Namespace,