logquacious package

Submodules

logquacious.backport_configurable_stacklevel module

Backport of configurable stacklevel for logging added in Python 3.8.

See https://github.com/python/cpython/pull/7424

class logquacious.backport_configurable_stacklevel.PatchedLoggerMixin(*args, **kwargs)[source]

Bases: object

Mixin adding temp_monkey_patched_logger that allows stacklevel kwarg.

Classes that include this mixin have a temp_monkey_patched_logger context manager that allows the use of the stacklevel keyword argument from Python 3.8.

Classes using this mixin must have a logging.Logger instance as an attribute of the class. By default, this is assumed to be named logger, but you can override the logger_attribute class attribute with the name of a different attribute.

logger_attribute = 'logger'

Name of logger instance on the class inheriting this mixin.

temp_monkey_patched_logger()[source]

Temporarily monkey patch logger to allow overriding log records.

The monkey patching is reset so that the behavior change is limited to the scope of this logger.

logquacious.backport_configurable_stacklevel.patch_logger(logger_class)[source]

Return logger class patched with stacklevel keyword argument.

logquacious.cascading_config module

class logquacious.cascading_config.CascadingConfig(config_values=None, cascade_map=None)[source]

Bases: collections.abc.Mapping

Cascading configuration values.

This class allows you to define parameter names that can match exactly, but if it doesn’t, parameter names will be searched as defined by cascade_map. cascade_map basically defines edges of a dependency graph, which is then used for a breadth-first search of parameter values.

Parameters
  • config_values (dict or list of (key, value) pairs) – Default values for a configuration, where keys are the parameter names and values are the associated value.

  • cascade_map (dict) – Dictionary defining cascading defaults. If a parameter name is not found, indexing cascade_map with the parameter name will return the parameter to look for.

  • kwargs (dict) – Keyword arguments for initializing dict.

cascade_list(name)[source]

Return list of cascade hierarchy for a given configuration name.

cascade_path(name)[source]

Return string of describing cascade.

get(name, default=None, _prev=None)[source]

Return best matching config value for name.

Get value from configuration. The search for name is in the following order:

  • self (Value in global configuration)

  • default

  • Alternate name specified by self.cascade_map

This method supports the pattern commonly used for optional keyword arguments to a function. For example:

>>> def print_value(key, **kwargs):
...     print(kwargs.get(key, 0))
>>> print_value('a')
0
>>> print_value('a', a=1)
1

Instead, you would create a config class and write:

>>> config = CascadingConfig({'a': 0})
>>> def print_value(key, **kwargs):
...     print(kwargs.get(key, config.get(key)))
>>> print_value('a')
0
>>> print_value('a', a=1)
1
>>> print_value('b')
None
>>> config.cascade_map['b'] = 'a'
>>> print_value('b')
0

See examples below for a demonstration of the cascading of configuration names.

Parameters
  • name (str) – Name of config value you want.

  • default (object) – Default value if name doesn’t exist in instance.

Examples

>>> config = CascadingConfig({'size': 0},
...                          cascade_map={'arrow.size': 'size'})
>>> config.get('size')
0
>>> top_choice = {'size': 1}
>>> top_choice.get('size', config.get('size'))
1
>>> config.get('non-existent', 'unknown')
'unknown'
>>> config.get('arrow.size')
0
>>> config.get('arrow.size', 2)
2
>>> top_choice.get('size', config.get('arrow.size'))
1

logquacious.constants module

logquacious.context_templates module

class logquacious.context_templates.ContextTemplates(config_dict=None)[source]

Bases: logquacious.cascading_config.CascadingConfig

classmethod resolve(templates)[source]

logquacious.log_context module

class logquacious.log_context.LogContext(logger, templates=None)[source]

Bases: object

Manager for context managers/decorators used for logging.

debug

Decorator/context-manager with level logging.DEBUG.

info

Decorator/context-manager with level logging.INFO.

warning

Decorator/context-manager with level logging.WARNING.

error

Decorator/context-manager with level logging.ERROR.

fatal

Decorator/context-manager with level logging.CRITICAL.

logquacious.log_manager module

