Reputation: 7185
I have a Python application which is deployed in production with Apache and mod_wsgi. In my application, I have setup logging as follows:
config
file:
log_file = os.path.dirname(os.path.abspath(__file__)) + "/app.log"
log_level = logging.DEBUG
__init__.py
file:
root_logger = logging.getLogger()
root_logger.setLevel(config.log_level)
log_format = logging.Formatter("%(asctime)s [%(levelname)s] [%(name)s] %(message)s")
file_handler = logging.FileHandler(config.log_file)
file_handler.setFormatter(log_format)
stream_handler = logging.StreamHandler()
stream_handler.setFormatter(log_format)
root_logger.addHandler(file_handler)
root_logger.addHandler(stream_handler)
This code is executed before the Flask application is created.
All logging that is not an error/exception is correctly logged to this custom file.
However, if an exception is generated while the web server is running, the errors are instead written to Apache's error log instead of the one configured here. How do I configure Python's logging/mod_wsgi/Apache so that everything is written to the log file configured here?
Upvotes: 1
Views: 3547
Reputation: 5164
Add your logger to WSGI application loggers, from flask error handling docs:
if not app.debug:
import logging
from themodule import TheHandlerYouWant
file_handler = TheHandlerYouWant(...)
file_handler.setLevel(logging.WARNING)
app.logger.addHandler(file_handler)
Upvotes: 2