github.com/sykesm/fabric@v1.1.0-preview.0.20200129034918-2aa12b1a0181/common/flogging/global_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 "testing" 12 13 "github.com/hyperledger/fabric/common/flogging" 14 "github.com/hyperledger/fabric/common/flogging/mock" 15 "github.com/stretchr/testify/assert" 16 ) 17 18 func TestGlobalReset(t *testing.T) { 19 flogging.Reset() 20 flogging.Global.SetFormat("json") 21 err := flogging.Global.ActivateSpec("logger=debug") 22 assert.NoError(t, err) 23 24 system, err := flogging.New(flogging.Config{}) 25 assert.NoError(t, err) 26 assert.NotEqual(t, flogging.Global.LoggerLevels, system.LoggerLevels) 27 assert.NotEqual(t, flogging.Global.Encoding(), system.Encoding()) 28 29 flogging.Reset() 30 assert.Equal(t, flogging.Global.LoggerLevels, system.LoggerLevels) 31 assert.Equal(t, flogging.Global.Encoding(), system.Encoding()) 32 } 33 34 func TestGlobalInitConsole(t *testing.T) { 35 flogging.Reset() 36 defer flogging.Reset() 37 38 buf := &bytes.Buffer{} 39 flogging.Init(flogging.Config{ 40 Format: "%{message}", 41 LogSpec: "DEBUG", 42 Writer: buf, 43 }) 44 45 logger := flogging.MustGetLogger("testlogger") 46 logger.Debug("this is a message") 47 48 assert.Equal(t, "this is a message\n", buf.String()) 49 } 50 51 func TestGlobalInitJSON(t *testing.T) { 52 flogging.Reset() 53 defer flogging.Reset() 54 55 buf := &bytes.Buffer{} 56 flogging.Init(flogging.Config{ 57 Format: "json", 58 LogSpec: "DEBUG", 59 Writer: buf, 60 }) 61 62 logger := flogging.MustGetLogger("testlogger") 63 logger.Debug("this is a message") 64 65 assert.Regexp(t, `{"level":"debug","ts":\d+.\d+,"name":"testlogger","caller":"flogging/global_test.go:\d+","msg":"this is a message"}\s+`, buf.String()) 66 } 67 68 func TestGlobalInitLogfmt(t *testing.T) { 69 flogging.Reset() 70 defer flogging.Reset() 71 72 buf := &bytes.Buffer{} 73 flogging.Init(flogging.Config{ 74 Format: "logfmt", 75 LogSpec: "DEBUG", 76 Writer: buf, 77 }) 78 79 logger := flogging.MustGetLogger("testlogger") 80 logger.Debug("this is a message") 81 82 assert.Regexp(t, `^ts=\d+.\d+ level=debug name=testlogger caller=flogging/global_test.go:\d+ msg="this is a message"`, buf.String()) 83 } 84 85 func TestGlobalInitPanic(t *testing.T) { 86 flogging.Reset() 87 defer flogging.Reset() 88 89 assert.Panics(t, func() { 90 flogging.Init(flogging.Config{ 91 Format: "%{color:evil}", 92 }) 93 }) 94 } 95 96 func TestGlobalDefaultLevel(t *testing.T) { 97 flogging.Reset() 98 99 assert.Equal(t, "info", flogging.DefaultLevel()) 100 } 101 102 func TestGlobalLoggerLevel(t *testing.T) { 103 flogging.Reset() 104 assert.Equal(t, "info", flogging.LoggerLevel("some.logger")) 105 } 106 107 func TestGlobalMustGetLogger(t *testing.T) { 108 flogging.Reset() 109 110 l := flogging.MustGetLogger("logger-name") 111 assert.NotNil(t, l) 112 } 113 114 func TestFlogginInitPanic(t *testing.T) { 115 defer flogging.Reset() 116 117 assert.Panics(t, func() { 118 flogging.Init(flogging.Config{ 119 Format: "%{color:broken}", 120 }) 121 }) 122 } 123 124 func TestActivateSpec(t *testing.T) { 125 defer flogging.Reset() 126 127 flogging.ActivateSpec("fatal") 128 assert.Equal(t, "fatal", flogging.Global.Spec()) 129 } 130 131 func TestActivateSpecPanic(t *testing.T) { 132 defer flogging.Reset() 133 134 assert.Panics(t, func() { 135 flogging.ActivateSpec("busted") 136 }) 137 } 138 139 func TestGlobalSetObserver(t *testing.T) { 140 flogging.Reset() 141 defer flogging.Reset() 142 143 observer := &mock.Observer{} 144 145 flogging.Global.SetObserver(observer) 146 o := flogging.Global.SetObserver(nil) 147 assert.Exactly(t, observer, o) 148 } 149 150 func TestGlobalSetWriter(t *testing.T) { 151 flogging.Reset() 152 defer flogging.Reset() 153 154 w := &bytes.Buffer{} 155 156 old := flogging.Global.SetWriter(w) 157 flogging.Global.SetWriter(old) 158 original := flogging.Global.SetWriter(nil) 159 160 assert.Exactly(t, old, original) 161 }