From 04255085614a7af4759831555b307f0922bb488d Mon Sep 17 00:00:00 2001 From: androiddrew Date: Tue, 14 Nov 2017 19:17:50 -0500 Subject: [PATCH] Added logging to application --- cookie_api/app.py | 18 ++++++++++++++++-- cookie_api/logging.py | 16 ++++++++++++++++ wsgi.py | 4 ++++ 3 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 cookie_api/logging.py diff --git a/cookie_api/app.py b/cookie_api/app.py index be1d43c..86252e6 100644 --- a/cookie_api/app.py +++ b/cookie_api/app.py @@ -5,14 +5,18 @@ from apistar.frameworks.wsgi import WSGIApp as App from apistar.handlers import docs_urls, static_urls from apistar.interfaces import Router, Injector, Auth from apistar_jwt.authentication import JWTAuthentication +import logbook from cookie_api.auth import auth_routes, auth_components from cookie_api.commands import commands from cookie_api.models import Cookie from cookie_api.schema import CookieSchema +from cookie_api import logging cookie_schema = CookieSchema() +logger = logbook.Logger('Cookies') + @annotate(authentication=[JWTAuthentication()]) def get_state(injector: Injector, auth: Auth): state = injector.state @@ -23,6 +27,7 @@ def get_state(injector: Injector, auth: Auth): def get_cookies(session: Session): + logger.info("Accessing the Cookies resource") cookies = session.query(Cookie).all() return cookie_schema.dump(cookies, many=True).data @@ -30,6 +35,7 @@ def get_cookies(session: Session): def get_cookie(session: Session, id): cookie = session.query(Cookie).filter_by(id=id).one_or_none() if cookie is None: + logger.warn("Someone keeps requesting bad cookie locations") msg = {"error": "404 Not Found"} return http.Response(msg, status=404) return cookie_schema.dump(cookie).data @@ -67,7 +73,11 @@ routes = [ Include('/static', static_urls) ] -app_settings = {} +app_settings = { + "LOGGING": { + "LEVEL": "DEBUG" + } +} routes = routes + auth_routes @@ -78,7 +88,11 @@ components = sqlalchemy_backend.components + auth_components def application_factory(settings={}): """Returns an instance of Cookie API""" - return App(settings={**app_settings, **settings}, + _settings = {**app_settings, **settings} + + logging.global_init(_settings) + + return App(settings=_settings, commands=commands, components=components, routes=routes) diff --git a/cookie_api/logging.py b/cookie_api/logging.py new file mode 100644 index 0000000..2da5848 --- /dev/null +++ b/cookie_api/logging.py @@ -0,0 +1,16 @@ +import sys +import logbook + + +def global_init(settings={}): + _logging_setting = settings.get("LOGGING", {"LEVEL": logbook.TRACE}) + + _log_file = _logging_setting.get("LOG_FILE") + _level = _logging_setting.get("LEVEL") + + if _log_file is not None: + logbook.TimedRotatingFileHandler(_log_file, level=_level).push_application() + else: + logbook.StreamHandler(sys.stdout, level=_level).push_application() + + diff --git a/wsgi.py b/wsgi.py index 0110410..32014bf 100644 --- a/wsgi.py +++ b/wsgi.py @@ -10,6 +10,10 @@ settings = { 'RENDERERS': [JSONRenderer()], 'JWT': { 'SECRET': 'thisisasecret' + }, + 'LOGGING': { + "LOG_FILE": 'mylogfile.log', + "LEVEL": "DEBUG" } }