Reputation: 1559
I have this piece of code to set my logger in Python:
#Configure logging
logging.basicConfig(format = '%(asctime)s %(name)-12s %(levelname)-8s %(message)s',
datefmt='%m-%d %H:%M',
filename= "log.txt",
level = logging.getLevelName('DEBUG'))
print(logging.getLogger().getEffectiveLevel())
But the output from the print statement sometimes is this:
30
And other times is this (which is correct):
10
But often even when the the logging level is set to the correct number, it is not logging anything to the file, but other times it works. What do I need to do to make sure my logging level is set correctly?
*Edit: Below is my solution based off the recommendation of @randomir.
**Edit: I had to make a second change where I set the level after I call logging.basicConfig()
or else the logging level still was not getting called consistently. The line `logging.getLogger().setLevel(...) now seems to work.
I created a new class: Logger.py.
import logging
class Logger(object):
def __init__(self):
logging.basicConfig(format = '%(asctime)s %(name)-12s %(levelname)-8s %(message)s',
datefmt='%m-%d %H:%M',
filename= "log.txt")
logging.getLogger().setLevel(logging.getLevelName("DEBUG"))
print(logging.getLogger().getEffectiveLevel())
And now instead of configuring the basic config directly in my startup class I just instantiate the logger class once:
from Logger import Logger
import logging
class LaunchPython(object):
#Configure Logging
Logger()
logging.info("Application has started")
On all subsequent classes that call the logger I just put import logging
on the top and then do logging.info(...)
or logging.debug(....)
. There is no need to import the Logger.py class and reinstantiate it.
Upvotes: 2
Views: 1360
Reputation: 18697
The logging.basicConfig()
creates a root logger for you application (that's a logger with the name root
, you can get it with logging.getLogger('root')
, or logging.getLogger()
).
The trick is that the root
logger gets created with defaults (like level=30
) on the first call to any logging function (like logging.info()
) if it doesn't exist already. So, make sure you call your basicConfig()
before any logging in any other part of the application.
You can do that by extracting your logger config to a separate module, like logger.py
, and then import that module in each of your modules. Or, if your application has a central entry point, just do the configuration there. Note that the 3rd party functions you call will also create the root
logger if it doesn't exist.
Also note, if you application is multi-threaded:
Note This function should be called from the main thread before other threads are started. In versions of Python prior to 2.7.1 and 3.2, if this function is called from multiple threads, it is possible (in rare circumstances) that a handler will be added to the root logger more than once, leading to unexpected results such as messages being duplicated in the log.
Upvotes: 4