github.com/yacovm/fabric@v2.0.0-alpha.0.20191128145320-c5d4087dc723+incompatible/common/flogging/logging_test.go (about) 1 /* 2 Copyright IBM Corp. All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package flogging_test 8 9 import ( 10 "bytes" 11 "errors" 12 "fmt" 13 "os" 14 "testing" 15 16 "github.com/hyperledger/fabric/common/flogging" 17 "github.com/hyperledger/fabric/common/flogging/mock" 18 "github.com/stretchr/testify/assert" 19 "go.uber.org/zap/zapcore" 20 ) 21 22 func TestNew(t *testing.T) { 23 logging, err := flogging.New(flogging.Config{}) 24 assert.NoError(t, err) 25 assert.Equal(t, zapcore.InfoLevel, logging.DefaultLevel()) 26 27 _, err = flogging.New(flogging.Config{ 28 LogSpec: "::=borken=::", 29 }) 30 assert.EqualError(t, err, "invalid logging specification '::=borken=::': bad segment '=borken='") 31 } 32 33 func TestNewWithEnvironment(t *testing.T) { 34 oldSpec, set := os.LookupEnv("FABRIC_LOGGING_SPEC") 35 if set { 36 defer os.Setenv("FABRIC_LOGGING_SPEC", oldSpec) 37 } 38 39 os.Setenv("FABRIC_LOGGING_SPEC", "fatal") 40 logging, err := flogging.New(flogging.Config{}) 41 assert.NoError(t, err) 42 assert.Equal(t, zapcore.FatalLevel, logging.DefaultLevel()) 43 44 os.Unsetenv("FABRIC_LOGGING_SPEC") 45 logging, err = flogging.New(flogging.Config{}) 46 assert.NoError(t, err) 47 assert.Equal(t, zapcore.InfoLevel, logging.DefaultLevel()) 48 } 49 50 //go:generate counterfeiter -o mock/write_syncer.go -fake-name WriteSyncer . writeSyncer 51 type writeSyncer interface { 52 zapcore.WriteSyncer 53 } 54 55 func TestLoggingSetWriter(t *testing.T) { 56 ws := &mock.WriteSyncer{} 57 58 w := &bytes.Buffer{} 59 logging, err := flogging.New(flogging.Config{ 60 Writer: w, 61 }) 62 assert.NoError(t, err) 63 64 old := logging.SetWriter(ws) 65 logging.SetWriter(w) 66 original := logging.SetWriter(ws) 67 68 assert.Exactly(t, old, original) 69 logging.Write([]byte("hello")) 70 assert.Equal(t, 1, ws.WriteCallCount()) 71 assert.Equal(t, []byte("hello"), ws.WriteArgsForCall(0)) 72 73 err = logging.Sync() 74 assert.NoError(t, err) 75 76 ws.SyncReturns(errors.New("welp")) 77 err = logging.Sync() 78 assert.EqualError(t, err, "welp") 79 } 80 81 func TestNamedLogger(t *testing.T) { 82 defer flogging.Reset() 83 buf := &bytes.Buffer{} 84 flogging.Global.SetWriter(buf) 85 86 t.Run("logger and named (child) logger with different levels", func(t *testing.T) { 87 defer buf.Reset() 88 logger := flogging.MustGetLogger("eugene") 89 logger2 := logger.Named("george") 90 flogging.ActivateSpec("eugene=info:eugene.george=error") 91 92 logger.Info("from eugene") 93 logger2.Info("from george") 94 assert.Contains(t, buf.String(), "from eugene") 95 assert.NotContains(t, buf.String(), "from george") 96 }) 97 98 t.Run("named logger where parent logger isn't enabled", func(t *testing.T) { 99 logger := flogging.MustGetLogger("foo") 100 logger2 := logger.Named("bar") 101 flogging.ActivateSpec("foo=fatal:foo.bar=error") 102 logger.Error("from foo") 103 logger2.Error("from bar") 104 assert.NotContains(t, buf.String(), "from foo") 105 assert.Contains(t, buf.String(), "from bar") 106 }) 107 } 108 109 func TestInvalidLoggerName(t *testing.T) { 110 names := []string{"test*", ".test", "test.", ".", ""} 111 for _, name := range names { 112 t.Run(name, func(t *testing.T) { 113 msg := fmt.Sprintf("invalid logger name: %s", name) 114 assert.PanicsWithValue(t, msg, func() { flogging.MustGetLogger(name) }) 115 }) 116 } 117 } 118 119 func TestCheck(t *testing.T) { 120 l := &flogging.Logging{} 121 observer := &mock.Observer{} 122 e := zapcore.Entry{} 123 124 // set observer 125 l.SetObserver(observer) 126 l.Check(e, nil) 127 assert.Equal(t, 1, observer.CheckCallCount()) 128 e, ce := observer.CheckArgsForCall(0) 129 assert.Equal(t, e, zapcore.Entry{}) 130 assert.Nil(t, ce) 131 132 l.WriteEntry(e, nil) 133 assert.Equal(t, 1, observer.WriteEntryCallCount()) 134 e, f := observer.WriteEntryArgsForCall(0) 135 assert.Equal(t, e, zapcore.Entry{}) 136 assert.Nil(t, f) 137 138 // remove observer 139 l.SetObserver(nil) 140 l.Check(zapcore.Entry{}, nil) 141 assert.Equal(t, 1, observer.CheckCallCount()) 142 } 143 144 func TestLoggerCoreCheck(t *testing.T) { 145 logging, err := flogging.New(flogging.Config{}) 146 assert.NoError(t, err) 147 148 logger := logging.ZapLogger("foo") 149 150 err = logging.ActivateSpec("info") 151 assert.NoError(t, err) 152 assert.False(t, logger.Core().Enabled(zapcore.DebugLevel), "debug should not be enabled at info level") 153 154 err = logging.ActivateSpec("debug") 155 assert.NoError(t, err) 156 assert.True(t, logger.Core().Enabled(zapcore.DebugLevel), "debug should now be enabled at debug level") 157 }