Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

594
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda