55 "fmt"
66 "os"
77 "path/filepath"
8+ "runtime"
89 "strings"
910
1011 "github.com/sirupsen/logrus"
@@ -15,36 +16,24 @@ import (
1516type CustomTextFormatter struct {}
1617
1718func (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
10392func 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