I am using the logging module to log in my application:
import logging
logging.basicConfig(level=logging.INFO)
However, this causes CherryPy to log all lines two times (one in stdout, one in the logging module; which happens to be set to stdout currently):
[02/Jun/2023:11:17:54] ENGINE Bus STARTING
INFO:cherrypy.error:[02/Jun/2023:11:17:54] ENGINE Bus STARTING
[02/Jun/2023:11:17:54] ENGINE Started monitor thread 'Autoreloader'.
INFO:cherrypy.error:[02/Jun/2023:11:17:54] ENGINE Started monitor thread 'Autoreloader'.
[02/Jun/2023:11:17:54] ENGINE Serving on http://0.0.0.0:5005
INFO:cherrypy.error:[02/Jun/2023:11:17:54] ENGINE Serving on http://0.0.0.0:5005
[02/Jun/2023:11:17:54] ENGINE Bus STARTED
INFO:cherrypy.error:[02/Jun/2023:11:17:54] ENGINE Bus STARTED
Of course, I only want the lines that are sent to the logging module, so I can control all logging in my application in the same way.
How can I prevent CherryPy to write the log lines also to stdout?
In general you should avoid
basicConfig()for all but the simplest scenarios (which is what it's intended for), because it gives you fairly limited control of the configuration.In this case, it's simple - you can just stop the
Cherrypyevents from being passed to the root logger's handler, which you configured in thebasicConfig()call. Here's an example which shows how to do that:When run, this prints: