Forums

Empty log file from Flask web app

Hello,

I'm new to PythonAnywhere and I try to test a simple Flask web app. The app works well but I can't get RotatingFileHandler working well. It does work when I launch it on my local machine but once on PythonAnywhere, no logs are written into the specified file.

Here is the snippet of code regarding the RotatingFileHandler :

formatter = Formatter('%(asctime)s %(levelname)s: %(message)s '
                      '[in %(pathname)s:%(lineno)d]')
prod_file_handler = RotatingFileHandler('test.log', maxBytes=10000000, backupCount=3)
prod_file_handler.setFormatter(formatter)
prod_file_handler.setLevel(logging.INFO)
app.logger.addHandler(prod_file_handler)
app.logger.setLevel(logging.INFO)

I checked the permission rights on the file 'test.log' (and even tried chmod 777) but no result. Can you please help me :-( ? Thank you

Ok after digging around I think I got it. The previous linked snippet of code was inside

if __name__ == '__main__':

So it was not run when launched from PythonAnywhere right ? I moved it outside the main block and now the log file is correctly filled :-)

Ah, that would do it -- glad you worked it out!