I'm using the Python logging module in a simple script of mine with the following setup at the moment.
logging.basicConfig(format='%(asctime)s %(message)s', level=logging.INFO, datefmt='%Y-%m-%d %H:%M:%S')
logger = logging.getLogger(__name__)
My problem is that this also catches 3rd party modules like requests and output info() log messages from them. Is there any way to suppress these messages or tell the logging module only to log messages from my own script?
Using named loggers in your modules:
you can set the log level for all the other loggers to error and for your loggers to debug:
The above answer is not really correct - it will just set the bar higher for messages from other modules to be shown.
A very quick approach would be to use this piece of code:
You have to set this after importing all modules - it will disable all loggers that were created up to this point. This will work most of the time, but some modules create their logger when you make a class instance for example (which would happen later in your code).
When you set up loggers according to the basic python tutorial they tell you to use
logging.basicConfig(...)
. This is a problem as this will set the handler (aka where the log will be routed to) tologging.lastResort
which is stderr starting with Python 3.2 for all loggers globally in the process. This means you now have enabled full logging for all modules.So a better approach is to create a different logger only for your modules and give it some handlers of its own instead of using
basicConfig()
.There are two ways of doing that:
1) All function:
This will give you the logger
log
that you can then use likelog.error("Error found")
. It will write to a new file called mylog.log and will also log so sys.stdout. You can change this as you like of course.2) Using a dict:
This will give the same result as the above, a bit longer, but maybe easier to read. This will automatically also set
'disable_existing_loggers': True
. If you do not want that, you have to add it and set it to False.