Merge pull request #863 from lugray/generic_log

Add Generic Log functions with level via argument
This commit is contained in:
Richard Poirier
2019-01-02 14:40:11 -08:00
committed by GitHub
3 changed files with 92 additions and 177 deletions
+15 -2
View File
@@ -3,9 +3,11 @@ package logrus_test
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"runtime"
"sync"
"testing"
"time"
@@ -109,6 +111,15 @@ func TestWarn(t *testing.T) {
})
}
func TestLog(t *testing.T) {
LogAndAssertJSON(t, func(log *Logger) {
log.Log(WarnLevel, "test")
}, func(fields Fields) {
assert.Equal(t, "test", fields["msg"])
assert.Equal(t, "warning", fields["level"])
})
}
func TestInfolnShouldAddSpacesBetweenStrings(t *testing.T) {
LogAndAssertJSON(t, func(log *Logger) {
log.Infoln("test", "test")
@@ -338,6 +349,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")
@@ -348,7 +360,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()
@@ -363,6 +375,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")
@@ -377,7 +390,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
}