123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716 |
- package logrus_test
- import (
- "bytes"
- "encoding/json"
- "io/ioutil"
- "os"
- "path/filepath"
- "sync"
- "testing"
- "time"
- "github.com/stretchr/testify/assert"
- "github.com/stretchr/testify/require"
- . "github.com/sirupsen/logrus"
- . "github.com/sirupsen/logrus/internal/testutils"
- )
- // TestReportCaller verifies that when ReportCaller is set, the 'func' field
- // is added, and when it is unset it is not set or modified
- // Verify that functions within the Logrus package aren't considered when
- // discovering the caller.
- func TestReportCallerWhenConfigured(t *testing.T) {
- LogAndAssertJSON(t, func(log *Logger) {
- log.ReportCaller = false
- log.Print("testNoCaller")
- }, func(fields Fields) {
- assert.Equal(t, "testNoCaller", fields["msg"])
- assert.Equal(t, "info", fields["level"])
- assert.Equal(t, nil, fields["func"])
- })
- LogAndAssertJSON(t, func(log *Logger) {
- log.ReportCaller = true
- log.Print("testWithCaller")
- }, func(fields Fields) {
- assert.Equal(t, "testWithCaller", fields["msg"])
- assert.Equal(t, "info", fields["level"])
- assert.Equal(t,
- "github.com/sirupsen/logrus_test.TestReportCallerWhenConfigured.func3", fields["func"])
- })
- }
- func logSomething(t *testing.T, message string) Fields {
- var buffer bytes.Buffer
- var fields Fields
- logger := New()
- logger.Out = &buffer
- logger.Formatter = new(JSONFormatter)
- logger.ReportCaller = true
- entry := logger.WithFields(Fields{
- "foo": "bar",
- })
- entry.Info(message)
- err := json.Unmarshal(buffer.Bytes(), &fields)
- assert.Nil(t, err)
- return fields
- }
- // TestReportCallerHelperDirect - verify reference when logging from a regular function
- func TestReportCallerHelperDirect(t *testing.T) {
- fields := logSomething(t, "direct")
- assert.Equal(t, "direct", fields["msg"])
- assert.Equal(t, "info", fields["level"])
- assert.Regexp(t, "github.com/.*/logrus_test.logSomething", fields["func"])
- }
- // TestReportCallerHelperDirect - verify reference when logging from a function called via pointer
- func TestReportCallerHelperViaPointer(t *testing.T) {
- fptr := logSomething
- fields := fptr(t, "via pointer")
- assert.Equal(t, "via pointer", fields["msg"])
- assert.Equal(t, "info", fields["level"])
- assert.Regexp(t, "github.com/.*/logrus_test.logSomething", fields["func"])
- }
- func TestPrint(t *testing.T) {
- LogAndAssertJSON(t, func(log *Logger) {
- log.Print("test")
- }, func(fields Fields) {
- assert.Equal(t, "test", fields["msg"])
- assert.Equal(t, "info", fields["level"])
- })
- }
- func TestInfo(t *testing.T) {
- LogAndAssertJSON(t, func(log *Logger) {
- log.Info("test")
- }, func(fields Fields) {
- assert.Equal(t, "test", fields["msg"])
- assert.Equal(t, "info", fields["level"])
- })
- }
- func TestWarn(t *testing.T) {
- LogAndAssertJSON(t, func(log *Logger) {
- log.Warn("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")
- }, func(fields Fields) {
- assert.Equal(t, "test test", fields["msg"])
- })
- }
- func TestInfolnShouldAddSpacesBetweenStringAndNonstring(t *testing.T) {
- LogAndAssertJSON(t, func(log *Logger) {
- log.Infoln("test", 10)
- }, func(fields Fields) {
- assert.Equal(t, "test 10", fields["msg"])
- })
- }
- func TestInfolnShouldAddSpacesBetweenTwoNonStrings(t *testing.T) {
- LogAndAssertJSON(t, func(log *Logger) {
- log.Infoln(10, 10)
- }, func(fields Fields) {
- assert.Equal(t, "10 10", fields["msg"])
- })
- }
- func TestInfoShouldAddSpacesBetweenTwoNonStrings(t *testing.T) {
- LogAndAssertJSON(t, func(log *Logger) {
- log.Infoln(10, 10)
- }, func(fields Fields) {
- assert.Equal(t, "10 10", fields["msg"])
- })
- }
- func TestInfoShouldNotAddSpacesBetweenStringAndNonstring(t *testing.T) {
- LogAndAssertJSON(t, func(log *Logger) {
- log.Info("test", 10)
- }, func(fields Fields) {
- assert.Equal(t, "test10", fields["msg"])
- })
- }
- func TestInfoShouldNotAddSpacesBetweenStrings(t *testing.T) {
- LogAndAssertJSON(t, func(log *Logger) {
- log.Info("test", "test")
- }, func(fields Fields) {
- assert.Equal(t, "testtest", fields["msg"])
- })
- }
- func TestWithFieldsShouldAllowAssignments(t *testing.T) {
- var buffer bytes.Buffer
- var fields Fields
- logger := New()
- logger.Out = &buffer
- logger.Formatter = new(JSONFormatter)
- localLog := logger.WithFields(Fields{
- "key1": "value1",
- })
- localLog.WithField("key2", "value2").Info("test")
- err := json.Unmarshal(buffer.Bytes(), &fields)
- assert.Nil(t, err)
- assert.Equal(t, "value2", fields["key2"])
- assert.Equal(t, "value1", fields["key1"])
- buffer = bytes.Buffer{}
- fields = Fields{}
- localLog.Info("test")
- err = json.Unmarshal(buffer.Bytes(), &fields)
- assert.Nil(t, err)
- _, ok := fields["key2"]
- assert.Equal(t, false, ok)
- assert.Equal(t, "value1", fields["key1"])
- }
- func TestUserSuppliedFieldDoesNotOverwriteDefaults(t *testing.T) {
- LogAndAssertJSON(t, func(log *Logger) {
- log.WithField("msg", "hello").Info("test")
- }, func(fields Fields) {
- assert.Equal(t, "test", fields["msg"])
- })
- }
- func TestUserSuppliedMsgFieldHasPrefix(t *testing.T) {
- LogAndAssertJSON(t, func(log *Logger) {
- log.WithField("msg", "hello").Info("test")
- }, func(fields Fields) {
- assert.Equal(t, "test", fields["msg"])
- assert.Equal(t, "hello", fields["fields.msg"])
- })
- }
- func TestUserSuppliedTimeFieldHasPrefix(t *testing.T) {
- LogAndAssertJSON(t, func(log *Logger) {
- log.WithField("time", "hello").Info("test")
- }, func(fields Fields) {
- assert.Equal(t, "hello", fields["fields.time"])
- })
- }
- func TestUserSuppliedLevelFieldHasPrefix(t *testing.T) {
- LogAndAssertJSON(t, func(log *Logger) {
- log.WithField("level", 1).Info("test")
- }, func(fields Fields) {
- assert.Equal(t, "info", fields["level"])
- assert.Equal(t, 1.0, fields["fields.level"]) // JSON has floats only
- })
- }
- func TestDefaultFieldsAreNotPrefixed(t *testing.T) {
- LogAndAssertText(t, func(log *Logger) {
- ll := log.WithField("herp", "derp")
- ll.Info("hello")
- ll.Info("bye")
- }, func(fields map[string]string) {
- for _, fieldName := range []string{"fields.level", "fields.time", "fields.msg"} {
- if _, ok := fields[fieldName]; ok {
- t.Fatalf("should not have prefixed %q: %v", fieldName, fields)
- }
- }
- })
- }
- func TestWithTimeShouldOverrideTime(t *testing.T) {
- now := time.Now().Add(24 * time.Hour)
- LogAndAssertJSON(t, func(log *Logger) {
- log.WithTime(now).Info("foobar")
- }, func(fields Fields) {
- assert.Equal(t, fields["time"], now.Format(time.RFC3339))
- })
- }
- func TestWithTimeShouldNotOverrideFields(t *testing.T) {
- now := time.Now().Add(24 * time.Hour)
- LogAndAssertJSON(t, func(log *Logger) {
- log.WithField("herp", "derp").WithTime(now).Info("blah")
- }, func(fields Fields) {
- assert.Equal(t, fields["time"], now.Format(time.RFC3339))
- assert.Equal(t, fields["herp"], "derp")
- })
- }
- func TestWithFieldShouldNotOverrideTime(t *testing.T) {
- now := time.Now().Add(24 * time.Hour)
- LogAndAssertJSON(t, func(log *Logger) {
- log.WithTime(now).WithField("herp", "derp").Info("blah")
- }, func(fields Fields) {
- assert.Equal(t, fields["time"], now.Format(time.RFC3339))
- assert.Equal(t, fields["herp"], "derp")
- })
- }
- func TestTimeOverrideMultipleLogs(t *testing.T) {
- var buffer bytes.Buffer
- var firstFields, secondFields Fields
- logger := New()
- logger.Out = &buffer
- formatter := new(JSONFormatter)
- formatter.TimestampFormat = time.StampMilli
- logger.Formatter = formatter
- llog := logger.WithField("herp", "derp")
- llog.Info("foo")
- err := json.Unmarshal(buffer.Bytes(), &firstFields)
- assert.NoError(t, err, "should have decoded first message")
- buffer.Reset()
- time.Sleep(10 * time.Millisecond)
- llog.Info("bar")
- err = json.Unmarshal(buffer.Bytes(), &secondFields)
- assert.NoError(t, err, "should have decoded second message")
- assert.NotEqual(t, firstFields["time"], secondFields["time"], "timestamps should not be equal")
- }
- func TestDoubleLoggingDoesntPrefixPreviousFields(t *testing.T) {
- var buffer bytes.Buffer
- var fields Fields
- logger := New()
- logger.Out = &buffer
- logger.Formatter = new(JSONFormatter)
- llog := logger.WithField("context", "eating raw fish")
- llog.Info("looks delicious")
- err := json.Unmarshal(buffer.Bytes(), &fields)
- assert.NoError(t, err, "should have decoded first message")
- assert.Equal(t, len(fields), 4, "should only have msg/time/level/context fields")
- assert.Equal(t, fields["msg"], "looks delicious")
- assert.Equal(t, fields["context"], "eating raw fish")
- buffer.Reset()
- llog.Warn("omg it is!")
- err = json.Unmarshal(buffer.Bytes(), &fields)
- assert.NoError(t, err, "should have decoded second message")
- assert.Equal(t, len(fields), 4, "should only have msg/time/level/context fields")
- assert.Equal(t, "omg it is!", fields["msg"])
- assert.Equal(t, "eating raw fish", fields["context"])
- assert.Nil(t, fields["fields.msg"], "should not have prefixed previous `msg` entry")
- }
- func TestNestedLoggingReportsCorrectCaller(t *testing.T) {
- var buffer bytes.Buffer
- var fields Fields
- logger := New()
- logger.Out = &buffer
- logger.Formatter = new(JSONFormatter)
- logger.ReportCaller = true
- llog := logger.WithField("context", "eating raw fish")
- llog.Info("looks delicious")
- err := json.Unmarshal(buffer.Bytes(), &fields)
- require.NoError(t, err, "should have decoded first message")
- assert.Equal(t, 6, len(fields), "should have msg/time/level/func/context fields")
- assert.Equal(t, "looks delicious", fields["msg"])
- assert.Equal(t, "eating raw fish", fields["context"])
- assert.Equal(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)))
- buffer.Reset()
- logger.WithFields(Fields{
- "Clyde": "Stubblefield",
- }).WithFields(Fields{
- "Jab'o": "Starks",
- }).WithFields(Fields{
- "uri": "https://www.youtube.com/watch?v=V5DTznu-9v0",
- }).WithFields(Fields{
- "func": "y drummer",
- }).WithFields(Fields{
- "James": "Brown",
- }).Print("The hardest workin' man in show business")
- err = json.Unmarshal(buffer.Bytes(), &fields)
- assert.NoError(t, err, "should have decoded second message")
- assert.Equal(t, 11, len(fields), "should have all builtin fields plus foo,bar,baz,...")
- assert.Equal(t, "Stubblefield", fields["Clyde"])
- assert.Equal(t, "Starks", fields["Jab'o"])
- assert.Equal(t, "https://www.youtube.com/watch?v=V5DTznu-9v0", fields["uri"])
- assert.Equal(t, "y drummer", fields["fields.func"])
- assert.Equal(t, "Brown", fields["James"])
- assert.Equal(t, "The hardest workin' man in show business", fields["msg"])
- assert.Nil(t, fields["fields.msg"], "should not have prefixed previous `msg` entry")
- 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)))
- logger.ReportCaller = false // return to default value
- }
- func logLoop(iterations int, reportCaller bool) {
- var buffer bytes.Buffer
- logger := New()
- logger.Out = &buffer
- logger.Formatter = new(JSONFormatter)
- logger.ReportCaller = reportCaller
- for i := 0; i < iterations; i++ {
- logger.Infof("round %d of %d", i, iterations)
- }
- }
- // Assertions for upper bounds to reporting overhead
- func TestCallerReportingOverhead(t *testing.T) {
- iterations := 5000
- before := time.Now()
- logLoop(iterations, false)
- during := time.Now()
- logLoop(iterations, true)
- after := time.Now()
- elapsedNotReporting := during.Sub(before).Nanoseconds()
- elapsedReporting := after.Sub(during).Nanoseconds()
- maxDelta := 1 * time.Second
- assert.WithinDuration(t, during, before, maxDelta,
- "%d log calls without caller name lookup takes less than %d second(s) (was %d nanoseconds)",
- iterations, maxDelta.Seconds(), elapsedNotReporting)
- assert.WithinDuration(t, after, during, maxDelta,
- "%d log calls without caller name lookup takes less than %d second(s) (was %d nanoseconds)",
- iterations, maxDelta.Seconds(), elapsedReporting)
- }
- // benchmarks for both with and without caller-function reporting
- func BenchmarkWithoutCallerTracing(b *testing.B) {
- for i := 0; i < b.N; i++ {
- logLoop(1000, false)
- }
- }
- func BenchmarkWithCallerTracing(b *testing.B) {
- for i := 0; i < b.N; i++ {
- logLoop(1000, true)
- }
- }
- func TestConvertLevelToString(t *testing.T) {
- assert.Equal(t, "trace", TraceLevel.String())
- assert.Equal(t, "debug", DebugLevel.String())
- assert.Equal(t, "info", InfoLevel.String())
- assert.Equal(t, "warning", WarnLevel.String())
- assert.Equal(t, "error", ErrorLevel.String())
- assert.Equal(t, "fatal", FatalLevel.String())
- assert.Equal(t, "panic", PanicLevel.String())
- }
- func TestParseLevel(t *testing.T) {
- l, err := ParseLevel("panic")
- assert.Nil(t, err)
- assert.Equal(t, PanicLevel, l)
- l, err = ParseLevel("PANIC")
- assert.Nil(t, err)
- assert.Equal(t, PanicLevel, l)
- l, err = ParseLevel("fatal")
- assert.Nil(t, err)
- assert.Equal(t, FatalLevel, l)
- l, err = ParseLevel("FATAL")
- assert.Nil(t, err)
- assert.Equal(t, FatalLevel, l)
- l, err = ParseLevel("error")
- assert.Nil(t, err)
- assert.Equal(t, ErrorLevel, l)
- l, err = ParseLevel("ERROR")
- assert.Nil(t, err)
- assert.Equal(t, ErrorLevel, l)
- l, err = ParseLevel("warn")
- assert.Nil(t, err)
- assert.Equal(t, WarnLevel, l)
- l, err = ParseLevel("WARN")
- assert.Nil(t, err)
- assert.Equal(t, WarnLevel, l)
- l, err = ParseLevel("warning")
- assert.Nil(t, err)
- assert.Equal(t, WarnLevel, l)
- l, err = ParseLevel("WARNING")
- assert.Nil(t, err)
- assert.Equal(t, WarnLevel, l)
- l, err = ParseLevel("info")
- assert.Nil(t, err)
- assert.Equal(t, InfoLevel, l)
- l, err = ParseLevel("INFO")
- assert.Nil(t, err)
- assert.Equal(t, InfoLevel, l)
- l, err = ParseLevel("debug")
- assert.Nil(t, err)
- assert.Equal(t, DebugLevel, l)
- l, err = ParseLevel("DEBUG")
- assert.Nil(t, err)
- assert.Equal(t, DebugLevel, l)
- l, err = ParseLevel("trace")
- assert.Nil(t, err)
- assert.Equal(t, TraceLevel, l)
- l, err = ParseLevel("TRACE")
- assert.Nil(t, err)
- assert.Equal(t, TraceLevel, l)
- l, err = ParseLevel("invalid")
- assert.Equal(t, "not a valid logrus Level: \"invalid\"", err.Error())
- }
- func TestUnmarshalText(t *testing.T) {
- var u Level
- for _, level := range AllLevels {
- t.Run(level.String(), func(t *testing.T) {
- assert.NoError(t, u.UnmarshalText([]byte(level.String())))
- assert.Equal(t, level, u)
- })
- }
- t.Run("invalid", func(t *testing.T) {
- assert.Error(t, u.UnmarshalText([]byte("invalid")))
- })
- }
- func TestGetSetLevelRace(t *testing.T) {
- wg := sync.WaitGroup{}
- for i := 0; i < 100; i++ {
- wg.Add(1)
- go func(i int) {
- defer wg.Done()
- if i%2 == 0 {
- SetLevel(InfoLevel)
- } else {
- GetLevel()
- }
- }(i)
- }
- wg.Wait()
- }
- func TestLoggingRace(t *testing.T) {
- logger := New()
- var wg sync.WaitGroup
- wg.Add(100)
- for i := 0; i < 100; i++ {
- go func() {
- logger.Info("info")
- wg.Done()
- }()
- }
- wg.Wait()
- }
- func TestLoggingRaceWithHooksOnEntry(t *testing.T) {
- logger := New()
- hook := new(ModifyHook)
- logger.AddHook(hook)
- entry := logger.WithField("context", "clue")
- var wg sync.WaitGroup
- wg.Add(100)
- for i := 0; i < 100; i++ {
- go func() {
- entry.Info("info")
- wg.Done()
- }()
- }
- wg.Wait()
- }
- func TestReplaceHooks(t *testing.T) {
- old, cur := &TestHook{}, &TestHook{}
- logger := New()
- logger.SetOutput(ioutil.Discard)
- logger.AddHook(old)
- hooks := make(LevelHooks)
- hooks.Add(cur)
- replaced := logger.ReplaceHooks(hooks)
- logger.Info("test")
- assert.Equal(t, old.Fired, false)
- assert.Equal(t, cur.Fired, true)
- logger.ReplaceHooks(replaced)
- logger.Info("test")
- assert.Equal(t, old.Fired, true)
- }
- // Compile test
- func TestLogrusInterfaces(t *testing.T) {
- var buffer bytes.Buffer
- // This verifies FieldLogger and Ext1FieldLogger work as designed.
- // Please don't use them. Use Logger and Entry directly.
- fn := func(xl Ext1FieldLogger) {
- var l FieldLogger = xl
- b := l.WithField("key", "value")
- b.Debug("Test")
- }
- // test logger
- logger := New()
- logger.Out = &buffer
- fn(logger)
- // test Entry
- e := logger.WithField("another", "value")
- fn(e)
- }
- // Implements io.Writer using channels for synchronization, so we can wait on
- // the Entry.Writer goroutine to write in a non-racey way. This does assume that
- // there is a single call to Logger.Out for each message.
- type channelWriter chan []byte
- func (cw channelWriter) Write(p []byte) (int, error) {
- cw <- p
- return len(p), nil
- }
- func TestEntryWriter(t *testing.T) {
- cw := channelWriter(make(chan []byte, 1))
- log := New()
- log.Out = cw
- log.Formatter = new(JSONFormatter)
- log.WithField("foo", "bar").WriterLevel(WarnLevel).Write([]byte("hello\n"))
- bs := <-cw
- var fields Fields
- err := json.Unmarshal(bs, &fields)
- assert.Nil(t, err)
- assert.Equal(t, fields["foo"], "bar")
- assert.Equal(t, fields["level"], "warning")
- }
- func TestLogLevelEnabled(t *testing.T) {
- log := New()
- log.SetLevel(PanicLevel)
- assert.Equal(t, true, log.IsLevelEnabled(PanicLevel))
- assert.Equal(t, false, log.IsLevelEnabled(FatalLevel))
- assert.Equal(t, false, log.IsLevelEnabled(ErrorLevel))
- assert.Equal(t, false, log.IsLevelEnabled(WarnLevel))
- assert.Equal(t, false, log.IsLevelEnabled(InfoLevel))
- assert.Equal(t, false, log.IsLevelEnabled(DebugLevel))
- assert.Equal(t, false, log.IsLevelEnabled(TraceLevel))
- log.SetLevel(FatalLevel)
- assert.Equal(t, true, log.IsLevelEnabled(PanicLevel))
- assert.Equal(t, true, log.IsLevelEnabled(FatalLevel))
- assert.Equal(t, false, log.IsLevelEnabled(ErrorLevel))
- assert.Equal(t, false, log.IsLevelEnabled(WarnLevel))
- assert.Equal(t, false, log.IsLevelEnabled(InfoLevel))
- assert.Equal(t, false, log.IsLevelEnabled(DebugLevel))
- assert.Equal(t, false, log.IsLevelEnabled(TraceLevel))
- log.SetLevel(ErrorLevel)
- assert.Equal(t, true, log.IsLevelEnabled(PanicLevel))
- assert.Equal(t, true, log.IsLevelEnabled(FatalLevel))
- assert.Equal(t, true, log.IsLevelEnabled(ErrorLevel))
- assert.Equal(t, false, log.IsLevelEnabled(WarnLevel))
- assert.Equal(t, false, log.IsLevelEnabled(InfoLevel))
- assert.Equal(t, false, log.IsLevelEnabled(DebugLevel))
- assert.Equal(t, false, log.IsLevelEnabled(TraceLevel))
- log.SetLevel(WarnLevel)
- assert.Equal(t, true, log.IsLevelEnabled(PanicLevel))
- assert.Equal(t, true, log.IsLevelEnabled(FatalLevel))
- assert.Equal(t, true, log.IsLevelEnabled(ErrorLevel))
- assert.Equal(t, true, log.IsLevelEnabled(WarnLevel))
- assert.Equal(t, false, log.IsLevelEnabled(InfoLevel))
- assert.Equal(t, false, log.IsLevelEnabled(DebugLevel))
- assert.Equal(t, false, log.IsLevelEnabled(TraceLevel))
- log.SetLevel(InfoLevel)
- assert.Equal(t, true, log.IsLevelEnabled(PanicLevel))
- assert.Equal(t, true, log.IsLevelEnabled(FatalLevel))
- assert.Equal(t, true, log.IsLevelEnabled(ErrorLevel))
- assert.Equal(t, true, log.IsLevelEnabled(WarnLevel))
- assert.Equal(t, true, log.IsLevelEnabled(InfoLevel))
- assert.Equal(t, false, log.IsLevelEnabled(DebugLevel))
- assert.Equal(t, false, log.IsLevelEnabled(TraceLevel))
- log.SetLevel(DebugLevel)
- assert.Equal(t, true, log.IsLevelEnabled(PanicLevel))
- assert.Equal(t, true, log.IsLevelEnabled(FatalLevel))
- assert.Equal(t, true, log.IsLevelEnabled(ErrorLevel))
- assert.Equal(t, true, log.IsLevelEnabled(WarnLevel))
- assert.Equal(t, true, log.IsLevelEnabled(InfoLevel))
- assert.Equal(t, true, log.IsLevelEnabled(DebugLevel))
- assert.Equal(t, false, log.IsLevelEnabled(TraceLevel))
- log.SetLevel(TraceLevel)
- assert.Equal(t, true, log.IsLevelEnabled(PanicLevel))
- assert.Equal(t, true, log.IsLevelEnabled(FatalLevel))
- assert.Equal(t, true, log.IsLevelEnabled(ErrorLevel))
- assert.Equal(t, true, log.IsLevelEnabled(WarnLevel))
- assert.Equal(t, true, log.IsLevelEnabled(InfoLevel))
- assert.Equal(t, true, log.IsLevelEnabled(DebugLevel))
- assert.Equal(t, true, log.IsLevelEnabled(TraceLevel))
- }
- func TestReportCallerOnTextFormatter(t *testing.T) {
- l := New()
- l.Formatter.(*TextFormatter).ForceColors = true
- l.Formatter.(*TextFormatter).DisableColors = false
- l.WithFields(Fields{"func": "func", "file": "file"}).Info("test")
- l.Formatter.(*TextFormatter).ForceColors = false
- l.Formatter.(*TextFormatter).DisableColors = true
- l.WithFields(Fields{"func": "func", "file": "file"}).Info("test")
- }
|