Skip to content

Commit d2e06ea

Browse files
authored
Merge pull request sirupsen#911 from sirupsen/caller_prettyfier_text_formatter
Add a CallerPrettyfier callback to the text formatter
2 parents 6ac99bf + 9611364 commit d2e06ea

File tree

3 files changed

+36
-5
lines changed

3 files changed

+36
-5
lines changed

internal/testutils/testutils.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func LogAndAssertText(t *testing.T, log func(*Logger), assertions func(fields ma
4040
log(logger)
4141

4242
fields := make(map[string]string)
43-
for _, kv := range strings.Split(buffer.String(), " ") {
43+
for _, kv := range strings.Split(strings.TrimRight(buffer.String(), "\n"), " ") {
4444
if !strings.Contains(kv, "=") {
4545
continue
4646
}

logrus_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,17 @@ func TestReportCallerWhenConfigured(t *testing.T) {
5353
assert.Equal(t, "somekindoffunc", fields[FieldKeyFunc])
5454
assert.Equal(t, "thisisafilename", fields[FieldKeyFile])
5555
})
56+
57+
LogAndAssertText(t, func(log *Logger) {
58+
log.ReportCaller = true
59+
log.Formatter.(*TextFormatter).CallerPrettyfier = func(f *runtime.Frame) (string, string) {
60+
return "somekindoffunc", "thisisafilename"
61+
}
62+
log.Print("testWithCallerPrettyfier")
63+
}, func(fields map[string]string) {
64+
assert.Equal(t, "somekindoffunc", fields[FieldKeyFunc])
65+
assert.Equal(t, "thisisafilename", fields[FieldKeyFile])
66+
})
5667
}
5768

5869
func logSomething(t *testing.T, message string) Fields {

text_formatter.go

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,12 @@ type TextFormatter struct {
7272
// FieldKeyMsg: "@message"}}
7373
FieldMap FieldMap
7474

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+
7581
terminalInitOnce sync.Once
7682
}
7783

@@ -113,6 +119,8 @@ func (f *TextFormatter) Format(entry *Entry) ([]byte, error) {
113119
keys = append(keys, k)
114120
}
115121

122+
var funcVal, fileVal string
123+
116124
fixedKeys := make([]string, 0, 4+len(data))
117125
if !f.DisableTimestamp {
118126
fixedKeys = append(fixedKeys, f.FieldMap.resolve(FieldKeyTime))
@@ -127,6 +135,12 @@ func (f *TextFormatter) Format(entry *Entry) ([]byte, error) {
127135
if entry.HasCaller() {
128136
fixedKeys = append(fixedKeys,
129137
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+
}
130144
}
131145

132146
if !f.DisableSorting {
@@ -161,6 +175,7 @@ func (f *TextFormatter) Format(entry *Entry) ([]byte, error) {
161175
if f.isColored() {
162176
f.printColored(b, entry, keys, data, timestampFormat)
163177
} else {
178+
164179
for _, key := range fixedKeys {
165180
var value interface{}
166181
switch {
@@ -173,9 +188,9 @@ func (f *TextFormatter) Format(entry *Entry) ([]byte, error) {
173188
case key == f.FieldMap.resolve(FieldKeyLogrusError):
174189
value = entry.err
175190
case key == f.FieldMap.resolve(FieldKeyFunc) && entry.HasCaller():
176-
value = entry.Caller.Function
191+
value = funcVal
177192
case key == f.FieldMap.resolve(FieldKeyFile) && entry.HasCaller():
178-
value = fmt.Sprintf("%s:%d", entry.Caller.File, entry.Caller.Line)
193+
value = fileVal
179194
default:
180195
value = data[key]
181196
}
@@ -212,8 +227,13 @@ func (f *TextFormatter) printColored(b *bytes.Buffer, entry *Entry, keys []strin
212227
caller := ""
213228

214229
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
217237
}
218238

219239
if f.DisableTimestamp {

0 commit comments

Comments
 (0)