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