Capture and log all Python warnings

- Capture all Python warnings so that they can shown as part of logging system
  on console and in log file.

- Also capture deprecation warnings into logging system if debug mode is
  enabled. Current versions of Python disable deprecation warnings by default.
  Django 1.11 also follows this approach now.

Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>

Reviewed-by: Johannes Keyser <johanneskeyser@posteo.de>
This commit is contained in:
Sunil Mohan Adapa 2017-08-08 17:17:57 +05:30 committed by Johannes Keyser
parent a9c7e28c3e
commit c2cf591e1b
No known key found for this signature in database
GPG Key ID: D1431C2C533CF0D0

View File

@ -26,6 +26,7 @@ import logging
import os
import stat
import sys
import warnings
import cherrypy
@ -82,6 +83,15 @@ def setup_logging():
cherrypy.log.error_file = cfg.status_log_file
cherrypy.log.access_file = cfg.access_log_file
# Capture all Python warnings such as deprecation warnings
logging.captureWarnings(True)
# Log all deprecation warnings when in debug mode
if cfg.debug:
warnings.filterwarnings('default', '', DeprecationWarning)
warnings.filterwarnings('default', '', PendingDeprecationWarning)
warnings.filterwarnings('default', '', ImportWarning)
def setup_server():
"""Setup CherryPy server"""