Setup Logging

Setup

drypy uses the standard python logging facility to display messages. Each drypy module initializes its own logger using the standard syntax:

logger = logging.getLogger(__name__)

Therefore, it’s enough to correctly configure the logger named drypy to get the output.

Follows a working example:

import logging
from drypy import dryrun
from drypy.patterns import sham

logger = logging.getLogger('drypy')
logger.setLevel(logging.INFO)
h = logging.StreamHandler()  # print to console
h.setLevel(logging.INFO)
logger.addHandler(h)

@sham
def do_something():
    print('hello')

do_something()
# now activate dryrun
dryrun(True)
do_something()

You should get the following output in your console:

hello
[DRYRUN] call to 'do_something()'

Configure custom logging level

By default, drypy emits logs with level logging.INFO. You can set a custom log level using the set_logging_level function:

import logging
from drypy import set_logging_level

set_logging_level(logging.DEBUG)

This will affect all the logs emitted by drypy.

It’s also possibile to set a specific log level for each decorated function by using the log_level argument on the drypy.patterns.sham() decorator. This value overrides the global setting above. Please refer to the API documentation for more details.

Important

This function will affect just the level of emitted logs. Please ensure that both the drypy logger and the attached handlers have a log level equal or lower to this level, otherwise they will filter out logs. Or leave them to logging.NOTSET to rely on root logger configuration. E.g., setting set_logging_level(logging.WARNING) and the drypy logger to logging.ERROR will produce no logs. Please refer to official python logging docs for more.