Consider the following piece of code:
import logging
logging.info("toto")
logging.basicConfig(level="INFO",
format="%(asctime)s - %(threadName)s -"
" %(levelname)s"
" - [%(funcName)s():%(filename)s:%(lineno)s]"
" - %(message)s")
logging.info("info")
logging.error("error")
logging.warning("warn")
logging.debug("debug")
The output is the following:
$ python mmt_test.py
ERROR:root:error
WARNING:root:warn
However, with the following code:
import logging
logging.basicConfig(level="INFO",
format="%(asctime)s - %(threadName)s -"
" %(levelname)s"
" - [%(funcName)s():%(filename)s:%(lineno)s]"
" - %(message)s")
logging.info("info")
logging.error("error")
logging.warning("warn")
logging.debug("debug")
The output is:
$ python mmt_test.py
2021-12-07 07:44:46,711 - MainThread - INFO - [<module>():mmt_test.py:8] - info
2021-12-07 07:44:46,711 - MainThread - ERROR - [<module>():mmt_test.py:9] - error
2021-12-07 07:44:46,711 - MainThread - WARNING - [<module>():mmt_test.py:10] - warn
In the first code sample, why isn't basicConfig
applied to the logs ? Does using the logging
module before using basicConfig prevents to use basicConfig
afterwards ?
This happens in python 3.5.2
, 3.8.3
and 3.9