skip to Main Content

I have a python flask app running on EC2.
To enable UI interface to be functional, I run this on ec2 after logging in as the correct user:

uwsgi --socket 0.0.0.0:8000 --protocol=http -w wsgi:application  -z 120 --http-timeout 120 --enable-threads -H /home/test/env --daemonize /home/test/test.log

and the UI becomes operational. When i click the download button certain computations take place before the csv becomes ready to download. I don’t have a uwsgi.ini file or any kind of nginx.

After a point the test.log becomes a very large file;
How do I write log files so that each days log gets written to a separate file?
or what is the best way to prevent one large log file from getting generated and instead have something that is more manageable?

2

Answers


  1. You could implement log rotation using Python logging handlers:

    from flask import Flask
    import logging
    from logging.handlers import RotatingFileHandler
    
    app = Flask(__name__)
    
    if __name__ == '__main__':
        handler = RotatingFileHandler('test.log', maxBytes=12000, backupCount=5)
        handler.setLevel(logging.INFO)
        app.logger.addHandler(handler)
        app.run()
    

    https://docs.python.org/3/library/logging.handlers.html#rotatingfilehandler

    This would rollover the logs when the maxBytes size has been reached. The primary log file will remain test.log but the filled log will be renamed to test.log.1, test.log.2 and so on.

    Login or Signup to reply.
  2. Uses the TimedRotatingFileHandler class.

    The TimedRotatingFileHandler object will create a log file for each day, but you will have to handle it within the server application code

    import os
    import logging
    from logging.handlers import TimedRotatingFileHandler
    from datetime import datetime
    
    
    logger = logging.getLogger(__name__)
    logger.setLevel(logging.INFO)
    
    formatter = logging.Formatter('[%(asctime)s] %(levelname)s: %(message)s', datefmt='%Y-%m-%d %H:%M:%S')
    
    handler = TimedRotatingFileHandler(os.path.join('./', 'custom_log.log'), 
                            when='midnight', backupCount=7)
    handler.setLevel(logging.INFO)
    handler.setFormatter(formatter)
    
    logger.addHandler(handler)
    
    logger.info("Info")
    logger.warning("Warning")
    logger.error("Error")
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search