github.com/tenywen/fabric@v1.0.0-beta.0.20170620030522-a5b1ed380643/gossip/util/logging.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 util 18 19 import ( 20 "sync" 21 22 "github.com/hyperledger/fabric/common/flogging" 23 "github.com/op/go-logging" 24 ) 25 26 // Module names for logger initialization. 27 const ( 28 LoggingChannelModule = "gossip/channel" 29 LoggingCommModule = "gossip/comm" 30 LoggingDiscoveryModule = "gossip/discovery" 31 LoggingElectionModule = "gossip/election" 32 LoggingGossipModule = "gossip/gossip" 33 LoggingMockModule = "gossip/comm/mock" 34 LoggingPullModule = "gossip/pull" 35 LoggingServiceModule = "gossip/service" 36 LoggingStateModule = "gossip/state" 37 ) 38 39 var loggersByModules = make(map[string]*logging.Logger) 40 var lock = sync.Mutex{} 41 var testMode bool 42 43 // defaultTestSpec is the default logging level for gossip tests 44 var defaultTestSpec = "WARNING" 45 46 // GetLogger returns a logger for given gossip module and peerID 47 func GetLogger(module string, peerID string) *logging.Logger { 48 if peerID != "" && testMode { 49 module = module + "#" + peerID 50 } 51 52 lock.Lock() 53 defer lock.Unlock() 54 55 if lgr, ok := loggersByModules[module]; ok { 56 return lgr 57 } 58 59 // Logger doesn't exist, create a new one 60 lgr := flogging.MustGetLogger(module) 61 loggersByModules[module] = lgr 62 return lgr 63 } 64 65 // SetupTestLogging sets the default log levels for gossip unit tests 66 func SetupTestLogging() { 67 testMode = true 68 flogging.InitFromSpec(defaultTestSpec) 69 }