Forums

Writing to error log causes 502 error

I'm trying to write to the error log (sys.stderr) using the code below. In this example I trigger the write by sending a GET request to my app's home URL. When I do this, the error log immediately fills up with a massive amount of text and causes a 502 error. This same code is working fine on my development server, so not sure why it's causing problems on PA. Insights much appreciated!

 

logger = logging.getLogger()
handler = logging.StreamHandler(stream=sys.stderr)
logger.addHandler(handler)
logger.setLevel(logging.DEBUG)
logger.debug("Debug message")

You've probably set up a logging loop with the existing logging: the default logger on PythonAnywhere sends stderr to the error log and your code adds another handler for stderr. So they end up cycling the message through each other over and over again until the web app crashes.

That was it, thanks. I was using this post as a reference, which I interpreted to mean that I should manually select stderr using StreamHandler. That may just be my poor interpretation :)