Added logging to application

email
androiddrew 7 years ago
parent e521e453a3
commit 0425508561

@ -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)

@ -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()

@ -10,6 +10,10 @@ settings = {
'RENDERERS': [JSONRenderer()],
'JWT': {
'SECRET': 'thisisasecret'
},
'LOGGING': {
"LOG_FILE": 'mylogfile.log',
"LEVEL": "DEBUG"
}
}

Loading…
Cancel
Save