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:
@@ -1,5 +1,7 @@
|
|||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
import logging
|
||||||
|
import sys
|
||||||
import imp
|
import imp
|
||||||
|
|
||||||
from flask import Flask
|
from flask import Flask
|
||||||
@@ -23,10 +25,26 @@ def f():
|
|||||||
if userfunc == None:
|
if userfunc == None:
|
||||||
print("Generic container: no requests supported")
|
print("Generic container: no requests supported")
|
||||||
abort(500)
|
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
|
# TODO: this starts the built-in server, which isn't the most
|
||||||
# efficient. We should use something better.
|
# efficient. We should use something better.
|
||||||
#
|
#
|
||||||
|
setup_logger(logging.DEBUG)
|
||||||
|
app.logger.info("Starting server")
|
||||||
app.run(host='0.0.0.0', port='8888')
|
app.run(host='0.0.0.0', port='8888')
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
from flask import request
|
from flask import request
|
||||||
|
from flask import current_app
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
msg = "%s %s:\n---HEADERS---\n%s\n--BODY--\n%s\n-----\n" % (request.method, request.path, request.headers, request.get_data())
|
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
|
return msg
|
||||||
|
|||||||
Reference in New Issue
Block a user