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.
This commit is contained in:
Soam Vasani
2016-12-08 14:03:27 -08:00
parent ab96ed2917
commit 5f02f6c8ca
2 changed files with 22 additions and 1 deletions
+19 -1
View File
@@ -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')
+3
View File
@@ -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