Technical Articles
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”.
Application
Python runtime(runtime.txt)
Since sap_cf_logging libraries supports python 2.7 and 3.5, I used python 3.5(not 3.6).
python-3.5.5
Used libraries(requirements.txt)
I only used Flask and sap_cf_logging.
Flask
sap_cf_logging
Flask application(sample-logging.py)
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
@app.route('/')
def hello():
logger = logging.getLogger('my.logger')
logger.info(request)
print(request)
return 'Hello World'
if __name__ == '__main__':
if cf_port is None:
app.run(host='0.0.0.0', port=5000, debug=True)
else:
app.run(host='0.0.0.0', port=int(cf_port), debug=True)
Deploy and Monitor Log
Deployment
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 'http://sample-logging-proud-impala.cfapps.eu10.hana.ondemand.com/' [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": "10.0.137.233",
"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 ‘http://sample-logging-proud-impala.cfapps.eu10.hana.ondemand.com/’ [GET]>” here, since “request” is object type variable.
Lastly
WIth sap_cf_logging library you will get much benefit while development. Have fun!
Hi,
when I use this with websockets e.g.:
@socketio.on('connectflask')
def handle_json_event(message,methods=['GET', 'POST']):
flask_logging.init(app, logging.INFO)
logger.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.
Hi,
I'm sorry that I don't know how to deal with.
Regards,
Yohei
Hi,
just in case someone has the same issue:
i solved it by adding
import gevent.monkey
gevent.monkey.patch_all()
on top of my python file.