Logging is an important task in any application. Logs help developers find problems in their applications during and after development. CherryPy also understands the importance of logs and will log all incoming requests as well as protocol errors in our application.
CherryPy manages two loggers:
Access log that logs every incoming requests
Application/Error log that traces errors or other application-level messages
We can access these logs by calling cherrypy.log().
We can also log exceptions explicitly using the following code:
try:
...
except Exception:
cherrypy.log("There is an exception.", traceback=True)
Both types of logs are written in files which can identified by the following keys in configuration:
log.access_file for incoming requests
log.error_file for the other log
To disable file logging, simply set an empty string to the log.access_file or log.error_file keys in the global configurations.
To disable, console logging, set the value of log.screen to False.
Eg.:
cherrypy.config.update(
{'log.screen': False, 'log.access_file': '', 'log.error_file': ''})
CherryPy applications allow use of other Python logging modules. One such tool is the 'logging' module which traces application level messages. The 'logging' module is part of the Python standard library and to use it we don't need to install anything. The benefit of using external loggers is the flexibility and extra features the provide which are not the part of CherryPy suite.
Comments