This is a very basic question on logging, straight from the documentation.
I am running the following example code from the documentation on logging. The behavior is not what I would expect:
-
There is no output to the file. I created the respective file before running the code. Why is this?
-
The list of handlers is empty. Adding
logger.addHandler( logging.FileHandler('./test.log') )
after setting the config adds a handler to the list with the path to the correct file, but still there is no output to the file. Its level is ‘NOTSET’. Addinglogger.setLevel()
seems to have no effect on this. -
When removing the filename attribute from the config,the messages are printed to the console, as expected. However, when removing the config line completely, I would expect the warning and and error to be printed, but they are not.
import logging
logger = logging.getLogger(__name__)
logging.basicConfig(filename='./test.log', level=logging.DEBUG)
logger.debug('This message should go to the log file')
logger.info('So should this')
logger.warning('And this, too')
logger.error('And non-ASCII stuff, too, like Øresund and Malmö')
print(logger.handlers)
I am running the code from the terminal in an environment with Python 3.11.0.
There are a lot of related questions (see list below), but none of them seem to answer my question:
- Problem here and here was to use
logging
instead oflogger
as object - Here there were additional parts of code, where the config was called multiple times. For me, there is just the code displayed above in the script.
- The common mistake mentioned here of calling the config only after a printing statement is not the case for me.
- Here a if condition wasn’t triggered.
a_member is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.