Home > Enterprise >  Why can't I use the logging module on Python 3.10?
Why can't I use the logging module on Python 3.10?

Time:04-03

A simple code I want to run to discover what "logging" module is:

import logging

logging.debug('This is a debug message')

This code is not working for me. I'm on SublimeText 3, my Python version is 3.10

I've got pip version 22.0.4, ez_setup version 0.9 and setuptools version 61

The logging module is in my lib:(picture) I don't understand... Can you help me please?

CodePudding user response:

The default output for Python logging is stdout, so if it is working it must print to the console. Why is not it working? Because first of all you must get a logger

logger = logging.getLogger('__main__')

Then you must set the level. By default DEBUG is not logging.

logger.setLevel(logging.DEBUG)

Now it must work

logger.debug('your message')

CodePudding user response:

According to the docs, the root logger is created with level WARNING, hence the debug call is ignored. You can change the level with setLevel:

>>> import logging
>>> logging.debug('test')
>>> logging.getLogger().setLevel(logging.DEBUG)
>>> logging.debug('test')
DEBUG:root:test
  • Related