Python Flask weird logging behavior (kubernetes)

Nurbol Alpysbayev

So I made a very simple flask-based app and hosted it in a kubernetes pod.

When I open the logs in Rancher, I can see this warning:

 * Serving Flask app "app/preapproved_limits/api.py"
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off

that I see as well when I start flask on my local machine.

But what I don't see on my local machine and do see in Rancher is this:

10.0.67.20 - - [02/Jul/2020 16:49:20] "GET /health HTTP/1.1" 200 -
INFO:werkzeug:10.0.67.20 - - [02/Jul/2020 16:49:20] "GET /health HTTP/1.1" 200 -

which gets logged every 5 seconds. What the heck?

But the most important and annoying thing is I can't see my logs which I make with the Python's print() function.

Can someone explain:

  • Why do I see something both in Rancher and locally (the initial Flask warning), but
  • why do I see something only in Rancher (the /health or INFO:werkzeug logs)
  • and why do I see something only locally on my machine, but not in Rancher (print())
Tom Wojcik

/health is a normal healthcheck endpoint that k8s needs.

Flask doesn't print to stdout by default because it buffers lines to make I/O more efficient. You can either call sys.stdout.flush(), print(flush=True), set env var PYTHONUNBUFFERED=1 in your Dockerfile (probably the easiest) or just use logging module as we all should.

You can read more about this env var here.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Weird JUnit logging behavior

From Dev

Python datetime weird behavior

From Dev

python thread weird behavior

From Dev

python dictionary weird behavior

From Dev

Set weird behavior (python)

From Dev

Weird scoping behavior in python

From Dev

Weird behavior in Python in array initialization

From Dev

Python, weird memory consumption behavior

From Dev

Weird behavior in python list concatenation

From Dev

The weird behavior of the print function in python

From Dev

Weird python file path behavior

From Dev

Weird sets behavior in python REPL

From Dev

Weird behavior with a lot of numbers python

From Dev

python dictionary weird behavior with dict as container

From Dev

Weird Behavior When Slicing a List in Python

From Dev

Python: Weird behavior while using `yield from`

From Dev

Knapsack algorithm, weird behavior (python3)

From Dev

Python getting ranks of elements in list weird behavior

From Dev

Weird behavior of barplot from python matplotlib with datetime

From Dev

multiprocessing.Queue weird python behavior

From Dev

Decimal Python Library has weird behavior

From Dev

Weird behavior of (^)

From Dev

Confusing behavior of the Python logging module in a multiprocessing context

From Dev

OS dependent behavior with python logging module

From Dev

logging is not working under Flask in Python 3

From Dev

Python Flask - tracking a request for logging purposes

From Dev

strange difference in python behavior with Requests, Flask and Kivy

From Dev

Python Class & Class Instantiation shows a very weird behavior

From Dev

Template / Generic user-defined classes in python weird behavior