0

I deployed a python function to google cloud which I use as a backend http hook for a mobile app via google firebase. The mobile app sends some data to the firebase backend, the function is triggered and sends some data back to the mobile app. This approach worked well and I am quite happy with its functionality.

In this python function I used python's print() function which then appeared in the logs on firebase. This also worked well until a month or so, but now the logs are simply empty. Not even the execution of the function shows up anymore. However the function still works when I trigger it from the app. I have heard from a friend that stdout does not work anymore, but why does python print() not work anymore? I am puzzled by this behavior, can anybody enlighten me?

U_flow
  • 475
  • 5
  • 18

1 Answers1

1

It's an intermittent problem with how Python flushes to the buffer. I've seen it where it sometimes works with Functions/Cloud Run and sometimes doesn't. It's a known issue with Python.

This SO post covers a lot of the details:

How can I flush the output of the print function (unbuffer python output)?

To verify this is the problem, try:

print("Hello, World!", flush=True)

To see if that solves it.

If it does, there are a lot of options in that post that cover the various ways you can force Python to flush print messages.

Gabe Weiss
  • 3,134
  • 1
  • 12
  • 15
  • Do I understand correctly, that the ```print()``` function should in theory still output to the ```info``` logs in firebase, but that I do not correctly flush the output? Wouldn't it be better to use dedicated logging functionality via ```google.cloud.logging```? – U_flow Mar 04 '22 at 08:53
  • No, print() won't output anything to the logs at all. The problem is that the function kills the connection/application before the buffer gets flushed, so nothing outputs at all. It WOULD be better to use the dedicated logging functionality, but that's often more of a pain than it's worth if you're just doing some quick debugging, for example. If it's production logging then yes, using the logging APIs is more reliable/production-ready. – Gabe Weiss Mar 04 '22 at 19:00