From 5f02f6c8ca17cac10208a0bdee8e3ef1f3f9008a Mon Sep 17 00:00:00 2001 From: Soam Vasani Date: Thu, 8 Dec 2016 14:03:27 -0800 Subject: [PATCH 1/2] Setup app logger in python environment The user's functions can now use `current_app.logger`, and use `kubectl logs` to see the logs that the function outputs. --- environments/python3/server.py | 20 +++++++++++++++++++- examples/python/requestdata.py | 3 +++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/environments/python3/server.py b/environments/python3/server.py index 4faca621..e2cd798c 100644 --- a/environments/python3/server.py +++ b/environments/python3/server.py @@ -1,5 +1,7 @@ #!/usr/bin/env python +import logging +import sys import imp from flask import Flask @@ -23,10 +25,26 @@ def f(): if userfunc == None: print("Generic container: no requests supported") abort(500) - return userfunc() + return userfunc() + +# +# Logging setup. TODO: Loglevel hard-coded for now. We could allow +# functions/routes to override this somehow; or we could create +# separate dev vs. prod environments. +# +def setup_logger(loglevel): + global app + root = logging.getLogger() + root.setLevel(loglevel) + ch = logging.StreamHandler(sys.stdout) + ch.setLevel(loglevel) + ch.setFormatter(logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')) + app.logger.addHandler(ch) # # TODO: this starts the built-in server, which isn't the most # efficient. We should use something better. # +setup_logger(logging.DEBUG) +app.logger.info("Starting server") app.run(host='0.0.0.0', port='8888') diff --git a/examples/python/requestdata.py b/examples/python/requestdata.py index 07673f85..5d368331 100644 --- a/examples/python/requestdata.py +++ b/examples/python/requestdata.py @@ -1,5 +1,8 @@ from flask import request +from flask import current_app def main(): msg = "%s %s:\n---HEADERS---\n%s\n--BODY--\n%s\n-----\n" % (request.method, request.path, request.headers, request.get_data()) + current_app.logger.info(msg) + return msg From 4a6a088eca8710d33653126186f3ed7735195d49 Mon Sep 17 00:00:00 2001 From: Soam Vasani Date: Thu, 8 Dec 2016 14:13:01 -0800 Subject: [PATCH 2/2] Modify example a bit --- examples/python/requestdata.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/examples/python/requestdata.py b/examples/python/requestdata.py index 5d368331..93f725a5 100644 --- a/examples/python/requestdata.py +++ b/examples/python/requestdata.py @@ -2,7 +2,6 @@ from flask import request from flask import current_app def main(): - msg = "%s %s:\n---HEADERS---\n%s\n--BODY--\n%s\n-----\n" % (request.method, request.path, request.headers, request.get_data()) - current_app.logger.info(msg) - + current_app.logger.info("Received request") + msg = "---HEADERS---\n%s\n--BODY--\n%s\n-----\n" % (request.headers, request.get_data()) return msg