Python logging to console
The problem comes from the call to basicConfig
which sets up a log handler for stderr and also accepts a format string, not a formatter. Because you are doing this work yourself later, you don't need to use the basicConfig
function. More information can be found in the python documentation.
Removing the call to basicConfig
, and adding a self.log.setLevel(self.log_level)
will fix the issue you are seeing.
Working code:
import logging import sys class Temp: def __init__(self, is_verbose=False): # configuring log if (is_verbose): self.log_level=logging.DEBUG else: self.log_level=logging.INFO log_format = logging.Formatter('[%(asctime)s] [%(levelname)s] - %(message)s') self.log = logging.getLogger(__name__) self.log.setLevel(self.log_level) # writing to stdout handler = logging.StreamHandler(sys.stdout) handler.setLevel(self.log_level) handler.setFormatter(log_format) self.log.addHandler(handler) # here self.log.debug("test") if __name__ == "__main__": t = Temp(True)
Looking through a similar issue on the Python bug tracker (https://bugs.python.org/issue16368), you can see that the formatter
argument is expected to be a string (hence the attempt to invoke find
):
log_format = '[%(asctime)s] [%(levelname)s] - %(message)s'logging.basicConfig(level=self.log_level, format=log_format)