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 tlib.base import TestHelper
     5
     6
     7 -def get_tlib_logger(request):
     8      """
     9      Returns logger named tlib.\n
    10      This logger should only be used by TLib modules. To log from a test or test module use get_test_logger\n
    11      Logger will get flushed at the end to ensure data is not lost if tests exits abnormally
    12          """
    13      return get_logger(request, "tlib")
    14
    15
    16 -def get_adb_logger(request):
    17      """
    18      Returns logger named adb.\n
    19      This logger should only be used by adb modules. To log from a test or test module use get_test_logger\n
    20      Logger will get flushed at the end to ensure data is not lost if tests exits abnormally
    21          """
    22      return get_logger(request, "adb")
    23
    24 -def get_test_logger(request):
    25      """
    26      Returns logger named test.\n
    27      This logger should only be used by test modules. To log from a test or test module use get_test_logger\n
    28      Logger will get flushed at the end to ensure data is not lost if tests exits abnormally
    29      """
    30      return get_logger(request, "test")
    31
    32 -def get_logger(request, logger):
    33      """
    34      Returns a logger that has a console and a file handler.\n
    35
    36      @param logger: Logger name to return
    37      @type logger: str
    38      @return:
    39      """
    40      #Get current folder
    41      config_folder = TestHelper.tlib_config_folder()
    42      try:
    43          logging.config.fileConfig(os.path.join(config_folder, "logging.conf"))
    44      except IOError:
    45          raise IOError("Couldn't create logger object. Make sure log folder exists and you have write permissions")
    46
    47      logger = logging.getLogger(logger)
    48
    49      # Create a file handler
    50      curr_folder = os.path.dirname(request.fspath.strpath)
    51
    52      #Go up to three levels to find log folder
    53      log_folder = None
    54      for i in range(1, 4):
    55          curr_folder = os.path.abspath(os.path.join(curr_folder, os.pardir))
    56
    57          #Check if there is a folder 'screenshots'
    58          folder = os.path.join(curr_folder, '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 logs folder. Create it under your project and try again")
    65
    66      formatter = logger.handlers[0].formatter
    67
    68      fh = logging.FileHandler(os.path.join(log_folder, 'test.log'))
    69      fh.setLevel(logging.DEBUG)
    70      fh.setFormatter(formatter)
    71
    72      logger.addHandler(fh)
    73
    74      return logger
    75