gouge.filters

This module contains generally useful filters.

class gouge.filters.ShiftingFilter(shift_by=0, logger='', min=0, max=50, offset=0)

Bases: Filter

This filter will shift the logging level of log records a certain number of log levels.

For example:

ShiftingFilter(1) will convert a message with level INFO to WARNING, and ShiftingFilter(-1) will do the reverse.

An example use-case is controlling log levels of libraries: It is possible that a library emits error-messages, but in the context of your application you would like to see those as warning messages only. If you have no control over that library your hands are tied and the only thing you can do is suppress these messages by changing the level of the associated logger. By modifying the log level using this filter still allows you to see the messages, albeit with another level which is more appropriate to your application.

Note

Filters can either be attached to handlers or loggers. But be aware that when attaching to loggers, they will only trigger on the exact logger they were attached to. Not in the parent hierarchy! See logging.Filter.filter() for details. To simplify attaching to loggers, this class offers the method inject().

Parameters:
shift_by=0

The number of levels to shift. Positive integers will increase the level, negative integers will decrease it.

logger=''

This and all child loggers will be shifted.

min=0

Don’t shift below this level.

max=50

Don’t shift above this level.

offset=0

An explicit, fine-grained offset value. This overrides shift_by!

inject(parent)

Loop over each known logger and attach this filter.

You can remove the attached filters again using cleanup().

Note

This will only attach the filter to loggers which already exist! If you see that something is not working as expected, make sure the logger exists before calling this! You can look at all the existing loggers using logging.Logger.manager.loggerDict.

Parameters:
parent

Attach the filter to this and all descendant loggers. Either a logger name or a logging.Logger instance (its name is used).

Return type:

None

cleanup()

Remove all filters applied via inject().

Return type:

None

filter(record)

Always returns True but will modify the logging level of record by the rules defined in this filter.

See logging.Filter.filter()

Return type:

bool