1
0
mirror of https://github.com/taigrr/log-socket synced 2026-03-21 06:02:19 -07:00

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
This commit is contained in:
2026-02-17 08:30:52 +00:00
parent 6a709d3963
commit 47bfb5ed98
3 changed files with 37 additions and 34 deletions

2
go.mod
View File

@@ -1,5 +1,5 @@
module github.com/taigrr/log-socket/v2 module github.com/taigrr/log-socket/v2
go 1.25.4 go 1.25.6
require github.com/gorilla/websocket v1.5.3 require github.com/gorilla/websocket v1.5.3

View File

@@ -23,7 +23,7 @@ var (
func init() { func init() {
namespaces = make(map[string]bool) namespaces = make(map[string]bool)
initColorEnabled() initColorEnabled()
stderrClient = CreateClient(DefaultNamespace) stderrClient = CreateClient()
stderrClient.SetLogLevel(LTrace) stderrClient.SetLogLevel(LTrace)
stderrFinished = make(chan bool, 1) stderrFinished = make(chan bool, 1)
go stderrClient.logStdErr() go stderrClient.logStdErr()
@@ -74,19 +74,19 @@ func Flush() {
} }
func (c *Client) Destroy() error { func (c *Client) Destroy() error {
var otherClients []*Client
if !c.initialized { if !c.initialized {
panic(errors.New("cannot delete uninitialized client, did you use CreateClient?")) panic(errors.New("cannot delete uninitialized client, did you use CreateClient?"))
} }
sliceTex.Lock() sliceTex.Lock()
c.writer = nil
c.initialized = false c.initialized = false
var otherClients []*Client
for _, x := range clients { for _, x := range clients {
if x.initialized { if x != c && x.initialized {
otherClients = append(otherClients, x) otherClients = append(otherClients, x)
} }
} }
clients = otherClients clients = otherClients
c.writer = nil
sliceTex.Unlock() sliceTex.Unlock()
return nil return nil
} }
@@ -107,6 +107,9 @@ func createLog(e Entry) {
sliceTex.Lock() sliceTex.Lock()
for _, c := range clients { for _, c := range clients {
func(c *Client, e Entry) { func(c *Client, e Entry) {
if c.writer == nil || !c.initialized {
return
}
// Filter by namespace if client has filters specified // Filter by namespace if client has filters specified
if !c.matchesNamespace(e.Namespace) { if !c.matchesNamespace(e.Namespace) {
return return
@@ -421,7 +424,7 @@ func Panic(args ...any) {
Namespace: DefaultNamespace, Namespace: DefaultNamespace,
} }
createLog(e) createLog(e)
if len(args) >= 0 { if len(args) > 0 {
switch args[0].(type) { switch args[0].(type) {
case error: case error:
panic(args[0]) panic(args[0])
@@ -445,7 +448,7 @@ func Panicf(format string, args ...any) {
Namespace: DefaultNamespace, Namespace: DefaultNamespace,
} }
createLog(e) createLog(e)
if len(args) >= 0 { if len(args) > 0 {
switch args[0].(type) { switch args[0].(type) {
case error: case error:
panic(args[0]) panic(args[0])
@@ -468,7 +471,7 @@ func Panicln(args ...any) {
Namespace: DefaultNamespace, Namespace: DefaultNamespace,
} }
createLog(e) createLog(e)
if len(args) >= 0 { if len(args) > 0 {
switch args[0].(type) { switch args[0].(type) {
case error: case error:
panic(args[0]) panic(args[0])

View File

@@ -28,7 +28,7 @@ func (l Logger) Trace(args ...any) {
e := Entry{ e := Entry{
Timestamp: time.Now(), Timestamp: time.Now(),
Output: output, Output: output,
File: fileInfo(l.FileInfoDepth), File: fileInfo(2 + l.FileInfoDepth),
Level: "TRACE", Level: "TRACE",
level: LTrace, level: LTrace,
Namespace: l.Namespace, Namespace: l.Namespace,
@@ -42,7 +42,7 @@ func (l Logger) Tracef(format string, args ...any) {
e := Entry{ e := Entry{
Timestamp: time.Now(), Timestamp: time.Now(),
Output: output, Output: output,
File: fileInfo(l.FileInfoDepth), File: fileInfo(2 + l.FileInfoDepth),
Level: "TRACE", Level: "TRACE",
level: LTrace, level: LTrace,
Namespace: l.Namespace, Namespace: l.Namespace,
@@ -56,7 +56,7 @@ func (l Logger) Traceln(args ...any) {
e := Entry{ e := Entry{
Timestamp: time.Now(), Timestamp: time.Now(),
Output: output, Output: output,
File: fileInfo(l.FileInfoDepth), File: fileInfo(2 + l.FileInfoDepth),
Level: "TRACE", Level: "TRACE",
level: LTrace, level: LTrace,
Namespace: l.Namespace, Namespace: l.Namespace,
@@ -70,7 +70,7 @@ func (l Logger) Debug(args ...any) {
e := Entry{ e := Entry{
Timestamp: time.Now(), Timestamp: time.Now(),
Output: output, Output: output,
File: fileInfo(l.FileInfoDepth), File: fileInfo(2 + l.FileInfoDepth),
Level: "DEBUG", Level: "DEBUG",
level: LDebug, level: LDebug,
Namespace: l.Namespace, Namespace: l.Namespace,
@@ -84,7 +84,7 @@ func (l Logger) Debugf(format string, args ...any) {
e := Entry{ e := Entry{
Timestamp: time.Now(), Timestamp: time.Now(),
Output: output, Output: output,
File: fileInfo(l.FileInfoDepth), File: fileInfo(2 + l.FileInfoDepth),
Level: "DEBUG", Level: "DEBUG",
level: LDebug, level: LDebug,
Namespace: l.Namespace, Namespace: l.Namespace,
@@ -98,7 +98,7 @@ func (l Logger) Info(args ...any) {
e := Entry{ e := Entry{
Timestamp: time.Now(), Timestamp: time.Now(),
Output: output, Output: output,
File: fileInfo(l.FileInfoDepth), File: fileInfo(2 + l.FileInfoDepth),
Level: "INFO", Level: "INFO",
level: LInfo, level: LInfo,
Namespace: l.Namespace, Namespace: l.Namespace,
@@ -112,7 +112,7 @@ func (l Logger) Infof(format string, args ...any) {
e := Entry{ e := Entry{
Timestamp: time.Now(), Timestamp: time.Now(),
Output: output, Output: output,
File: fileInfo(l.FileInfoDepth), File: fileInfo(2 + l.FileInfoDepth),
Level: "INFO", Level: "INFO",
level: LInfo, level: LInfo,
Namespace: l.Namespace, Namespace: l.Namespace,
@@ -126,7 +126,7 @@ func (l Logger) Infoln(args ...any) {
e := Entry{ e := Entry{
Timestamp: time.Now(), Timestamp: time.Now(),
Output: output, Output: output,
File: fileInfo(l.FileInfoDepth), File: fileInfo(2 + l.FileInfoDepth),
Level: "INFO", Level: "INFO",
level: LInfo, level: LInfo,
Namespace: l.Namespace, Namespace: l.Namespace,
@@ -140,7 +140,7 @@ func (l Logger) Notice(args ...any) {
e := Entry{ e := Entry{
Timestamp: time.Now(), Timestamp: time.Now(),
Output: output, Output: output,
File: fileInfo(l.FileInfoDepth), File: fileInfo(2 + l.FileInfoDepth),
Level: "NOTICE", Level: "NOTICE",
level: LNotice, level: LNotice,
Namespace: l.Namespace, Namespace: l.Namespace,
@@ -154,7 +154,7 @@ func (l Logger) Noticef(format string, args ...any) {
e := Entry{ e := Entry{
Timestamp: time.Now(), Timestamp: time.Now(),
Output: output, Output: output,
File: fileInfo(l.FileInfoDepth), File: fileInfo(2 + l.FileInfoDepth),
Level: "NOTICE", Level: "NOTICE",
level: LNotice, level: LNotice,
Namespace: l.Namespace, Namespace: l.Namespace,
@@ -168,7 +168,7 @@ func (l Logger) Noticeln(args ...any) {
e := Entry{ e := Entry{
Timestamp: time.Now(), Timestamp: time.Now(),
Output: output, Output: output,
File: fileInfo(l.FileInfoDepth), File: fileInfo(2 + l.FileInfoDepth),
Level: "NOTICE", Level: "NOTICE",
level: LNotice, level: LNotice,
Namespace: l.Namespace, Namespace: l.Namespace,
@@ -182,7 +182,7 @@ func (l Logger) Warn(args ...any) {
e := Entry{ e := Entry{
Timestamp: time.Now(), Timestamp: time.Now(),
Output: output, Output: output,
File: fileInfo(l.FileInfoDepth), File: fileInfo(2 + l.FileInfoDepth),
Level: "WARN", Level: "WARN",
level: LWarn, level: LWarn,
Namespace: l.Namespace, Namespace: l.Namespace,
@@ -196,7 +196,7 @@ func (l Logger) Warnf(format string, args ...any) {
e := Entry{ e := Entry{
Timestamp: time.Now(), Timestamp: time.Now(),
Output: output, Output: output,
File: fileInfo(l.FileInfoDepth), File: fileInfo(2 + l.FileInfoDepth),
Level: "WARN", Level: "WARN",
level: LWarn, level: LWarn,
Namespace: l.Namespace, Namespace: l.Namespace,
@@ -210,7 +210,7 @@ func (l Logger) Warnln(args ...any) {
e := Entry{ e := Entry{
Timestamp: time.Now(), Timestamp: time.Now(),
Output: output, Output: output,
File: fileInfo(l.FileInfoDepth), File: fileInfo(2 + l.FileInfoDepth),
Level: "WARN", Level: "WARN",
level: LWarn, level: LWarn,
Namespace: l.Namespace, Namespace: l.Namespace,
@@ -224,7 +224,7 @@ func (l Logger) Error(args ...any) {
e := Entry{ e := Entry{
Timestamp: time.Now(), Timestamp: time.Now(),
Output: output, Output: output,
File: fileInfo(l.FileInfoDepth), File: fileInfo(2 + l.FileInfoDepth),
Level: "ERROR", Level: "ERROR",
level: LError, level: LError,
Namespace: l.Namespace, Namespace: l.Namespace,
@@ -238,7 +238,7 @@ func (l Logger) Errorf(format string, args ...any) {
e := Entry{ e := Entry{
Timestamp: time.Now(), Timestamp: time.Now(),
Output: output, Output: output,
File: fileInfo(l.FileInfoDepth), File: fileInfo(2 + l.FileInfoDepth),
Level: "ERROR", Level: "ERROR",
level: LError, level: LError,
Namespace: l.Namespace, Namespace: l.Namespace,
@@ -252,7 +252,7 @@ func (l Logger) Errorln(args ...any) {
e := Entry{ e := Entry{
Timestamp: time.Now(), Timestamp: time.Now(),
Output: output, Output: output,
File: fileInfo(l.FileInfoDepth), File: fileInfo(2 + l.FileInfoDepth),
Level: "ERROR", Level: "ERROR",
level: LError, level: LError,
Namespace: l.Namespace, Namespace: l.Namespace,
@@ -266,13 +266,13 @@ func (l Logger) Panic(args ...any) {
e := Entry{ e := Entry{
Timestamp: time.Now(), Timestamp: time.Now(),
Output: output, Output: output,
File: fileInfo(l.FileInfoDepth), File: fileInfo(2 + l.FileInfoDepth),
Level: "PANIC", Level: "PANIC",
level: LPanic, level: LPanic,
Namespace: l.Namespace, Namespace: l.Namespace,
} }
createLog(e) createLog(e)
if len(args) >= 0 { if len(args) > 0 {
switch args[0].(type) { switch args[0].(type) {
case error: case error:
panic(args[0]) panic(args[0])
@@ -290,13 +290,13 @@ func (l Logger) Panicf(format string, args ...any) {
e := Entry{ e := Entry{
Timestamp: time.Now(), Timestamp: time.Now(),
Output: output, Output: output,
File: fileInfo(l.FileInfoDepth), File: fileInfo(2 + l.FileInfoDepth),
Level: "PANIC", Level: "PANIC",
level: LPanic, level: LPanic,
Namespace: l.Namespace, Namespace: l.Namespace,
} }
createLog(e) createLog(e)
if len(args) >= 0 { if len(args) > 0 {
switch args[0].(type) { switch args[0].(type) {
case error: case error:
panic(args[0]) panic(args[0])
@@ -314,13 +314,13 @@ func (l Logger) Panicln(args ...any) {
e := Entry{ e := Entry{
Timestamp: time.Now(), Timestamp: time.Now(),
Output: output, Output: output,
File: fileInfo(l.FileInfoDepth), File: fileInfo(2 + l.FileInfoDepth),
Level: "PANIC", Level: "PANIC",
level: LPanic, level: LPanic,
Namespace: l.Namespace, Namespace: l.Namespace,
} }
createLog(e) createLog(e)
if len(args) >= 0 { if len(args) > 0 {
switch args[0].(type) { switch args[0].(type) {
case error: case error:
panic(args[0]) panic(args[0])
@@ -338,7 +338,7 @@ func (l Logger) Fatal(args ...any) {
e := Entry{ e := Entry{
Timestamp: time.Now(), Timestamp: time.Now(),
Output: output, Output: output,
File: fileInfo(l.FileInfoDepth), File: fileInfo(2 + l.FileInfoDepth),
Level: "FATAL", Level: "FATAL",
level: LFatal, level: LFatal,
Namespace: l.Namespace, Namespace: l.Namespace,
@@ -354,7 +354,7 @@ func (l Logger) Fatalf(format string, args ...any) {
e := Entry{ e := Entry{
Timestamp: time.Now(), Timestamp: time.Now(),
Output: output, Output: output,
File: fileInfo(l.FileInfoDepth), File: fileInfo(2 + l.FileInfoDepth),
Level: "FATAL", Level: "FATAL",
level: LFatal, level: LFatal,
Namespace: l.Namespace, Namespace: l.Namespace,
@@ -370,7 +370,7 @@ func (l Logger) Fatalln(args ...any) {
e := Entry{ e := Entry{
Timestamp: time.Now(), Timestamp: time.Now(),
Output: output, Output: output,
File: fileInfo(l.FileInfoDepth), File: fileInfo(2 + l.FileInfoDepth),
Level: "FATAL", Level: "FATAL",
level: LFatal, level: LFatal,
Namespace: l.Namespace, Namespace: l.Namespace,