commong: move logging functions into */logs.go

The idea is that we should be able to skip it to find the true call
site. However, in some case, there is no true call site as we are in an
internal goroutine (like for Kafka). We can't do too complex things as
it would cost more CPU.

Here is the tentative. We should keep the last valid caller.

```go
// Run adds more context to an event, including "module" and "caller".
func (h contextHook) Run(e *zerolog.Event, _ zerolog.Level, _ string) {
	callStack := stack.Callers()
	callStack = callStack[3:] // Trial and error, there is a test to check it works.

	// We want to get the next caller that is in our own module but that is not logs.go.
	for _, call := range callStack {
		module := call.FunctionName()
		if !strings.HasPrefix(module, stack.ModuleName) || strings.HasSuffix(call.FileName(), "/logs.go") {
			continue
		}
		caller := callStack[0].SourceFile(true)
		e.Str("caller", caller)
		module = strings.SplitN(module, ".", 2)[0]
		e.Str("module", module)
		break
	}
}
```
This commit is contained in:
Vincent Bernat
2025-11-04 22:02:32 +01:00
parent be4ad888c1
commit a3507a3ff2
4 changed files with 41 additions and 31 deletions

View File

@@ -4,6 +4,7 @@
package kafka
import (
"github.com/twmb/franz-go/pkg/kfake"
"github.com/twmb/franz-go/pkg/kgo"
"akvorado/common/helpers"
@@ -43,3 +44,17 @@ func (l *Logger) Log(level kgo.LogLevel, msg string, keyvals ...any) {
l.r.Debug().Fields(keyvals).Msg(msg)
}
}
// Logf logs a message at the specified level for kfake.
func (l *Logger) Logf(level kfake.LogLevel, msg string, keyvals ...any) {
switch level {
case kfake.LogLevelError:
l.r.Error().Fields(keyvals).Msg(msg)
case kfake.LogLevelWarn:
l.r.Warn().Fields(keyvals).Msg(msg)
case kfake.LogLevelInfo:
l.r.Info().Fields(keyvals).Msg(msg)
case kfake.LogLevelDebug:
l.r.Debug().Fields(keyvals).Msg(msg)
}
}

View File

@@ -133,17 +133,3 @@ func InterceptMessages(t *testing.T, cluster *kfake.Cluster, callback func(*kgo.
}
var _ kfake.Logger = &Logger{}
// Logf logs a message at the specified level for kfake.
func (l *Logger) Logf(level kfake.LogLevel, msg string, keyvals ...any) {
switch level {
case kfake.LogLevelError:
l.r.Error().Fields(keyvals).Msg(msg)
case kfake.LogLevelWarn:
l.r.Warn().Fields(keyvals).Msg(msg)
case kfake.LogLevelInfo:
l.r.Info().Fields(keyvals).Msg(msg)
case kfake.LogLevelDebug:
l.r.Debug().Fields(keyvals).Msg(msg)
}
}

View File

@@ -0,0 +1,26 @@
// SPDX-FileCopyrightText: 2025 Free Mobile
// SPDX-License-Identifier: AGPL-3.0-only
package snmp
import (
"fmt"
"akvorado/common/reporter"
)
type goSNMPLogger struct {
r *reporter.Reporter
}
func (l *goSNMPLogger) Print(v ...any) {
if e := l.r.Debug(); e.Enabled() {
e.Msg(fmt.Sprint(v...))
}
}
func (l *goSNMPLogger) Printf(format string, v ...any) {
if e := l.r.Debug(); e.Enabled() {
e.Msg(fmt.Sprintf(format, v...))
}
}

View File

@@ -13,7 +13,6 @@ import (
"github.com/gosnmp/gosnmp"
"akvorado/common/reporter"
"akvorado/outlet/metadata/provider"
)
@@ -210,19 +209,3 @@ func (p *Provider) Poll(ctx context.Context, exporter, agent netip.Addr, port ui
}
return provider.Answer{}, nil
}
type goSNMPLogger struct {
r *reporter.Reporter
}
func (l *goSNMPLogger) Print(v ...any) {
if e := l.r.Debug(); e.Enabled() {
e.Msg(fmt.Sprint(v...))
}
}
func (l *goSNMPLogger) Printf(format string, v ...any) {
if e := l.r.Debug(); e.Enabled() {
e.Msg(fmt.Sprintf(format, v...))
}
}