Skip to content

Commit

Permalink
Remove sensitivity to file line changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Lisa Ugray committed Jan 2, 2019
1 parent a6668e7 commit e8fd0ba
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions logrus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package logrus_test
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"runtime"
"sync"
"testing"
"time"
Expand Down Expand Up @@ -338,6 +340,7 @@ func TestNestedLoggingReportsCorrectCaller(t *testing.T) {
llog := logger.WithField("context", "eating raw fish")

llog.Info("looks delicious")
_, _, line, _ := runtime.Caller(0)

err := json.Unmarshal(buffer.Bytes(), &fields)
require.NoError(t, err, "should have decoded first message")
Expand All @@ -348,7 +351,7 @@ func TestNestedLoggingReportsCorrectCaller(t *testing.T) {
"github.com/sirupsen/logrus_test.TestNestedLoggingReportsCorrectCaller", fields["func"])
cwd, err := os.Getwd()
require.NoError(t, err)
assert.Equal(t, filepath.ToSlash(cwd+"/logrus_test.go:340"), filepath.ToSlash(fields["file"].(string)))
assert.Equal(t, filepath.ToSlash(fmt.Sprintf("%s/logrus_test.go:%d", cwd, line-1)), filepath.ToSlash(fields["file"].(string)))

buffer.Reset()

Expand All @@ -363,6 +366,7 @@ func TestNestedLoggingReportsCorrectCaller(t *testing.T) {
}).WithFields(Fields{
"James": "Brown",
}).Print("The hardest workin' man in show business")
_, _, line, _ = runtime.Caller(0)

err = json.Unmarshal(buffer.Bytes(), &fields)
assert.NoError(t, err, "should have decoded second message")
Expand All @@ -377,7 +381,7 @@ func TestNestedLoggingReportsCorrectCaller(t *testing.T) {
assert.Equal(t,
"github.com/sirupsen/logrus_test.TestNestedLoggingReportsCorrectCaller", fields["func"])
require.NoError(t, err)
assert.Equal(t, filepath.ToSlash(cwd+"/logrus_test.go:365"), filepath.ToSlash(fields["file"].(string)))
assert.Equal(t, filepath.ToSlash(fmt.Sprintf("%s/logrus_test.go:%d", cwd, line-1)), filepath.ToSlash(fields["file"].(string)))

logger.ReportCaller = false // return to default value
}
Expand Down

0 comments on commit e8fd0ba

Please sign in to comment.