diff --git a/app/__init__.py b/app/__init__.py index d80733498d7c9c4d48b0c140ca9833259629fb20..9d6a2f9f85fa94497235775b7835ecfb32f5a70b 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -1,10 +1,27 @@ +import logging +from logging.handlers import RotatingFileHandler from flask import Flask +MEGABYTE = 10**6 +LOG_FORMAT = '[%(asctime)s] %(pathname)s:%(lineno)d %(levelname)s - %(message)s' +LOG_PATH = '/var/log/flask.log' + def create_app(config): """Set up the application.""" app = Flask(__name__) app.config.from_pyfile(config) + if config != 'config/local.py': + handler = RotatingFileHandler(LOG_PATH, maxBytes=MEGABYTE, backupCount=1) + + handler.setLevel(logging.INFO) + handler.setFormatter(logging.Formatter(LOG_FORMAT, datefmt='%Y-%m-%d %H:%M:%S')) + + app.logger.addHandler(handler) + app.logger.setLevel(logging.INFO) + + app.logger.info('Setting up application...') + from .lib import database database.register(app) diff --git a/app/permissions/auth.py b/app/permissions/auth.py index d6bbeafd89fb71dbf99e6e0e434b88499a19d1fd..360b77c73edf15cd252939a3011a383e72c7e60d 100644 --- a/app/permissions/auth.py +++ b/app/permissions/auth.py @@ -1,4 +1,5 @@ """Permissions to check authentication.""" +from flask import current_app from werkzeug.exceptions import Unauthorized from jose import jwt import json @@ -51,13 +52,13 @@ class AuthNeed(Permission): # For now we will print and return unauthorized. In the future # we will log these errors and the requester except jwt.ExpiredSignatureError: - print('Token is expired') + current_app.logger.info('Token is expired') return False except jwt.JWTClaimsError: - print('Incorrect claims. Please check the audience and the issuer') + current_app.logger.info('Incorrect claims. Please check the audience and the issuer') return False except Exception: - print('Invalid header. Unable to parse the token') + current_app.logger.info('Invalid header. Unable to parse the token') return False # Check permissions