Package tlib :: Package base :: Module LogHelper |
[hide private]
[frames] | no frames]

# Source Code for Module tlib.base.LogHelper

     1  import os
     2  import logging
     3  import logging.config
     4  from logging import Formatter
     5  from tlib.base import TestHelper
     6
     7
     8 -def get_tlib_logger(request=None):
     9      """
    10      Returns logger named tlib.\n
    11      This logger should only be used by TLib modules. To log from a test or test module use get_test_logger\n
    12      Logger will get flushed at the end to ensure data is not lost if tests exits abnormally
    13      :param request
    14      """
    15      if request is None:
    16          return get_logger(os.path.abspath(os.curdir), "tlib")
    17      else:
    18          return get_logger(request.fspath.strpath, "tlib")
    19
    20
    21 -def get_adb_logger(request=None):
    22      """
    23      Returns logger named adb.\n
    24      This logger should only be used by adb modules. To log from a test or test module use get_test_logger\n
    25      Logger will get flushed at the end to ensure data is not lost if tests exits abnormally
    26      """
    27      if request is None:
    28          return get_logger(os.path.abspath(os.curdir), "adb")
    29      else:
    30          return get_logger(request.fspath.strpath, "adb")
    31
    32 -def get_test_logger(request=None):
    33      """
    34      Returns logger named test.\n
    35      This logger should only be used by test modules. To log from a test or test module use get_test_logger\n
    36      Logger will get flushed at the end to ensure data is not lost if tests exits abnormally
    37      """
    38      if request is None:
    39          return get_logger(os.path.abspath(os.curdir), "test")
    40      else:
    41          return get_logger(request.fspath.strpath, "test")
    42
    43 -def get_logger(path, name):
    44      """
    45      Returns a logger that has a console and a file handler.\n
    46
    47      @param name: Logger to return
    48      @type name: str
    49      @return:
    50      """
    51      # find location of .log file
    52      #Go up to three levels to find log folder. Search starts in current directory
    53      log_folder = None
    54      for i in range(1, 5):
    55          path = os.path.abspath(os.path.join(path, os.pardir))
    56
    57          #Check if there is a folder 'logs'
    58          folder = os.path.join(path, 'logs')
    59          if os.path.exists(folder):
    60              log_folder = folder
    61              break
    62
    63      if log_folder is None:
    64          raise IOError("Couldn't find folder 'logs'. Create it under your project folder and try again")
    65
    66      #Create logger
    67      logger = logging.getLogger(name)
    68      logger.setLevel(logging.DEBUG)
    69
    70      #Add a file handler
    71      fh = logging.FileHandler(os.path.join(log_folder, 'test.log'))
    72      logger.addHandler(fh)
    73      logger.propagate = 0
    74
    75      fh.setLevel(logging.DEBUG)
    76
    77      #Set log format
    78      formatter = Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
    79      fh.setFormatter(formatter)
    80
    81      return logger
    82