Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

593
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda