github.com/adnan-c/fabric_e2e_couchdb@v0.6.1-preview.0.20170228180935-21ce6b23cf91/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  	"io/ioutil"
    21  	"log"
    22  	"sync"
    23  
    24  	"github.com/hyperledger/fabric/common/flogging"
    25  	"github.com/op/go-logging"
    26  	"google.golang.org/grpc/grpclog"
    27  )
    28  
    29  // Module names for logger initialization.
    30  const (
    31  	LoggingChannelModule   = "gossip/channel"
    32  	LoggingCommModule      = "gossip/comm"
    33  	LoggingDiscoveryModule = "gossip/discovery"
    34  	LoggingElectionModule  = "gossip/election"
    35  	LoggingGossipModule    = "gossip/gossip"
    36  	LoggingMockModule      = "gossip/comm/mock"
    37  	LoggingPullModule      = "gossip/pull"
    38  	LoggingServiceModule   = "gossip/service"
    39  	LoggingStateModule     = "gossip/state"
    40  )
    41  
    42  var loggersByModules = make(map[string]*logging.Logger)
    43  var lock = sync.Mutex{}
    44  var logger = logging.MustGetLogger("gossip/util")
    45  
    46  // defaultSpec is used to set the default logging level for all the
    47  // gossip modules.
    48  var defaultSpec = "WARNING"
    49  
    50  func init() {
    51  	// This make sure we get a "leveled" logging using the default
    52  	// format and output location defined in the flogging package,
    53  	// when the gossip module is not called from a peer process.
    54  	flogging.InitFromSpec(defaultSpec)
    55  	logger.Debugf("Setting default logging level to %s.", defaultSpec)
    56  
    57  	grpclog.SetLogger(log.New(ioutil.Discard, "", 0))
    58  }
    59  
    60  // GetLogger returns a logger for given gossip module and peerID
    61  func GetLogger(module string, peerID string) *logging.Logger {
    62  	if peerID != "" {
    63  		module = module + "#" + peerID
    64  	}
    65  
    66  	lock.Lock()
    67  	defer lock.Unlock()
    68  
    69  	if lgr, ok := loggersByModules[module]; ok {
    70  		return lgr
    71  	}
    72  
    73  	// Logger doesn't exist, create a new one
    74  	lgr := logging.MustGetLogger(module)
    75  	loggersByModules[module] = lgr
    76  	return lgr
    77  }