How to modifying the behaviour of the Python logging facility? -
i change way in messages debug , info level displayed when using python's native logging facility. "change", not mean altering format adding logical level. example:
# global variable set @ time of initializing logging. required_verbosity_level = 7 # variable passed each call logger. supplied_verbosity_level = 5
so when creating logger pass global requirement.
logger = loggerbridge(required_verbosity_level = 7)
then when call method, pass appropriate level:
logger.debug('this debug message.', supplied_verbosity_level = 5)
so internally, logic (5<7) , make message visible due fact supplied
value meets required
one. however, in following case:
logger.debug('this debug message.', supplied_verbosity_level = 11)
the message will never visible supplied
value higher required
value. question is: best place implement such behaviour?
right now, tried couple of things based on inheriting current logger class , overriding internal behaviour, known mixin
approach:
class loggerbridge(object): def __init__(self, required_verbosity_level): self.required_verbosity_level = required_verbosity_level def _log_bridge(self, logger): logger(message) def info(self, message, supplied_verbosity_level): if supplied_verbosity_level < self.required_verbosity_level: self._log_bridge(logging.info, message) def debug(self, message, supplied_verbosity_level): if supplied_verbosity_level < self.required_verbosity_level: self._log_bridge(logging.debug, message)
in theory, seems working. however, right way? there way solve using of built-in logging bits, such custom handler
or custom filter
?
if want custom debug levels it's supported out of box. set custom debug level (required_verbosity_level
in example) on logger using setlevel() , use log() method logging , pass custom level (supplied_verbosity_level
in example). if want further customize logic might want override isenabledfor() method.
Comments
Post a Comment