github.com/adnan-c/fabric_e2e_couchdb@v0.6.1-preview.0.20170228180935-21ce6b23cf91/common/flogging/logging_test.go (about) 1 /* 2 Copyright IBM Corp. 2016 All Rights Reserved. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package flogging_test 18 19 import ( 20 "os" 21 "testing" 22 23 "github.com/hyperledger/fabric/common/flogging" 24 "github.com/op/go-logging" 25 "github.com/spf13/viper" 26 ) 27 28 func TestLevelDefault(t *testing.T) { 29 viper.Reset() 30 31 flogging.InitFromViper("") 32 33 assertDefaultLevel(t, flogging.DefaultLevel()) 34 } 35 36 func TestLevelOtherThanDefault(t *testing.T) { 37 viper.Reset() 38 viper.Set("logging_level", "warning") 39 40 flogging.InitFromViper("") 41 42 assertDefaultLevel(t, logging.WARNING) 43 } 44 45 func TestLevelForSpecificModule(t *testing.T) { 46 viper.Reset() 47 viper.Set("logging_level", "core=info") 48 49 flogging.InitFromViper("") 50 51 assertModuleLevel(t, "core", logging.INFO) 52 } 53 54 func TestLeveltForMultipleModules(t *testing.T) { 55 viper.Reset() 56 viper.Set("logging_level", "core=warning:test=debug") 57 58 flogging.InitFromViper("") 59 60 assertModuleLevel(t, "core", logging.WARNING) 61 assertModuleLevel(t, "test", logging.DEBUG) 62 } 63 64 func TestLevelForMultipleModulesAtSameLevel(t *testing.T) { 65 viper.Reset() 66 viper.Set("logging_level", "core,test=warning") 67 68 flogging.InitFromViper("") 69 70 assertModuleLevel(t, "core", logging.WARNING) 71 assertModuleLevel(t, "test", logging.WARNING) 72 } 73 74 func TestLevelForModuleWithDefault(t *testing.T) { 75 viper.Reset() 76 viper.Set("logging_level", "info:test=warning") 77 78 flogging.InitFromViper("") 79 80 assertDefaultLevel(t, logging.INFO) 81 assertModuleLevel(t, "test", logging.WARNING) 82 } 83 84 func TestLevelForModuleWithDefaultAtEnd(t *testing.T) { 85 viper.Reset() 86 viper.Set("logging_level", "test=warning:info") 87 88 flogging.InitFromViper("") 89 90 assertDefaultLevel(t, logging.INFO) 91 assertModuleLevel(t, "test", logging.WARNING) 92 } 93 94 func TestLevelForSpecificCommand(t *testing.T) { 95 viper.Reset() 96 viper.Set("logging.node", "error") 97 98 flogging.InitFromViper("node") 99 100 assertDefaultLevel(t, logging.ERROR) 101 } 102 103 func TestLevelForUnknownCommandGoesToDefault(t *testing.T) { 104 viper.Reset() 105 106 flogging.InitFromViper("unknown command") 107 108 assertDefaultLevel(t, flogging.DefaultLevel()) 109 } 110 111 func TestLevelInvalid(t *testing.T) { 112 viper.Reset() 113 viper.Set("logging_level", "invalidlevel") 114 115 flogging.InitFromViper("") 116 117 assertDefaultLevel(t, flogging.DefaultLevel()) 118 } 119 120 func TestLevelInvalidModules(t *testing.T) { 121 viper.Reset() 122 viper.Set("logging_level", "core=invalid") 123 124 flogging.InitFromViper("") 125 126 assertDefaultLevel(t, flogging.DefaultLevel()) 127 } 128 129 func TestLevelInvalidEmptyModule(t *testing.T) { 130 viper.Reset() 131 viper.Set("logging_level", "=warning") 132 133 flogging.InitFromViper("") 134 135 assertDefaultLevel(t, flogging.DefaultLevel()) 136 } 137 138 func TestLevelInvalidModuleSyntax(t *testing.T) { 139 viper.Reset() 140 viper.Set("logging_level", "type=warn=again") 141 142 flogging.InitFromViper("") 143 144 assertDefaultLevel(t, flogging.DefaultLevel()) 145 } 146 147 func TestGetModuleLevelDefault(t *testing.T) { 148 level, _ := flogging.GetModuleLevel("peer") 149 150 // peer should be using the default log level at this point 151 if level != "INFO" { 152 t.FailNow() 153 } 154 } 155 156 func TestGetModuleLevelDebug(t *testing.T) { 157 flogging.SetModuleLevel("peer", "DEBUG") 158 level, _ := flogging.GetModuleLevel("peer") 159 160 // ensure that the log level has changed to debug 161 if level != "DEBUG" { 162 t.FailNow() 163 } 164 } 165 166 func TestGetModuleLevelInvalid(t *testing.T) { 167 flogging.SetModuleLevel("peer", "invalid") 168 level, _ := flogging.GetModuleLevel("peer") 169 170 // ensure that the log level didn't change after invalid log level specified 171 if level != "DEBUG" { 172 t.FailNow() 173 } 174 } 175 176 func TestSetModuleLevel(t *testing.T) { 177 flogging.SetModuleLevel("peer", "WARNING") 178 179 // ensure that the log level has changed to warning 180 assertModuleLevel(t, "peer", logging.WARNING) 181 } 182 183 func TestSetModuleLevelInvalid(t *testing.T) { 184 flogging.SetModuleLevel("peer", "invalid") 185 186 // ensure that the log level didn't change after invalid log level specified 187 assertModuleLevel(t, "peer", logging.WARNING) 188 } 189 190 func ExampleSetLoggingFormat() { 191 // initializes logging backend for testing and sets 192 // time to 1970-01-01 00:00:00.000 UTC 193 logging.InitForTesting(flogging.DefaultLevel()) 194 195 logFormat := "%{time:2006-01-02 15:04:05.000 MST} [%{module}] %{shortfunc} -> %{level:.4s} %{id:03x} %{message}" 196 flogging.SetLoggingFormat(logFormat, os.Stdout) 197 198 logger := logging.MustGetLogger("floggingTest") 199 logger.Infof("test") 200 201 // Output: 202 // 1970-01-01 00:00:00.000 UTC [floggingTest] ExampleSetLoggingFormat -> INFO 001 test 203 } 204 205 func ExampleSetLoggingFormat_second() { 206 // initializes logging backend for testing and sets 207 // time to 1970-01-01 00:00:00.000 UTC 208 logging.InitForTesting(flogging.DefaultLevel()) 209 210 logFormat := "%{time:15:04:05.000} [%{module}] %{shortfunc} -> %{level:.4s} %{id:03x} %{message}" 211 flogging.SetLoggingFormat(logFormat, os.Stdout) 212 213 logger := logging.MustGetLogger("floggingTest") 214 logger.Infof("test") 215 216 // Output: 217 // 00:00:00.000 [floggingTest] ExampleSetLoggingFormat_second -> INFO 001 test 218 } 219 220 func assertDefaultLevel(t *testing.T, expectedLevel logging.Level) { 221 assertModuleLevel(t, "", expectedLevel) 222 } 223 224 func assertModuleLevel(t *testing.T, module string, expectedLevel logging.Level) { 225 assertEquals(t, expectedLevel, logging.GetLevel(module)) 226 } 227 228 func assertEquals(t *testing.T, expected interface{}, actual interface{}) { 229 if expected != actual { 230 t.Errorf("Expected: %v, Got: %v", expected, actual) 231 } 232 }