github.com/muhammedhassanm/blockchain@v0.0.0-20200120143007-697261defd4d/sawtooth-core-master/sdk/python/sawtooth_sdk/processor/log.py (about)

     1  # Copyright 2017 Intel Corporation
     2  #
     3  # Licensed under the Apache License, Version 2.0 (the "License");
     4  # you may not use this file except in compliance with the License.
     5  # You may obtain a copy of the License at
     6  #
     7  #     http://www.apache.org/licenses/LICENSE-2.0
     8  #
     9  # Unless required by applicable law or agreed to in writing, software
    10  # distributed under the License is distributed on an "AS IS" BASIS,
    11  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  # See the License for the specific language governing permissions and
    13  # limitations under the License.
    14  # ------------------------------------------------------------------------------
    15  
    16  import logging
    17  import logging.config
    18  import os
    19  
    20  from colorlog import ColoredFormatter
    21  
    22  
    23  def create_console_handler(verbose_level):
    24      """
    25      Set up the console logging for a transaction processor.
    26      Args:
    27          verbose_level (int): The log level that the console should print out
    28      """
    29      clog = logging.StreamHandler()
    30      formatter = ColoredFormatter(
    31          "%(log_color)s[%(asctime)s.%(msecs)03d "
    32          "%(levelname)-8s %(module)s]%(reset)s "
    33          "%(white)s%(message)s",
    34          datefmt="%Y-%m-%d %H:%M:%S",
    35          reset=True,
    36          log_colors={
    37              'DEBUG': 'cyan',
    38              'INFO': 'green',
    39              'WARNING': 'yellow',
    40              'ERROR': 'red',
    41              'CRITICAL': 'red',
    42          })
    43  
    44      clog.setFormatter(formatter)
    45  
    46      if verbose_level == 0:
    47          clog.setLevel(logging.WARN)
    48      elif verbose_level == 1:
    49          clog.setLevel(logging.INFO)
    50      else:
    51          clog.setLevel(logging.DEBUG)
    52  
    53      return clog
    54  
    55  
    56  def init_console_logging(verbose_level=2):
    57      """
    58      Set up the console logging for a transaction processor.
    59      Args:
    60          verbose_level (int): The log level that the console should print out
    61      """
    62      logger = logging.getLogger()
    63      logger.setLevel(logging.DEBUG)
    64      logger.addHandler(create_console_handler(verbose_level))
    65  
    66  
    67  def log_configuration(log_config=None, log_dir=None, name=None):
    68      """
    69      Sets up the loggers for a transaction processor.
    70      Args:
    71          log_config (dict): A dictinary of log config options
    72          log_dir (string): The log directory's path
    73          name (string): The name of the expected logging file
    74      """
    75      if log_config is not None:
    76          logging.config.dictConfig(log_config)
    77      else:
    78          log_filename = os.path.join(log_dir, name)
    79          debug_handler = logging.FileHandler(log_filename + "-debug.log")
    80          debug_handler.setFormatter(logging.Formatter(
    81              '[%(asctime)s.%(msecs)03d [%(threadName)s] %(module)s'
    82              ' %(levelname)s] %(message)s', "%H:%M:%S"))
    83          debug_handler.setLevel(logging.DEBUG)
    84  
    85          error_handler = logging.FileHandler(log_filename + "-error.log")
    86          error_handler.setFormatter(logging.Formatter(
    87              '[%(asctime)s.%(msecs)03d [%(threadName)s] %(module)s'
    88              ' %(levelname)s] %(message)s', "%H:%M:%S"))
    89          error_handler.setLevel(logging.ERROR)
    90  
    91          logging.getLogger().addHandler(error_handler)
    92          logging.getLogger().addHandler(debug_handler)