Skip to content

Commit b3e41ed

Browse files
fix: show actual source file location in logs instead of logger internals
Previously, log messages showed internal logger locations (e.g. logrus/entry.go:304) instead of the actual source file where the logging occurred.
1 parent 8d267ba commit b3e41ed

File tree

1 file changed

+25
-23
lines changed

1 file changed

+25
-23
lines changed

utils/logger/logger.go

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"os"
77
"path/filepath"
8+
"runtime"
89
"strings"
910

1011
"github.com/sirupsen/logrus"
@@ -15,36 +16,24 @@ import (
1516
type CustomTextFormatter struct{}
1617

1718
func (f *CustomTextFormatter) Format(entry *logrus.Entry) ([]byte, error) {
18-
// Get the caller information
19-
var fileLocation string
20-
if entry.HasCaller() {
21-
// Get relative path by removing the workspace root
22-
fullPath := entry.Caller.File
23-
if workspaceRoot, err := filepath.Abs("."); err == nil {
24-
if rel, err := filepath.Rel(workspaceRoot, fullPath); err == nil {
25-
fullPath = rel
26-
}
27-
}
28-
fileLocation = fmt.Sprintf("%s:%d", fullPath, entry.Caller.Line)
29-
}
30-
3119
// Format timestamp
3220
timestamp := entry.Time.Format("2006-01-02T15:04:05-07:00")
3321

3422
// Format fields
3523
var fields string
24+
var location string
3625
if len(entry.Data) > 0 {
3726
var fieldStrings []string
3827
for k, v := range entry.Data {
39-
fieldStrings = append(fieldStrings, fmt.Sprintf("%s=%v", k, v))
28+
if k == "caller" {
29+
location = fmt.Sprintf(" (%v)", v)
30+
} else {
31+
fieldStrings = append(fieldStrings, fmt.Sprintf("%s=%v", k, v))
32+
}
33+
}
34+
if len(fieldStrings) > 0 {
35+
fields = " " + strings.Join(fieldStrings, " ")
4036
}
41-
fields = " " + strings.Join(fieldStrings, " ")
42-
}
43-
44-
// Format the log message
45-
var location string
46-
if fileLocation != "" {
47-
location = fmt.Sprintf(" (%s)", fileLocation)
4837
}
4938

5039
logMessage := fmt.Sprintf("%s [%s]%s %s%s\n",
@@ -93,15 +82,28 @@ func Initialize(logsDir string) error {
9382
fileLogger.SetOutput(lumberjackLogger)
9483
fileLogger.SetLevel(logrus.DebugLevel)
9584

96-
// Enable caller information for file location
97-
fileLogger.SetReportCaller(true)
85+
// We'll handle caller information ourselves
86+
fileLogger.SetReportCaller(false)
9887

9988
return nil
10089
}
10190

10291
// Log logs a message with the given level and fields
10392
func Log(level logrus.Level, msg string, fields logrus.Fields) {
10493
if fileLogger != nil {
94+
// Get caller information
95+
_, file, line, ok := runtime.Caller(2)
96+
if ok {
97+
if workspaceRoot, err := filepath.Abs("."); err == nil {
98+
if rel, err := filepath.Rel(workspaceRoot, file); err == nil {
99+
file = rel
100+
}
101+
}
102+
if fields == nil {
103+
fields = logrus.Fields{}
104+
}
105+
fields["caller"] = fmt.Sprintf("%s:%d", file, line)
106+
}
105107
entry := fileLogger.WithFields(fields)
106108
entry.Log(level, msg)
107109
}

0 commit comments

Comments
 (0)