Browse Source

add file and line number in output when report caller is enabled

tags/v1.2.0
David Bariod 1 year ago
parent
commit
d2654b752f
4 changed files with 22 additions and 4 deletions
  1. 5
    0
      formatter.go
  2. 1
    0
      json_formatter.go
  3. 8
    2
      logrus_test.go
  4. 8
    2
      text_formatter.go

+ 5
- 0
formatter.go View File

@@ -10,6 +10,7 @@ const (
10 10
 	FieldKeyTime           = "time"
11 11
 	FieldKeyLogrusError    = "logrus_error"
12 12
 	FieldKeyFunc           = "func"
13
+	FieldKeyFile           = "file"
13 14
 )
14 15
 
15 16
 // The Formatter interface is used to implement a custom Formatter. It takes an
@@ -69,5 +70,9 @@ func prefixFieldClashes(data Fields, fieldMap FieldMap, reportCaller bool) {
69 70
 		if l, ok := data[funcKey]; ok {
70 71
 			data["fields."+funcKey] = l
71 72
 		}
73
+		fileKey := fieldMap.resolve(FieldKeyFile)
74
+		if l, ok := data[fileKey]; ok {
75
+			data["fields."+fileKey] = l
76
+		}
72 77
 	}
73 78
 }

+ 1
- 0
json_formatter.go View File

@@ -83,6 +83,7 @@ func (f *JSONFormatter) Format(entry *Entry) ([]byte, error) {
83 83
 	data[f.FieldMap.resolve(FieldKeyLevel)] = entry.Level.String()
84 84
 	if entry.HasCaller() {
85 85
 		data[f.FieldMap.resolve(FieldKeyFunc)] = entry.Caller.Function
86
+		data[f.FieldMap.resolve(FieldKeyFile)] = fmt.Sprintf("%s:%d", entry.Caller.File, entry.Caller.Line)
86 87
 	}
87 88
 
88 89
 	var b *bytes.Buffer

+ 8
- 2
logrus_test.go View File

@@ -4,6 +4,7 @@ import (
4 4
 	"bytes"
5 5
 	"encoding/json"
6 6
 	"io/ioutil"
7
+	"os"
7 8
 	"sync"
8 9
 	"testing"
9 10
 	"time"
@@ -339,11 +340,14 @@ func TestNestedLoggingReportsCorrectCaller(t *testing.T) {
339 340
 
340 341
 	err := json.Unmarshal(buffer.Bytes(), &fields)
341 342
 	require.NoError(t, err, "should have decoded first message")
342
-	assert.Equal(t, len(fields), 5, "should have msg/time/level/func/context fields")
343
+	assert.Equal(t, 6, len(fields), "should have msg/time/level/func/context fields")
343 344
 	assert.Equal(t, "looks delicious", fields["msg"])
344 345
 	assert.Equal(t, "eating raw fish", fields["context"])
345 346
 	assert.Equal(t,
346 347
 		"github.com/sirupsen/logrus_test.TestNestedLoggingReportsCorrectCaller", fields["func"])
348
+	cwd, err := os.Getwd()
349
+	require.NoError(t, err)
350
+	assert.Equal(t, cwd+"/logrus_test.go:339", fields["file"])
347 351
 
348 352
 	buffer.Reset()
349 353
 
@@ -361,7 +365,7 @@ func TestNestedLoggingReportsCorrectCaller(t *testing.T) {
361 365
 
362 366
 	err = json.Unmarshal(buffer.Bytes(), &fields)
363 367
 	assert.NoError(t, err, "should have decoded second message")
364
-	assert.Equal(t, 10, len(fields), "should have all builtin fields plus foo,bar,baz,...")
368
+	assert.Equal(t, 11, len(fields), "should have all builtin fields plus foo,bar,baz,...")
365 369
 	assert.Equal(t, "Stubblefield", fields["Clyde"])
366 370
 	assert.Equal(t, "Starks", fields["Jab'o"])
367 371
 	assert.Equal(t, "https://www.youtube.com/watch?v=V5DTznu-9v0", fields["uri"])
@@ -371,6 +375,8 @@ func TestNestedLoggingReportsCorrectCaller(t *testing.T) {
371 375
 	assert.Nil(t, fields["fields.msg"], "should not have prefixed previous `msg` entry")
372 376
 	assert.Equal(t,
373 377
 		"github.com/sirupsen/logrus_test.TestNestedLoggingReportsCorrectCaller", fields["func"])
378
+	require.NoError(t, err)
379
+	assert.Equal(t, cwd+"/logrus_test.go:364", fields["file"])
374 380
 
375 381
 	logger.ReportCaller = false // return to default value
376 382
 }

+ 8
- 2
text_formatter.go View File

@@ -126,7 +126,8 @@ func (f *TextFormatter) Format(entry *Entry) ([]byte, error) {
126 126
 		fixedKeys = append(fixedKeys, f.FieldMap.resolve(FieldKeyLogrusError))
127 127
 	}
128 128
 	if entry.HasCaller() {
129
-		fixedKeys = append(fixedKeys, f.FieldMap.resolve(FieldKeyFunc))
129
+		fixedKeys = append(fixedKeys,
130
+			f.FieldMap.resolve(FieldKeyFunc), f.FieldMap.resolve(FieldKeyFile))
130 131
 	}
131 132
 
132 133
 	if !f.DisableSorting {
@@ -172,6 +173,10 @@ func (f *TextFormatter) Format(entry *Entry) ([]byte, error) {
172 173
 				value = entry.Message
173 174
 			case f.FieldMap.resolve(FieldKeyLogrusError):
174 175
 				value = entry.err
176
+			case f.FieldMap.resolve(FieldKeyFunc):
177
+				value = entry.Caller.Function
178
+			case f.FieldMap.resolve(FieldKeyFile):
179
+				value = fmt.Sprintf("%s:%d", entry.Caller.File, entry.Caller.Line)
175 180
 			default:
176 181
 				value = entry.Data[key]
177 182
 			}
@@ -208,7 +213,8 @@ func (f *TextFormatter) printColored(b *bytes.Buffer, entry *Entry, keys []strin
208 213
 	caller := ""
209 214
 
210 215
 	if entry.HasCaller() {
211
-		caller = fmt.Sprintf(" %s()", entry.Caller.Function)
216
+		caller = fmt.Sprintf("%s:%d %s()",
217
+			entry.Caller.File, entry.Caller.Line, entry.Caller.Function)
212 218
 	}
213 219
 
214 220
 	if f.DisableTimestamp {

Loading…
Cancel
Save