@@ -72,6 +72,12 @@ type TextFormatter struct {
72
72
// FieldKeyMsg: "@message"}}
73
73
FieldMap FieldMap
74
74
75
+ // CallerPrettyfier can be set by the user to modify the content
76
+ // of the function and file keys in the json data when ReportCaller is
77
+ // activated. If any of the returned value is the empty string the
78
+ // corresponding key will be removed from json fields.
79
+ CallerPrettyfier func (* runtime.Frame ) (function string , file string )
80
+
75
81
terminalInitOnce sync.Once
76
82
}
77
83
@@ -113,6 +119,8 @@ func (f *TextFormatter) Format(entry *Entry) ([]byte, error) {
113
119
keys = append (keys , k )
114
120
}
115
121
122
+ var funcVal , fileVal string
123
+
116
124
fixedKeys := make ([]string , 0 , 4 + len (data ))
117
125
if ! f .DisableTimestamp {
118
126
fixedKeys = append (fixedKeys , f .FieldMap .resolve (FieldKeyTime ))
@@ -127,6 +135,12 @@ func (f *TextFormatter) Format(entry *Entry) ([]byte, error) {
127
135
if entry .HasCaller () {
128
136
fixedKeys = append (fixedKeys ,
129
137
f .FieldMap .resolve (FieldKeyFunc ), f .FieldMap .resolve (FieldKeyFile ))
138
+ if f .CallerPrettyfier != nil {
139
+ funcVal , fileVal = f .CallerPrettyfier (entry .Caller )
140
+ } else {
141
+ funcVal = entry .Caller .Function
142
+ fileVal = fmt .Sprintf ("%s:%d" , entry .Caller .File , entry .Caller .Line )
143
+ }
130
144
}
131
145
132
146
if ! f .DisableSorting {
@@ -161,6 +175,7 @@ func (f *TextFormatter) Format(entry *Entry) ([]byte, error) {
161
175
if f .isColored () {
162
176
f .printColored (b , entry , keys , data , timestampFormat )
163
177
} else {
178
+
164
179
for _ , key := range fixedKeys {
165
180
var value interface {}
166
181
switch {
@@ -173,9 +188,9 @@ func (f *TextFormatter) Format(entry *Entry) ([]byte, error) {
173
188
case key == f .FieldMap .resolve (FieldKeyLogrusError ):
174
189
value = entry .err
175
190
case key == f .FieldMap .resolve (FieldKeyFunc ) && entry .HasCaller ():
176
- value = entry . Caller . Function
191
+ value = funcVal
177
192
case key == f .FieldMap .resolve (FieldKeyFile ) && entry .HasCaller ():
178
- value = fmt . Sprintf ( "%s:%d" , entry . Caller . File , entry . Caller . Line )
193
+ value = fileVal
179
194
default :
180
195
value = data [key ]
181
196
}
@@ -212,8 +227,13 @@ func (f *TextFormatter) printColored(b *bytes.Buffer, entry *Entry, keys []strin
212
227
caller := ""
213
228
214
229
if entry .HasCaller () {
215
- caller = fmt .Sprintf ("%s:%d %s()" ,
216
- entry .Caller .File , entry .Caller .Line , entry .Caller .Function )
230
+ funcVal := fmt .Sprintf ("%s()" , entry .Caller .Function )
231
+ fileVal := fmt .Sprintf ("%s:%d" , entry .Caller .File , entry .Caller .Line )
232
+
233
+ if f .CallerPrettyfier != nil {
234
+ funcVal , fileVal = f .CallerPrettyfier (entry .Caller )
235
+ }
236
+ caller = fileVal + " " + funcVal
217
237
}
218
238
219
239
if f .DisableTimestamp {
0 commit comments