log - Customized logging system

CSTBox logging helpers.

Built on top of Python default logging system, to provide a simplified use of logs and a uniform formatting. Can be used as a drop-in replacement of logging module, since all toplevel functions and constants are surfaced here.

class pycstbox.log.Loggable(logger=None, logname=None)

Mixin adding logging methods in a convenient way.

This class takes care of creating a log instance, based on what is provided at instantiation time. See constructor documentation for details.

It exposes common logging.Logger output methods, prefixing them by ‘log_’ to avoid any name clash.

Creates a log instance based on the parameters provided. If logger is defined, it is used as the logger. Otherwise a logger is created locally. If logname is provided, this logger will be named accordingly. Otherwise the class name is used.

Parameters:
  • logger (Logger) – (optional) the logger to use
  • logname (str) – (optional) the name of the log created locally, if logger is not provided. Default: the class name
logger

The embedded logger instance.

log_setLevel_from_args(args)

Sets the logger level based on the attribute “loglevel” of the args parameter, if it exists. If not the case, this method has no effect.

The typical use case of this method is to pass the result of ArgumentParser.parse_args for args.

Parameters:args – an object supposed to have an attribute named ‘loglevel’, which value is the (case insensitive) name of one of the log levels defined in logging
pycstbox.log.setup_logging(name=None, level=20)

Setup the root log format and level, and optionally change its default name (“root”) to the provided one.

pycstbox.log.set_loglevel_from_args(logger, args)

Sets the level of a logger according to the ‘loglevel’ attribute of args got from an ArgumentParser.

Parameters:
  • logger (Logger) – the logger which log level must be modified
  • args – the CLI arguments, as parser by an argument parser
pycstbox.log.loglevel_from_args(args)

Returns the log level (as defined in logging) corresponding to the -L/–loglevel command line option value.

Parameters:args – the CLI arguments, as parser by an argument parser
class pycstbox.log.DummyLogger

A do-nothing logger which can be used as a fallback.