1

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?

0

1 Answer 1

2

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)
Sign up to request clarification or add additional context in comments.

2 Comments

Just to add, the logging configuration I set above also worked in logging errors to the correct files. I found that the solution was to remove the following: app.debug = True, and then all specified logging worked as expected.
Good to know that you have figured out the problems.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.