Skip to main content

Self-logging exceptions: Attach log messages to exceptions and output them conditionally.

Project description

Installation

Use setup.py :

python setup.py install

Warning

This module modifies sys.excepthook when it is imported!

Usage

Self-logging exceptions

Attach a log message to an exception:

import logging
import logging_exceptions as exlog

e = ValueError("Wrong value")
logger = logging.getLogger(__name__)
with exlog.log_to_exception(logger, e):
    logger.critical("This is a %s log mressage", "long")
raise e # The exception can also be raised inside the with statement

If the error is not caught, the log message will be displayed upon program termination.

Catch the error and display the log message at a costum log-level:

import logging_exceptions as exlog
import logging
try:
    e = ValueError("Wrong value")
    logger = logging.getLogger(__name__)
    with exlog.log_to_exception(logger, e):
        logger.critical("This is a %s log mressage", "long")
        raise e
except ValueError as err:
    exlog.log(err, level=logging.DEBUG, with_stacktrace=False)

Here, exlog.log is a synonym for exlog.log_exception

Logging from a different function name and line number

The code in log_exception calls the _log function of a Logger object. To avoid recording the function name and line number of log_to_exception, logging_exception calls logging.setLoggerClass(ExlogLogger) to set the class for all loggers created after import of logging_exception to ExlogLogger.

ExlogLogger is a subclass of logging.Logger defined by this module. It overrides findCaller to have more control over the recorded caller. In addition to ignoring functions defined inside logging_exceptions, ExlogLogger classes have the public attribute ignored_functions.

By adding function names to ignored_functions, it is possible to record matching function’s parents instead of matching functions as the caller.

In particular, the context manager logging_exceptions.log_at_caller can be used in helper functions to temporarily update a logger’s ignored_functions in order to record a function’s caller instead of the function.

The following code will emit a log message with the function name set to foo, not helper_function:

import logging_exceptions as exlog
logger = logging.get_logger(__name__)
def helper_function():
   with exlog.log_at_caller(logger):
     logger.log("This is a log message")
def foo():
   helper_function()

Commandline convenience functions

The following convenience functions are not directly related to exceptions, but useful if you use argparse.

Add the ‘–verbose’, ‘–debug’ and ‘–quiet’ options to an argparse.Argumentparser instance.

import argparse
import logging_exceptions as exlog

parser=argparse.ArgumentParser("Some help text")
exlog.update_parser(parser)
args = parser.parse_args()

logging.basicConfig()
# The following call updates the log levels of the root logger
# and potential some other loggers.
exlog.config_from_args(args)

Now the script can be used from the commandline like this:

# Set the log-level for the loggers with the names `path.to.module1`
# and `path.to.module2` to DEBUG.
python script.py --debug path.to.module1,path.to.module2

Examples

See the file ‘logging_exceptions_examples.py’

Comparison to logging.handlers.MemoryHandler

The logging.handlers module contains a handler for a similar purpose: The MemoryHandler. It buffers log messages and only emits them, if a log record of severity error or above is encountered. I will quickly explain the differences between MemoryHandler and my module:

MemoryHandler is great if you know that an event of severity ERROR may occur in the future (typically in the same function) and you want to prepare for this potential exception. Typically, you know the scope for which the exceptions have to be buffered and you know when the buffered exceptions are no longer needed and can be discarded.

While for MemoryHandler the error condition is rather unspecific, the scope in which we have to decide between discarding and emitting the log messages is well known.

The log_to_exception decorator, on the other hand, is useful if the exception is well specified (it is already created/ caught), but the the scope in which the exception may or may not be caught is unspecified. Examples would be library functions that raise an error.

A typical example would be the following:

import logging
from logging_exceptions import log_to_exception

# Whoever calls public_function may want to catch the ValueError and hide
# the log messages or change their level to logging.DEBUG
def public_function():
    logger = logging.getLogger(__name__)
    a = some_complex_calculation(12)
    try:
        some_function(a)
    except ValueError as e:
        with log_to_exception(logger, e):
            log.error("While calling `some_function` with %s, "
                      "which is result of `some_complex_calculation`(%d),"
                      " an error occurred", a, 12)
        raise

Compatibility

Compatible with python 2.7 and python 3

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

logging_exceptions-0.1.7.tar.gz (8.4 kB view details)

Uploaded Source

Built Distributions

logging_exceptions-0.1.7-py3.5.egg (11.6 kB view details)

Uploaded Source

logging_exceptions-0.1.7-py2.py3-none-any.whl (10.1 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file logging_exceptions-0.1.7.tar.gz.

File metadata

File hashes

Hashes for logging_exceptions-0.1.7.tar.gz
Algorithm Hash digest
SHA256 6453acb0d4e435866ee0869a77a5801e4fd16c0e7a803a3c74c53fb15b981c68
MD5 03cba0bd91efbf0c1e07ee0a89aa37ed
BLAKE2b-256 59652a50cb58001f49ae7744058ed2e18b39b2841e7ef3938b3b9567b4634338

See more details on using hashes here.

File details

Details for the file logging_exceptions-0.1.7-py3.5.egg.

File metadata

File hashes

Hashes for logging_exceptions-0.1.7-py3.5.egg
Algorithm Hash digest
SHA256 e8ea9e48b7dd75eb97ed09064f79f3b05c8cbfda986592ff932c3700c4ad24e3
MD5 ba4f843a6381747a38a797889bb4cd52
BLAKE2b-256 32fd64e0916b57a50ef51058053fd49d65d610c4051063ee2854fe8ea1cbf032

See more details on using hashes here.

File details

Details for the file logging_exceptions-0.1.7-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for logging_exceptions-0.1.7-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 acc19fd2ab2921101216a67fe3d355bcde8f9223d512bfc6d1b0f2c71cec9ba4
MD5 3ee6ac4028c2cb7e283a68f6819a1fe4
BLAKE2b-256 6428bf90b0cd5d90dbbab9fd166c0b50b69a054770b23a8c114885ad2db94051

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page