Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where do prints go when running Flask with Apache?

I just deployed the first website I built with Flask to a production server. I enabled logging using the logging.handlers.RotatingFileHandler to /log/myapp.log, with which I can log messages using for example:

current_app.logger.error('this is a massive error')

This works fine. It makes me wonder about some pieces of code though, which contain for example print 'some info here' which I used while debugging. Where do these printouts go? Into the void /dev/null or somewhere else? Is there a possibility to catch them somehow?

All tips are welcome!

like image 643
kramer65 Avatar asked Jul 22 '15 09:07

kramer65


1 Answers

Everything you output using print goes to the standard output, i.e. /dev/stdout on a Unix server (by default). Since apache is running as a service, you will probably never see these output.

One way to deal with this is to redirect the standard output of your scripts to some files:

>>> import sys
>>> sys.stdout = open('output.logs', 'w')
>>> print('Hello World!') # Nothing appears bellow
>>> sys.stdout = sys.__stdout__ # Reset to the standard output
>>> open('output.logs', 'r').read()
'Hello World!\n'
like image 61
Holt Avatar answered Sep 19 '22 08:09

Holt