Skip to main content

Move logging code out of your business logic with decorators

Project description

Downloads

Move logging code out of your business logic with python decorators.

Logging is a nice tool in your toolbox for tracing bugs and getting a better sense how your application is working in production. But if you are like me, you often omit logging code, so it will not hide business logic or feature your code with complexity.

Fortunately pythons decorator now came to our rescue and provides us with a nice library to add logging to our code without stealing readability and understandability.

If you want to know more about the intentions behind logdecorator check out my blog post.

Update: logdecorator==2.0

Thanks to all dependants :) I just released a new version of logdecorator (2.0). You can read the changes at my blog or in the changelog.

Installation

Installation is as easy as it can get:

$ pip install logdecorator

How to use it

Imagine a function download with no arguments and some download code in it.

def download():
    # some download code
    pass

if __name__ == "__main__":
    download()

Say you are going to launch your new tool but want to add some logging before releasing the kraken. Your code will probably look something like the following:

import logging
from .exceptions import MyException1, MyException2

logger = logging.getLogger(__name__)


def download():
    logger.debug("Start downloading")
    # some download code
    logger.debug("Downloading finished successfully")


if __name__ == "__main__":
    try:
        download()
    except (MyException1, MyException2):
        logger.error("Error on downloading")
        raise

You just added at least a couple lines of code which are eventually stumbling in your way when you are trying to understand your business logic to find a bug. But what’s even worse is, that you added an additional indentation (try:… except: …) just for the sake of logging.

With logdecorator you can leave your code nearly as it was and reach the same goals.

import logging
from logdecorator import log_on_start, log_on_end, log_on_error
from .exceptions import MyException1, MyException2


@log_on_start(logging.DEBUG, "Start downloading")
@log_on_error(logging.ERROR, "Error on downloading",
              on_exceptions=(MyException1, MyException2),
              reraise=True)
@log_on_end(logging.DEBUG, "Downloading finished successfully")
def download():
    # some download code


if __name__ == "__main__":
    download()

Maybe the improvement to the previous snippet does not seem great for you but if you actually fill in business logic into # some download code it should become obvious :)

What logdecorator can do for you

Decorators

logdecorator provides four different built-in decorators:

  • log_on_start
  • log_on_end
  • log_on_error
  • log_exception

whose behaviour corresponds to their names.

Use variables in messages

The message, given to the decorator, is treated as a python format string which takes the functions arguments as format arguments.

Sticking to the previous example one could write:

import logging
from logdecorator import log_on_start
from .exceptions import MyException1, MyException2


@log_on_start(logging.DEBUG, "Start downloading '{url:s}'")
def download(url):
    # some download code


if __name__ == "__main__":
    download("http://my.file.com/file.bin")

Which results in the message Start downloading 'http://my.file.com/file.bin' gets logged.

Arguments

log_on_start

log_level > The log level at which the message should be send

message > The message to log

logger (optional) > An alternative logger object. If no logger is given logdecorator creates a > logger object with the name of the module the decorated function is in > (decorated_function.__module__) > > Default: Creates a new logger with the name decorated_function.__module__

log_on_end

log_level > The log level at which the message should be send

message > The message to log

logger (optional) > An alternative logger object. If no logger is given logdecorator creates a > logger object with the name of the module the decorated function is in > (decorated_function.__module__) > > Default: Creates a new logger with the name decorated_function.__module__

result_format_variable (optional) > The variable name one can use in the message to reference the result of the > decorated function > e.g. @log_on_end(DEBUG, “Result was: {result!r}”) > > Default: “result”

log_on_error

log_level > The log level at which the message should be send

message > The message to log

logger (optional) > An alternative logger object. If no logger is given logdecorator creates a > logger object with the name of the module the decorated function is in > (decorated_function.__module__) > > Default: Creates a new logger with the name decorated_function.__module__

on_exceptions (optional) > A tuple containing exception classes or a single exception, which should get > caught and trigger the logging of the log_on_error decorator. > > Default: tuple() (No exceptions will get caught)

reraise (optional) > Controls if caught exceptions should get reraised after logging > > Default: False

exception_format_variable (optional) > The variable name one can use in the message to reference the caught exception > raised in the decorated function > e.g. @log_on_error(ERROR, “Error was: {e!r}”, …) > > Default: “e”

log_exception

log_level > The log level at which the message should be send

message > The message to log

logger (optional) > An alternative logger object. If no logger is given logdecorator creates a > logger object with the name of the module the decorated function is in > (decorated_function.__module__) > > Default: Creates a new logger with the name decorated_function.__module__

on_exceptions (optional) > A tuple containing exception classes or a single exception, which should get > caught and trigger the logging of the log_on_error decorator. > > Default: tuple() (No exceptions will get caught)

reraise (optional) > Controls if caught exceptions should get reraised after logging > > Default: False

exception_format_variable (optional) > The variable name one can use in the message to reference the caught exception > raised in the decorated function > e.g. @log_on_error(ERROR, “Error was: {e!r}”, …) > > Default: “e”

Project details


Download files

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

Files for logdecorator, version 2.1
Filename, size & hash File type Python version Upload date
logdecorator-2.1-py3-none-any.whl (8.6 kB) View hashes Wheel py3
logdecorator-2.1.tar.gz (5.6 kB) View hashes Source None

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page