Skip to Content
Technical Articles
Author's profile photo Yohei Fukuhara

Logging from Python Flask application deployed on Cloud Foundry

Hi All,

I am writing this blog to describe output logs from Flask based applications on SAP cloud foundry.

Logging is very important for efficient development.  With sap_cf_logging library, it is easier to output and see logs.

Please see my GitHub repository “YoheiFukuhara/cloudfoundry-python-logging” for further detail.

If you don’t know how to deploy application to Cloud Foundry, see another article “Create simple Flask REST API using Cloud Foundry”.



Python runtime(runtime.txt)

Since sap_cf_logging libraries supports python 2.7 and 3.5, I used python 3.5(not 3.6).


Used libraries(requirements.txt)

I only used Flask and sap_cf_logging.


Flask application(

The application code is like this.

from flask import Flask, request
from sap.cf_logging import flask_logging
import os, logging

app = Flask(__name__)

#Iitialize logging
flask_logging.init(app, logging.INFO)

cf_port = os.getenv('PORT')

# Only get method by default
def hello():
    logger = logging.getLogger('my.logger')
    return 'Hello World'

if __name__ == '__main__':
    if cf_port is None:'', port=5000, debug=True)
    else:'', port=int(cf_port), debug=True)

Deploy and Monitor Log


There is nothing special about deployment.  Just push the application.

cf push <application name>

Monitor logs

See logs using cf CLI.

cf logs <application name>

When you want to see log history, use option “–recent”.

cf logs <application name> --recent

This is the log, after accessing the application via browser.

You can see http request information as JSON format in the red frame.  There are many helpful information as timestamp, line no and so on.

	"msg": "<Request '' [GET]>",
	"line_no": 16,
	"layer": "python",
	"written_ts": 1544603923048154000,
	"correlation_id": "74f6ee3d-433d-4797-467d-cae1f053812b",
	"component_name": "sample-logging",
	"space_name": "dev",
	"logger": "my.logger",
	"container_id": "",
	"thread": "Thread-12",
	"written_at": "2018-12-12T08:38:43.048Z",
	"level": "INFO",
	"component_instance": "0",
	"type": "log",
	"space_id": "2741076c-c551-4b62-8ebc-c1328bbc08d7",
	"component_id": "1df97d1e-6386-48b7-9035-3bab082edffc",
	"component_type": "application"

Python “print” command can also leave log.  But it only leaves “<Request ‘’ [GET]>” here, since “request” is object type variable.


WIth sap_cf_logging library you will get much benefit while development.  Have fun!

Assigned Tags

      You must be Logged on to comment or reply to a post.
      Author's profile photo Marcus Schiffer
      Marcus Schiffer



      when I use this with websockets e.g.:


      def handle_json_event(message,methods=['GET', 'POST']):

      flask_logging.init(app, logging.INFO)'Custom Event Called !')


      I get an error:

      "RuntimeError: Working outside of application context.This typically means that you attempted to use functionality that needed to interface with the current application object in some way. To solve this, set up an application context with app.app_context(). "

      Any help is appreciated.

      Author's profile photo Yohei Fukuhara
      Yohei Fukuhara
      Blog Post Author



      I'm sorry that I don't know how to deal with.




      Author's profile photo Marcus Schiffer
      Marcus Schiffer



      just in case someone has the same issue:

      i solved it by adding

      import gevent.monkey

      on top of my python file.