lmi_logging

Utilities for logging framework.

lmi.scripts.common.lmi_logging.LOG_LEVEL_2_COLOR = {40: 9, 50: 13, 30: 11}

Dictionary assigning color code to logging level.

class lmi.scripts.common.lmi_logging.LevelDispatchingFormatter(formatters, default='%(cseq)s%(levelname_)-8s:%(creset)s %(message)s', datefmt=None)

Formatter class for logging module. It allows to predefine different format string used for some level ranges.

Parameters:
  • formatters (dict) –

    Mapping of module names to format. It is a dictionary of following format:

    { max_level1 : format1
    , max_level2 : format2
    , ... }
    

    format in parameters description can be either string or another formatter object.

    For example if you want to have format3 used for ERROR and CRITICAL levels, format2 for INFO and format1 for anything else, your dictionary will look like this:

    { logging.INFO - 1 : format1
    , logging.INFO     : format2 }
    

    And the default value should have format3 assigned.

  • default – Default format to use. This format is used for all levels higher than the maximum of formatters‘ keys.
format(record)

Interface for logging module.

class lmi.scripts.common.lmi_logging.LogRecord(name, level, *args, **kwargs)

Overrides logging.LogRecord. It adds new attributes:

  • levelname_ - Name of level in lowercase.

  • cseq - Escape sequence for terminal used to set color

    assigned to particular log level.

  • creset - Escape sequence for terminal used to reset foreground

    color.

These can be used in format strings initializing logging formatters.

Accepts the same arguments as base class.

lmi.scripts.common.lmi_logging.get_color_sequence(color_code)

Computer color sequence for particular color code.

Returns:Escape sequence for terminal used to set foreground color.
Return type:str
lmi.scripts.common.lmi_logging.get_logger(module_name)

Convenience function for getting callable returning logger for particular module name. It’s supposed to be used at module’s level to assign its result to global variable like this:

from lmi.scripts import common

LOG = common.get_logger(__name__)

This can be used in module’s functions and classes like this:

def module_function(param):
    LOG().debug("This is debug statement logging param: %s", param)

Thanks to LOG being a callable, it always returns valid logger object with current configuration, which may change overtime.

Parameters:module_name (string) – Absolute dotted module path.
Return type:logging.Logger
lmi.scripts.common.lmi_logging.setup_logger(use_colors=True)

This needs to be called before any logging takes place.