Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

602
Views
how do I save flask app logs separately per day instead of one giant file

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?

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

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.

over 4 years ago · Santiago Trujillo Report

0

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")
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!