Let gunicorn handle logging

This commit is contained in:
Joel Collins 2019-03-19 12:46:16 +00:00
parent 1bd3d0dacd
commit d445b359e9

View file

@ -6,6 +6,8 @@ TODO: Implement API route to cleanly shut down server
from flask import ( from flask import (
Flask, render_template) Flask, render_template)
from flask.logging import default_handler
from flask_cors import CORS from flask_cors import CORS
from openflexure_microscope.api.exceptions import JSONExceptionHandler from openflexure_microscope.api.exceptions import JSONExceptionHandler
@ -15,14 +17,30 @@ from openflexure_microscope.camera.pi import StreamingCamera
from openflexure_microscope.stage.openflexure import Stage from openflexure_microscope.stage.openflexure import Stage
import atexit import atexit
import logging, sys import logging
import sys, os
logging.basicConfig(stream=sys.stderr, level=logging.DEBUG) # Handle logging
is_gunicorn = "gunicorn" in os.environ.get("SERVER_SOFTWARE", "")
if (__name__ == "__main__") or (not is_gunicorn):
# If imported, but not by gunicorn
print("Letting sys handle logs")
logging.basicConfig(stream=sys.stderr, level=logging.DEBUG)
else:
print("Letting gunicorn handle logs")
# If running in gunicorn, let gunicorn handle logging
root = logging.getLogger()
gunicorn_logger = logging.getLogger('gunicorn.error')
for handler in gunicorn_logger.handlers:
root.addHandler(handler)
root.setLevel(gunicorn_logger.level)
# Create a dummy microscope object, with no hardware attachments # Create a dummy microscope object, with no hardware attachments
api_microscope = Microscope(None, None) api_microscope = Microscope(None, None)
# Generate API URI based on version from filename # Generate API URI based on version from filename
def uri(suffix, api_version, base=None): def uri(suffix, api_version, base=None):
if not base: if not base:
@ -78,7 +96,6 @@ def index():
##### API ROUTES ###### ##### API ROUTES ######
from openflexure_microscope.api.v1 import blueprints from openflexure_microscope.api.v1 import blueprints
logging.debug("Registering blueprints...")
# Base routes # Base routes
base_blueprint = blueprints.base.construct_blueprint(api_microscope) base_blueprint = blueprints.base.construct_blueprint(api_microscope)
app.register_blueprint(base_blueprint, url_prefix=uri('', 'v1')) app.register_blueprint(base_blueprint, url_prefix=uri('', 'v1'))
@ -121,4 +138,4 @@ atexit.register(cleanup)
if __name__ == "__main__": if __name__ == "__main__":
app.run(host='0.0.0.0', port="5000", threaded=True, debug=True, use_reloader=False) app.run(host='0.0.0.0', port="5000", threaded=True, debug=True, use_reloader=False)