I have a python app served by gunicorn on port 8000 on an ec2 instance as shown below:
main.py
import os
import subprocess
import sys
import argparse
from endpoints import app
import logging
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Run the Flask app with Gunicorn.")
parser.add_argument('--mode', choices=['prod', 'dev'], default='dev',
help="Specify the mode to run the application: 'prod' or 'dev'")
parser.add_argument('--port', default=8000,
help="Specify the port to run the application")
args = parser.parse_args()
port = int(os.environ.get("PORT", args.port))
os.environ["MODE"] = args.mode
os.environ["PORT"] = str(port)
if args.mode == 'dev':
app.debug = True
app.logger.addHandler(logging.StreamHandler(sys.stdout))
app.logger.setLevel(logging.INFO)
else:
app.debug = False
print(f”””Server running in {
'development mode' if app.debug else 'production mode'} on PORT {port}""")
pythonpath = os.path.dirname(os.path.abspath(__file__))
os.environ["PYTHONPATH"] = pythonpath
cmd = [
"gunicorn",
"-c", "app/gunicorn_config.py"
]
try:
subprocess.check_call(cmd)
except subprocess.CalledProcessError as e:
print(f"Error starting Gunicorn: {e}", file=sys.stderr)
sys.exit(1)
and here is my gunicorn config file
gunicorn_config.py
import os
port = int(os.environ.get("PORT", 8000))
bind = f"0.0.0.0:{port}"
workers = 4
accesslog = "-"
reload = True
wsgi_app = "app.endpoints:app"
Whenever I ssh into my ec2 instance and the terminal is opened i would love to see the logs of the running server. I know the logs can be piped to a file, but I don’t want to pipe to file. I just want a way to connect to the running server and see the latest logs as they are streaming in.
Is there a way to do that?
1