mirror of
https://github.com/taigrr/log-socket
synced 2026-03-20 14:52:27 -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:
2
go.mod
2
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
|
||||
|
||||
17
log/log.go
17
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])
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user