API Reference

reconplogger module

Classes:

RLoggerProperty(*args, **kwargs)

Class designed to be inherited by other classes to add an rlogger property.

Functions:

configure_root_logger()

Installs a named handler on the root logger and removes stream handlers from named loggers.

logger_setup([logger_name, config, level])

Sets up logging configuration and returns the logger.

flask_app_logger_setup(flask_app[, ...])

Sets up logging configuration, configures flask to use it, and returns the logger.

get_correlation_id()

Returns the current correlation id.

set_correlation_id(correlation_id)

Sets the correlation id for the current application context.

correlation_id_context(correlation_id)

Context manager to set the correlation id for the current application context.

add_file_handler(logger, file_path[, ...])

Adds a file handler to a given logger.

class reconplogger.RLoggerProperty(*args, **kwargs)

Bases: object

Class designed to be inherited by other classes to add an rlogger property.

Methods:

__init__(*args, **kwargs)

Initializer for LoggerProperty class.

Attributes:

rlogger

The logger property for the class.

__init__(*args, **kwargs)

Initializer for LoggerProperty class.

property rlogger

The logger property for the class.

Getter:

Returns the current logger.

Setter:

Sets the reconplogger logger if True or sets null_logger if False or sets the given logger.

Raises:

ValueError – If an invalid logger value is given.

reconplogger.configure_root_logger()

Installs a named handler on the root logger and removes stream handlers from named loggers.

After this call every log record in the process flows through the single root handler, regardless of which named logger emitted it. All named loggers (except those with only NullHandler instances) have their StreamHandler instances removed and propagate set to True so records bubble up to the root while keeping non-stream handlers such as file handlers attached.

Return type:

None

reconplogger.logger_setup(logger_name='plain_logger', config=None, level=None)

Sets up logging configuration and returns the logger.

If the environment variable LOGGER_ROOT_HANDLER is set to the name of a handler defined in the logging config, that handler is installed on the root logger so that all third-party loggers (which propagate to the root by default) are also captured. The primary logger level remains controlled by level / LOGGER_LEVEL, while the root logger level can be controlled independently through LOGGER_ROOT_LEVEL. On subsequent calls the same primary logger is returned without reconfiguring the root. To force a fresh configuration pass, call reset_configs() first.

Parameters:
  • logger_name (str) – Name of the logger that needs to be used.

  • config (Optional[str]) – Configuration string or path to configuration file or configuration file via environment variable.

  • level (Optional[str]) – Optional logging level that overrides one in config.

Return type:

Logger

Returns:

The logger object.

reconplogger.flask_app_logger_setup(flask_app, logger_name='plain_logger', config=None, level=None)

Sets up logging configuration, configures flask to use it, and returns the logger.

Parameters:
  • flask_app (flask.app.Flask) – The flask app object.

  • logger_name (str) – Name of the logger that needs to be used.

  • config (Optional[str]) – Configuration string or path to configuration file or configuration file via environment variable.

  • level (Optional[str]) – Optional logging level that overrides one in config.

Return type:

Logger

Returns:

The logger object.

reconplogger.get_correlation_id()

Returns the current correlation id.

Raises:
  • ImportError – When flask package not available.

  • RuntimeError – When run outside an application context or if flask app has not been setup.

Return type:

str

reconplogger.set_correlation_id(correlation_id)

Sets the correlation id for the current application context.

Raises:
  • ImportError – When flask package not available.

  • RuntimeError – When run outside an application context or if flask app has not been setup.

reconplogger.correlation_id_context(correlation_id)

Context manager to set the correlation id for the current application context.

Use as with correlation_id_context(correlation_id): …. Calls to get_correlation_id() will return the correlation id set for the context.

Parameters:

correlation_id (Optional[str]) – The correlation id to set in the context.

reconplogger.add_file_handler(logger, file_path, format='%(asctime)s\\t%(levelname)s -- %(filename)s:%(lineno)s -- %(message)s', level='DEBUG')

Adds a file handler to a given logger.

Parameters:
  • logger (Logger) – Logger object where to add the file handler.

  • file_path (str) – Path to log file for handler.

  • format (str) – Format for logging.

  • level (Optional[str]) – Logging level for the handler.

Return type:

FileHandler

Returns:

The handler object which could be used for removeHandler.