crossbario / txaio

Utilities to support code that runs unmodified on Twisted and asyncio
MIT License
58 stars 30 forks source link

aio._TxaioLogWrapper._set_log_level does not set log level in underlying logging.Logger instance #131

Open blinkingmatt opened 6 years ago

blinkingmatt commented 6 years ago

System:

The txaio logging framework delegates the info, warning and the like calls to a "logging.Logger" instance. But calling "_TxaioLogWrapper._set_log_level" does not set log level in underlying "logging.Logger" instance. The corresponding methods of "logging.Logger" check for the effective log level. Without setting the log level in "_set_log_level" the log level of the root logger (parent) is always used.

I suggest a small addition in _"_TxaioLogWrapper._set_loglevel":

class _TxaioLogWrapper(ILogger):
    ......

    def _set_log_level(self, level):
        target_level = log_levels.index(level)
        # this binds either _log or _no_op above to this instance,
        # depending on the desired level.
        for (idx, name) in enumerate(log_levels):
            if idx <= target_level:
                log_method = functools.partial(_log, self, name)
            else:
                log_method = _no_op
            setattr(self, name, log_method)
        self._log_level = level

        # must set log level in logger
        if level == 'trace':
            level = 'debug'
        self._logger.setLevel( level.upper() )
sethrh commented 4 years ago

I just spent the last couple hours tracking this down, and of course didn't find this bug until I was done. Still same issue in txaio 20.4.1. I made a gist to repro: https://gist.github.com/sethrh/331cadc538051cb0c0eb1de001a3b70f

I found this because I wanted to set my logger's level to debug, while setting all of the autobahn.websocket* loggers to warn.

Additionally, individual loggers should have an interface to set their log levels.