Logs apparently not working properly

Hello!

I followed the Docs’ Python deployment tutorial to learn about Fly.

It worked and I modified the code a little bit to check how I would handle logs and environment variables.

So in addition to the original code, I just addded code to print: 1) the string passed to the route and 2) the environment variable “ENV_USER”.

import sys
from flask import Flask, render_template
import os

app = Flask(__name__)

@app.route('/')
@app.route('/<name>')
def hello(name=None):
    print("[NAME]:", name, file=sys.stdout)
    print(os.getenv("ENV_USER", "STANDARD_USER"), file=sys.stdout)
    return render_template('hello.html', name=name)

The application is deployed and running without any error but its not working.

I am successfully requesting the endpoint but the info is not being logged as you can see in these images.
Screenshot from 2023-02-07 18-27-18

It works when I’m running the server locally, btw.

Anyone know what I’m doing wrong?

Thank you so much in advance :slight_smile:

2 Likes

Having the same problem, I am trying to make it work with using “logging”

logging.basicConfig(
    format="%(levelname)s:%(message)s\n", level=logging.DEBUG, stream=sys.stdout
)

Unfortunately it won’t show up in Fly’s logs for me either.

It looks like Python is buffering the messages. Try adding flush=True to your print(). ie:

print("[NAME]:", name, file=sys.stdout, flush=True)