fix: correct logging if caller has generics

- If the caller function has generics then
`runtime.FuncForPC(...).Name()` will not show the generic types and
instead collapse it to `[...]`. Remove this suffix from the function
name.
- This fixes an issue where the logging of functions that use generics
such as `db.Find` to be logged as `]()` instead of `Find()`, as the last
dot in `[...]` was being used as a cutoff point.
- Unit test added.
This commit is contained in:
Gusted 2025-03-05 00:10:46 +01:00
parent 6b436955fc
commit 101efdd2e7
No known key found for this signature in database
GPG key ID: FD821B732837125F
2 changed files with 28 additions and 1 deletions

View file

@ -191,7 +191,7 @@ func (l *LoggerImpl) Log(skip int, level Level, format string, logArgs ...any) {
if ok {
fn := runtime.FuncForPC(pc)
if fn != nil {
event.Caller = fn.Name() + "()"
event.Caller = strings.TrimSuffix(fn.Name(), "[...]") + "()"
}
}
event.Filename, event.Line = strings.TrimPrefix(filename, projectPackagePrefix), line