How do I store, capture and tail Python logs in Elastic Beanstalk?

0

I have a simple "hello world" Flask application deployed into AWS Elastic Beanstalk. Before I start building my application I want to ensure everything is working, including simple application logging. I followed this AWS article on setting it up, but I can't get my log from EB (and so I don't know whether it got created and logged messages or not either).

Here is my .ebextensions/logging.config file:

files:
  "/opt/elasticbeanstalk/tasks/taillogs.d/application.conf" :
    mode: "000755"
    owner: root
    group: root
    content: |
      /tmp/application.log
      
  "/opt/elasticbeanstalk/tasks/bundlelogs.d/application.conf" :
    mode: "000755"
    owner: root
    group: root
    content: |
      /tmp/application.log

Here is the Flask code in my app, which shows in the output console when running locally and creates the expected log file in /tmp/application.log on my local macOS system. But "eb logs" as well as GUI download of logs doesn't contain my /tmp/application.log. What am I doing wrong?

application.py

from flask import Flask, render_template
import logging
from logging import FileHandler
from logging.handlers import RotatingFileHandler

# EB looks for an 'application' callable by default.
application = Flask(__name__)

#  routes
@application.route('/')
def index():
    print("Hello from print")
    application.logger.info("Hello from logger")
    return render_template('index.html')

# run the app (this has to go last otherwise the routes above are not registered yet)
if __name__ == "__main__":
    formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
    stream_handler = logging.StreamHandler()
    stream_handler.setLevel(logging.DEBUG)
    stream_handler.setFormatter(formatter)
    # file_handler = RotatingFileHandler('tmp/application.log', maxBytes=1024,backupCount=5)
    file_handler = FileHandler('/tmp/application.log')
    file_handler.setLevel(logging.DEBUG)
    file_handler.setFormatter(formatter)
    application.logger.addHandler(stream_handler)
    application.logger.addHandler(file_handler)
    application.logger.setLevel(logging.DEBUG)
    application.run(use_debugger=True, use_reloader=False, passthrough_errors=True)
asked 7 months ago93 views
No Answers

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions