Can't get flask to log to opt/python/log on Elastic Beanstalk AWS

2.3k Views Asked by At

I have been trying to get my Flask app to log to a log file. In elastic beanstalk I have a logger in my flask app that logs files to

opt/python/log

The file is successfully created but when I try to do something like

log.warning('ERROR')

The line does not show up correctly. I have tested to make sure my logger works correctly on localhost. I have read many stack overflow responses and believe it's a permissions issue, however when I try adding

commands:
  01_change_permissions:
command: chmod g+s /opt/python/log
  02_change_owner:
command: chown root:wsgi /opt/python/log

to

.ebextensions

Or even something like

files:
  "/opt/elasticbeanstalk/tasks/taillogs.d/history.conf" :
     mode: "000755"
     owner: root
     group: root
     content: |
       /opt/log/python/history.log

Nothing seems to work. Any help would be appreciated!

3

There are 3 best solutions below

2
On

Read the docs.

I think you should use logger like: app.logger.warning('ERROR').

0
On

Pulling from this answer regarding logging for django app.

In command 2 you need to change both the owner and group of the directory to be: wsgi.

A third command is also necessary to change the default owner

also, I had to add a third command to set the file access control. Someone smarter than me can chime in with why it is necessary, but without it, I've run into problems.

your .ebextensions config file should be:

commands:
  01_change_permissions:
    command: chmod g+s /opt/python/log
  02_change_owner:
    command: chown wsgi:wsgi /opt/python/log
  03_change_default_owner:
    command: setfacl -d -m g::rw /opt/python/log
0
On

There were two pieces to the puzzle.

Firstly:

The logger needs to be connected to the application as per this GitHub

https://gist.github.com/sm-azure/c4b2edd8a726d81f42c98ee40fc1730a

application.logger.addHandler(YourFileHandler)

Secondly:

You need to call the logger via the application.logger, not just the logger; i.e.

application.logger.debug("This text will log")

The following form will NOT WORK:

logger.debug("This text will NOT log")