class logquacious.log_manager.LogManager(name=None, context_templates=None)[source]

Bases: logquacious.backport_configurable_stacklevel.PatchedLoggerMixin

Logging manager for use as a logger, decorator, or contextmanager.

>>> log = LogManager(__name__)
>>> log.info('Normal logging statement')

[INFO] Normal logging statement

>>> @log.context.info
... def logged_function():
...     log.info('Inside logged_function')

[DEBUG] Start logged_function [INFO] Inside logged_function [DEBUG] Finish logged_function

>>> with log.context.info("context manager"):
...     log.info("Inside context manager")

[DEBUG] Start context manager [INFO] Inside context manger [DEBUG] Finish context manager

and_reraise(allowed_exceptions, msg='Logging error and reraising', level=40, exc_info=True, stacklevel=3)[source]

Context manager that logs and reraises given error.

Parameters
  • allowed_exceptions – Exception(s) to log before reraising.

  • msg – Message logged for exceptions.

  • level – Logging level for logging exceptions.

  • exc_info – If True, include exception info.

  • stacklevel – Stacklevel of logging statement. Defaults to level 3 since this method (level=2) defers functionality to a helper utility (level=1), but logging should use the context where this is called (level=3).

and_suppress(allowed_exceptions, msg='Suppressed error and logging', level=40, exc_info=True, stacklevel=3)[source]

Context manager that logs and suppresses given error.

Parameters
  • allowed_exceptions – Exception(s) to log and suppress.

  • msg – Message logged for exceptions.

  • level – Logging level for logging exceptions.

  • exc_info – If True, include exception info.

  • stacklevel – Stacklevel of logging statement. Defaults to level 3 since this method (level=2) defers functionality to a helper utility (level=1), but logging should use the context where this is called (level=3).

logquacious.utils module

class logquacious.utils.HandleException(handled_exceptions, on_exception)[source]

Bases: contextlib.ContextDecorator

handled_exceptions = ()
on_exception()[source]
logquacious.utils.format_function_args(args, kwargs, show_args=False, show_kwargs=False)[source]
logquacious.utils.get_logger(name_or_logger)[source]
logquacious.utils.is_sequence(value)[source]

Return True if value is a non-string sequence.

See https://stackoverflow.com/a/1835259/260303

logquacious.utils.is_string(value)[source]

Module contents

Top-level package for logquacious.

class logquacious.LogManager(name=None, context_templates=None)[source]

Bases: logquacious.backport_configurable_stacklevel.PatchedLoggerMixin

Logging manager for use as a logger, decorator, or contextmanager.

>>> log = LogManager(__name__)
>>> log.info('Normal logging statement')

[INFO] Normal logging statement

>>> @log.context.info
... def logged_function():
...     log.info('Inside logged_function')

[DEBUG] Start logged_function [INFO] Inside logged_function [DEBUG] Finish logged_function

>>> with log.context.info("context manager"):
...     log.info("Inside context manager")

[DEBUG] Start context manager [INFO] Inside context manger [DEBUG] Finish context manager

and_reraise(allowed_exceptions, msg='Logging error and reraising', level=40, exc_info=True, stacklevel=3)[source]

Context manager that logs and reraises given error.

Parameters
  • allowed_exceptions – Exception(s) to log before reraising.

  • msg – Message logged for exceptions.

  • level – Logging level for logging exceptions.

  • exc_info – If True, include exception info.

  • stacklevel – Stacklevel of logging statement. Defaults to level 3 since this method (level=2) defers functionality to a helper utility (level=1), but logging should use the context where this is called (level=3).

and_suppress(allowed_exceptions, msg='Suppressed error and logging', level=40, exc_info=True, stacklevel=3)[source]

Context manager that logs and suppresses given error.

Parameters
  • allowed_exceptions – Exception(s) to log and suppress.

  • msg – Message logged for exceptions.

  • level – Logging level for logging exceptions.

  • exc_info – If True, include exception info.

  • stacklevel – Stacklevel of logging statement. Defaults to level 3 since this method (level=2) defers functionality to a helper utility (level=1), but logging should use the context where this is called (level=3).