Ejemplo n.º 1
0
def register_logging(app):
    # 文件日志
    formatter = logging.Formatter(
        '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
    file_handler = RotatingFileHandler(filename='./logs/flaskdemo.log',
                                       maxBytes=10 * 1024 * 1024,
                                       backupCount=10)
    file_handler.setFormatter(formatter)
    file_handler.setLevel(logging.INFO)
    app.logger.setLevel(logging.INFO)

    default_handler.setLevel(logging.INFO)

    class RequestFormatter(logging.Formatter):
        def format(self, record) -> str:
            record.url = request.url
            record.remote_addr = request.remote_addr
            return super(RequestFormatter, self).format(record)

    request_formatter = RequestFormatter(
        '[%(asctime)s] %(remote_addr)s requested %(url)s\n%(levelname)s in %(module)s: %(message)s'
    )
    mail_handler = SMTPHandler(mailhost=app.config['MAIL_SERVER'],
                               fromaddr=app.config['MAIL_USERNAME'],
                               toaddrs=app.config['ALBUMY_ADMIN_EMAIL'],
                               subject='Application Error',
                               credentials=(app.config['MAIL_USERNAME'],
                                            app.config['MAIL_PASSWORD']))
    mail_handler.setLevel(logging.ERROR)
    mail_handler.setFormatter(request_formatter)
    if not app.debug:
        app.logger.addHandler(file_handler)
        app.logger.addHandler(default_handler)
        app.logger.addHandler(mail_handler)
Ejemplo n.º 2
0
def register_logging(app):
    class RequestFormatter(logging.Formatter):
        def format(self, record):
            record.url = request.url
            record.remote_addr = request.remote_addr
            return super(RequestFormatter, self).format(record)

    request_formatter = RequestFormatter(
        '[%(asctime)s] %(remote_addr)s requested %(url)s\n'
        '%(levelname)s in %(module)s: %(message)s')
    default_handler.setLevel(logging.DEBUG)
    formatter = logging.Formatter(
        '%(asctime)s - %(name)s - %(levelname)s - %(message)s')

    file_handler = RotatingFileHandler(os.path.join(basedir,
                                                    'logs/bluelog.log'),
                                       maxBytes=10 * 1024 * 1024,
                                       backupCount=10)
    file_handler.setFormatter(formatter)
    file_handler.setLevel(logging.DEBUG)
    default_handler.setFormatter(formatter)
    mail_handler = SMTPHandler(mailhost=app.config['MAIL_SERVER'],
                               fromaddr=app.config['MAIL_USERNAME'],
                               toaddrs=['ADMIN_EMAIL'],
                               subject='Bluelog Application Error',
                               credentials=(app.config['MAIL_USERNAME'],
                                            app.config['MAIL_PASSWORD']))
    mail_handler.setLevel(logging.ERROR)
    mail_handler.setFormatter(request_formatter)

    if not app.debug:
        app.logger.addHandler(mail_handler)
        app.logger.addHandler(file_handler)
        app.logger.addHandler(default_handler)
Ejemplo n.º 3
0
def create_app():
    app = Flask(__name__, instance_path='/opt/flaskapp-instance')

    # Pull in app settings
    app.config.from_object(settings)

    # Configure logging
    default_handler.setLevel(
        logging.DEBUG if app.debug else app.config['PROD_LOG_LEVEL'])

    app.logger.setLevel(logging.DEBUG)

    # Apply Werkzeug middleware so Flask can correctly deduce
    # the client request environment when behind a proxy
    if (app.config['UPSTREAM_PROXY_COUNT'] > 0):
        app.wsgi_app = ProxyFix(app.wsgi_app,
                                app.config['UPSTREAM_PROXY_COUNT'])

    # Setup the Flask-JWT-Extended extension
    if app.debug:
        jwt_pub_key_file = '.jwt/dummy/pubkey.pem'
    else:
        jwt_pub_key_file = '.jwt/pubkey.pem'

    with app.open_instance_resource(jwt_pub_key_file) as f:
        app.config['JWT_PUBLIC_KEY'] = f.read()

    JWTManager(app)

    # Allow cross origin requests on all endpoints (they will
    # still need to present a valid JWT to gain access)
    CORS(app)

    # Import views
    from flaskapp.views import auth
    from flaskapp.views.api_v1 import manage
    from flaskapp.views.api_v1 import devices
    from flaskapp.views.api_v1 import station
    from flaskapp.views.api_v1 import home

    # Register blueprints
    app.register_blueprint(auth.bp, url_prefix='/auth')
    app.register_blueprint(manage.bp, url_prefix='/api/v1/manage')
    app.register_blueprint(devices.bp, url_prefix='/api/v1/devices')
    app.register_blueprint(station.bp, url_prefix='/api/v1/station')
    app.register_blueprint(home.bp, url_prefix='/api/v1/home')

    # Register app level error handlers
    app.register_error_handler(HTTPException, app_exception_handler)

    # Set final actions on outgoing responses
    @app.after_request
    def perform_exit_actions(response):
        response.headers.add('X-Content-Type-Options', 'nosniff')
        response.headers.add('X-XSS-Protection', '1; mode=block')

        return response

    return app
Ejemplo n.º 4
0
def create_app():
    select_default_settings()

    app = Flask(__name__)
    app.config.from_object(os.environ.get('MINIFICTION_SETTINGS'))

    default_handler.setLevel(app.config['LOGLEVEL'])
    logging.basicConfig(level=app.config['LOGLEVEL'],
                        format=app.config['LOGFORMAT'])

    app.static_folder = app.config['STATIC_ROOT']
    app.extra_css = []
    app.extra_js = []

    app.session_interface = LazySecureCookieSessionInterface()

    if app.config['UMASK'] is not None:
        if isinstance(app.config['UMASK'], str):
            app.config['UMASK'] = int(app.config['UMASK'], 8)
        os.umask(app.config['UMASK'])

    if app.config['TESTING']:
        if not os.path.isdir(app.config['TESTING_DIRECTORY']):
            os.makedirs(app.config['TESTING_DIRECTORY'])
        elif os.listdir(app.config['TESTING_DIRECTORY']):
            raise RuntimeError('Testing directory %r is not empty' %
                               app.config['TESTING_DIRECTORY'])

    init_bl()

    configure_user_agent(app)
    configure_i18n(app)
    configure_cache(app)
    configure_forms(app)
    configure_users(app)
    configure_error_handlers(app)
    configure_views(app)
    configure_admin_views(app)
    configure_ajax(app)
    configure_errorpages(app)
    configure_templates(app)
    if not app.config['SPHINX_DISABLED']:
        configure_search(app)
    configure_celery(app)
    configure_captcha(app)
    configure_story_voting(app)
    configure_misc(app)
    configure_development(app)
    configure_frontend(app)
    configure_sidebar(app)

    app.context_processor(templates_context)

    init_plugins(app)
    database.configure_for_app(app)

    return app
Ejemplo n.º 5
0
def create_app(config_name):
    app = Flask(__name__)

    # Config
    app.config.from_object(config[config_name])
    config[config_name].init_app(app)

    # Extensions
    db.init_app(app)
    auth.init_app(app=app)

    # Middleware / Wrappers
    if app.config['APP_ENABLE_PROXY_FIX']:
        ReverseProxyPrefixFix(app)
    if app.config['APP_ENABLE_REQUEST_ID']:
        RequestID(app)
    if app.config['APP_ENABLE_SENTRY']:
        sentry_sdk.init(**app.config['SENTRY_CONFIG'])

    # Logging
    formatter = RequestFormatter(
        '[%(asctime)s] [%(levelname)s] [%(request_id)s] [%(url)s]: %(message)s'
    )
    default_handler.setFormatter(formatter)
    default_handler.setLevel(app.config['LOGGING_LEVEL'])

    # Error handlers
    app.register_error_handler(BadRequest, error_handler_generic_bad_request)
    app.register_error_handler(NotFound, error_handler_generic_not_found)
    app.register_error_handler(UnprocessableEntity,
                               error_handler_generic_unprocessable_entity)
    app.register_error_handler(InternalServerError,
                               error_handler_generic_internal_server_error)

    # CLI commands
    app.cli.add_command(seeding_cli_group)
    app.cli.add_command(importing_cli_group)

    # Routes
    app.add_url_rule('/', 'index', index_route)
    app.add_url_rule('/meta/health/canary',
                     'canary_health_check',
                     healthcheck_canary_route,
                     methods=['get', 'options'])

    # Resource blueprints
    app.register_blueprint(projects_blueprint)
    app.register_blueprint(people_blueprint)
    app.register_blueprint(grants_blueprint)
    app.register_blueprint(organisations_blueprint)
    app.register_blueprint(category_schemes_blueprint)
    app.register_blueprint(category_terms_blueprint)
    app.register_blueprint(participants_blueprint)
    app.register_blueprint(allocations_blueprint)
    app.register_blueprint(categorisations_blueprint)

    return app
Ejemplo n.º 6
0
def bootstrap():
    del app.logger.handlers[:]
    logger = logging.getLogger()
    logger.setLevel(logging.INFO)
    default_handler.setLevel(logging.INFO)
    logger.addHandler(default_handler)

    global prediction_service
    prediction_service = get_prediction_service(
        app.config['PREDICTION_SERVICE'])
Ejemplo n.º 7
0
def create_app():
    select_default_settings()

    app = Flask(__name__)
    app.config.from_object(os.environ.get('MINIFICTION_SETTINGS'))

    default_handler.setLevel(app.config['LOGLEVEL'])
    logging.basicConfig(level=app.config['LOGLEVEL'], format=app.config['LOGFORMAT'])

    app.static_folder = app.config['STATIC_ROOT']
    app.extra_css = []
    app.extra_js = []

    if app.config['UMASK'] is not None:
        if isinstance(app.config['UMASK'], str):
            app.config['UMASK'] = int(app.config['UMASK'], 8)
        os.umask(app.config['UMASK'])

    if app.config['TESTING']:
        if not os.path.isdir(app.config['TESTING_DIRECTORY']):
            os.makedirs(app.config['TESTING_DIRECTORY'])
        elif os.listdir(app.config['TESTING_DIRECTORY']):
            raise RuntimeError('Testing directory %r is not empty' % app.config['TESTING_DIRECTORY'])

    init_bl()

    configure_user_agent(app)
    configure_i18n(app)
    configure_cache(app)
    configure_forms(app)
    configure_users(app)
    configure_error_handlers(app)
    configure_views(app)
    configure_admin_views(app)
    configure_ajax(app)
    configure_errorpages(app)
    configure_templates(app)
    if not app.config['SPHINX_DISABLED']:
        configure_search(app)
    configure_celery(app)
    configure_captcha(app)
    configure_story_voting(app)
    configure_misc(app)
    configure_development(app)
    configure_frontend(app)
    configure_sidebar(app)

    app.context_processor(templates_context)

    init_plugins(app)
    database.configure_for_app(app)

    return app
Ejemplo n.º 8
0
def register_logging(app):
    app.logger.setLevel(logging.INFO)

    formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
    file_handler = RotatingFileHandler('logs/log.txt', maxBytes=10 * 1024 * 1024, backupCount=10)
    file_handler.setFormatter(formatter)
    file_handler.setLevel(logging.INFO)
    default_handler.setLevel(logging.INFO)
    # 在调试状态下不会添加处理器
    if not app.debug:
        app.logger.addHandler(file_handler)
        app.logger.addHandler(default_handler)
Ejemplo n.º 9
0
def init_logger():
    """
    Utility function to initialise the logger.

    Use this to set up logging for your server.
    This should be called just after the app object is intantiated.
    """
    formatter = RequestFormatter(
        "[%(asctime)s] %(remote_addr)s requested %(url)s\n"
        "%(levelname)s in %(module)s: %(message)s")
    default_handler.setLevel(logging.INFO)
    default_handler.setFormatter(formatter)
Ejemplo n.º 10
0
    def set_log_level(self, log_level):
        r"""Set the logging level.

        Args:
            log_level (int): Logging level.

        """
        super(FlaskService, self).set_log_level(log_level)
        from flask.logging import default_handler
        werkzeug_logger = logging.getLogger('werkzeug')
        default_handler.setLevel(level=log_level)
        self.app.logger.setLevel(level=log_level)
        werkzeug_logger.setLevel(level=log_level)
Ejemplo n.º 11
0
def register_logger(app: Flask):
    app.logger.setLevel(logging.INFO)

    formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s "
                                  "%(message)s")
    if app.debug:
        file_handler = RotatingFileHandler(filename="logs/lsfd202201.log",
                                           maxBytes=10 * 1024 * 1024,
                                           backupCount=10)
        file_handler.setFormatter(formatter)
        file_handler.setLevel(logging.INFO)
        app.logger.addHandler(file_handler)
    if not app.debug:
        default_handler.setLevel(logging.INFO)
        app.logger.addHandler(default_handler)
Ejemplo n.º 12
0
def init_app(app):
    root = logging.getLogger()
    if app.config.get('LOGGING_HANDLER') == 'syslog':
        from logging.handlers import SysLogHandler
        syslog_handler = SysLogHandler(
            app.config.get('LOGGING_ADDRESS', '/dev/log'))
        syslog_handler.setFormatter(
            logging.Formatter(
                app.config.get(
                    'LOGGING_FORMAT',
                    '[%(asctime)s] %(levelname)s in %(module)s: %(message)s')))
        app.logger.addHandler(syslog_handler)
        root.addHandler(syslog_handler)
    if 'LOGGING_LEVEL' in app.config:
        default_handler.setLevel(app.config['LOGGING_LEVEL'])
        app.logger.setLevel(app.config['LOGGING_LEVEL'])
        root.setLevel(app.config['LOGGING_LEVEL'])
Ejemplo n.º 13
0
def register_logging(app):

    formatter = logging.Formatter(
        '%(asctime)s - %(name)s - %(levelname)s - %(message)s')

    file_handler = RotatingFileHandler(os.path.join(basedir,
                                                    'logs/myblog.log'),
                                       maxBytes=10 * 1024 * 1024,
                                       backupCount=10)
    file_handler.setFormatter(formatter)
    file_handler.setLevel(logging.INFO)

    default_handler.setLevel(logging.INFO)

    if not app.debug:
        app.logger.addHandler(file_handler)
        app.logger.addHandler(default_handler)
Ejemplo n.º 14
0
def configure_logging(app):
    """Configure file(info) and email(error) logging."""

    log_format = app.config.get(
        "LOGGING_FORMAT",
        "%(asctime)s %(levelname)s: %(message)s [in %(pathname)s:%(lineno)d]",
    )
    date_format = "%Y-%m-%dT%H:%M:%SZ"
    log_levels = {
        "CRITICAL": logging.CRITICAL,
        "ERROR": logging.ERROR,
        "WARNING": logging.WARNING,
        "INFO": logging.INFO,
        "DEBUG": logging.DEBUG,
        "NOTSET": logging.NOTSET,
    }

    log_formatter = logging.Formatter(log_format, date_format)
    log_level = log_levels.get(app.config["LOGGING_LEVEL"], log_levels["INFO"])

    if "LOGGING_LOCATION" in app.config:

        log_file_name = app.config["LOGGING_LOCATION"]
        parent_dir = os.path.abspath(os.path.join(log_file_name, os.pardir))

        if not os.path.exists(parent_dir):
            os.makedirs(parent_dir)

        log_handler = RotatingFileHandler(filename=log_file_name,
                                          maxBytes=10000,
                                          backupCount=5)
        log_handler.setLevel(log_level)
        log_handler.setFormatter(log_formatter)
        app.logger.addHandler(log_handler)

        app.logger.debug("Logging initialized...")
        app.logger.debug("... log file location: {}".format(
            app.config.get("LOGGING_LOCATION")))
    else:
        default_handler.setLevel(log_level)
        default_handler.setFormatter(log_formatter)
        log_handler = logging.basicConfig(stream=sys.stdout)
        app.logger.debug("Logging initialized...")
        app.logger.debug("... default flask logging handler")
def create_app(config_name):
    app = Flask(__name__)

    # Config
    app.config.from_object(config[config_name])
    config[config_name].init_app(app)

    # Middleware / Wrappers
    if app.config['APP_ENABLE_SENTRY']:
        sentry_sdk.init(**app.config['SENTRY_CONFIG'])
    if app.config['APP_ENABLE_CORS']:
        CORS(app, **app.config['CORS_CONFIG'])
    if app.config['APP_ENABLE_REQUEST_ID']:
        RequestID(app)

    # Logging
    class RequestFormatter(logging.Formatter):
        def format(self, record):
            record.url = 'NA'
            record.request_id = 'NA'

            if has_request_context():
                record.url = request.url
                if app.config['APP_ENABLE_REQUEST_ID']:
                    record.request_id = request.environ.get("HTTP_X_REQUEST_ID")

            return super().format(record)
    formatter = RequestFormatter(
        '[%(asctime)s] [%(levelname)s] [%(request_id)s] [%(url)s] %(module)s: %(message)s'
    )
    default_handler.setFormatter(formatter)
    default_handler.setLevel(app.config['LOGGING_LEVEL'])

    # Error handlers
    app.register_error_handler(400, error_handler_generic_bad_request)
    app.register_error_handler(404, error_handler_generic_not_found)
    app.register_error_handler(413, error_handler_request_entity_too_large)
    app.register_error_handler(500, error_handler_generic_internal_server_error)

    # App
    app.register_blueprint(meta_blueprint)
    app.register_blueprint(main_blueprint)

    return app
Ejemplo n.º 16
0
 def init_app(cls, app: Flask) -> None:
     """初始化flask应用对象"""
     log_level = getenv('LOG_LEVEL') or 'INFO'
     app.logger.setLevel(log_level)
     default_handler.setLevel(log_level)
     default_handler.setFormatter(
         Formatter(
             '[%(asctime)s] %(pathname)s:%(lineno)d [%(levelname)s] %(message)s'
         ))
     app.before_request(before_app_request)
     app.teardown_request(teardown_app_request)
     app.json_encoder = CustomJSONEncoder
     sub, url = cls.BP_SUB_DOMAIN, cls.BP_URL_PREFIX
     app.register_blueprint(bp_admin_api,
                            subdomain=sub.get('admin'),
                            url_prefix=url.get('admin_api'))
     app.register_blueprint(bp_admin_ext,
                            subdomain=sub.get('admin'),
                            url_prefix=url.get('admin_ext'))
Ejemplo n.º 17
0
def register_logger(app: Flask):
    app.logger.setLevel(logging.DEBUG)
    if not (app.testing or app.debug):
        app.logger.setLevel(logging.INFO)
    formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s "
                                  "%(message)s")
    if app.debug or app.testing:
        try:
            os.mkdir("logs")
        except:  # noqa: E722
            pass
        file_handler = RotatingFileHandler(filename="logs/flog.log",
                                           maxBytes=10 * 1024 * 1024,
                                           backupCount=10)
        file_handler.setFormatter(formatter)
        file_handler.setLevel(logging.INFO)
        app.logger.addHandler(file_handler)
    else:
        default_handler.setLevel(logging.INFO)
        app.logger.addHandler(default_handler)
Ejemplo n.º 18
0
def initialize_logger():
    """Set up logger format and level"""
    formatter = logging.Formatter(
        '[%(asctime)s]:[%(levelname)s]:[%(name)s]: %(message)s')

    default_handler.setFormatter(formatter)
    default_handler.setLevel(logging.DEBUG)

    wsgi_handler = logging.StreamHandler(
        stream='ext://flask.logging.wsgi_errors_stream')
    wsgi_handler.setFormatter(formatter)
    wsgi_handler.setLevel(logging.DEBUG)

    logger = logging.getLogger()
    logger.setLevel(logging.DEBUG)
    logger.addHandler(default_handler)

    # 3rd party loggers
    logging.getLogger('botocore').setLevel(logging.INFO)
    logging.getLogger('urllib3').setLevel(logging.INFO)
Ejemplo n.º 19
0
def main():
    global runner
    global app

    logger = logging.getLogger()
    default_handler.setFormatter(logging.Formatter(
        '[%(asctime)s] %(levelname)s in %(module)s: %(message)s'
    ))
    default_handler.setLevel(logging.DEBUG)
    logger.addHandler(default_handler)

    logging.info(HEAD)

    config = load_env()

    runner = Runner(config)
    runner.start()

    app.run(debug=True, use_reloader=False)

    runner.stop()
Ejemplo n.º 20
0
def register_logging(app):
    logging.getLogger(__name__)
    app.logger.name = app.name
    log_dir_name = "logs"
    log_file_name = app.name + '-' + time.strftime(
        '%Y-%m-%d', time.localtime(time.time())) + '.log'
    log_file_folder = os.path.abspath(
        os.path.dirname(__file__)) + os.sep + log_dir_name
    make_dir(log_file_folder)

    log_file_str = log_file_folder + os.sep + log_file_name
    logging_format = logging.Formatter(
        '[%(asctime)s][%(levelname)s][%(filename)s][%(funcName)s: %(lineno)s] - %(message)s'
    )
    timeRotatingHandler = logging.handlers.TimedRotatingFileHandler(
        log_file_str, when="D", interval=1, backupCount=10)
    timeRotatingHandler.setFormatter(logging_format)
    timeRotatingHandler.setLevel(logging.DEBUG)
    app.logger.addHandler(timeRotatingHandler)

    default_handler.setFormatter(logging_format)
    default_handler.setLevel(logging.DEBUG)
Ejemplo n.º 21
0
def register_logging(app):
    class RequestFormatter(logging.Formatter):
        def format(self, record):
            record.url = request.url
            record.remote_addr = request.remote_addr
            return super(RequestFormatter, self).format(record)

    formatter = logging.Formatter(
        '%(asctime)s - %(name)s - %(levelname)s - %(message)s')

    basedir = os.path.abspath(os.path.dirname(os.path.dirname(__file__)))

    file_handler = RotatingFileHandler(os.path.join(basedir,
                                                    'app/logs/wuhan.log'),
                                       maxBytes=10 * 1024 * 1024,
                                       backupCount=10)
    file_handler.setFormatter(formatter)
    file_handler.setLevel(logging.INFO)
    default_handler.setLevel(logging.INFO)

    if not app.debug:
        app.logger.addHandler(file_handler)
        app.logger.addHandler(default_handler)
Ejemplo n.º 22
0
    db = client[mongoconnection.db]
    db.authenticate(mongoconnection.user, password=mongoconnection.passwd)
    col = db[mongoconnection.col]
    expres = col.find_one({'_id': exp})

    data = np.array(expres['result'])

    img = StringIO.StringIO()
    fig = plt.figure(figsize=(16, 10), dpi=100)

    axes = fig.add_subplot(1, 1, 1)

    axes.plot(data[:, 0], data[:, 1], color='r')
    axes.plot(data[:, 0], data[:, 2], color='r', linestyle='--')
    axes.plot(data[:, 0], data[:, 3], color='g')
    axes.plot(data[:, 0], data[:, 4], color='g', linestyle='--')

    plt.savefig(img, format='png')
    img.seek(0)

    plot_url = base64.b64encode(img.getvalue())
    plt.close()

    return render_template('Experiment.html', exp=expres, plot_url=plot_url)


if __name__ == '__main__':
    # The Flask Server is started
    default_handler.setLevel(logging.WARNING)
    app.run(host='0.0.0.0', port=port, debug=False)
Ejemplo n.º 23
0
Archivo: run.py Proyecto: codeway3/OSP
from logging.handlers import RotatingFileHandler
from flask.logging import default_handler
from app import app

if __name__ == '__main__':
    handler = RotatingFileHandler(filename='./log/app.log',
                                  maxBytes=1048576,
                                  backupCount=3)
    formatter = logging.Formatter(
        fmt=
        '%(asctime)s - %(name)s[line:%(lineno)d] - %(levelname)s - %(message)s',
        datefmt='%Y-%m-%d %H:%M:%S')
    handler.setFormatter(formatter)
    handler.setLevel(logging.DEBUG)
    app.logger.addHandler(handler)
    """ # flask.logging.create_logger
    logger = logging.getLogger('flask.app')

    if app.debug and logger.level == logging.NOTSET:
        logger.setLevel(logging.DEBUG)

    if not has_level_handler(logger):
        logger.addHandler(default_handler)

    return logger
    """
    default_handler.setLevel(logging.INFO)
    app.logger.addHandler(default_handler)

    app.run(host='0.0.0.0', port=9000)
def create_app(config_name):
    app = Flask(__name__)

    # Config
    app.config.from_object(config[config_name])
    config[config_name].init_app(app)

    # Logging
    class RequestFormatter(logging.Formatter):
        def format(self, record):
            record.url = 'NA'
            record.request_id = 'NA'

            if has_request_context():
                record.url = request.url
                if app.config['APP_ENABLE_REQUEST_ID']:
                    record.request_id = request.environ.get("HTTP_X_REQUEST_ID")

            return super().format(record)
    formatter = RequestFormatter(
        '[%(asctime)s] [%(levelname)s] [%(request_id)s] [%(url)s] %(module)s: %(message)s'
    )
    default_handler.setFormatter(formatter)
    default_handler.setLevel(app.config['LOGGING_LEVEL'])

    # Middleware / Wrappers
    if app.config['APP_ENABLE_PROXY_FIX']:
        ReverseProxyPrefixFix(app)
    if app.config['APP_ENABLE_REQUEST_ID']:
        RequestID(app)
    if app.config['APP_ENABLE_SENTRY']:
        sentry_sdk.init(**app.config['SENTRY_CONFIG'])

    # API client
    if not app.config['TESTING']:
        arctic_projects_api_auth = BackendApplicationClient(client_id=app.config['AZURE_OAUTH_APPLICATION_ID'])
        arctic_projects_api_auth_session = OAuth2Session(client=arctic_projects_api_auth)
        arctic_projects_api_auth_token = arctic_projects_api_auth_session.fetch_token(
            token_url=app.config['AZURE_OAUTH_TOKEN_URL'],
            client_id=app.config['AZURE_OAUTH_APPLICATION_ID'],
            client_secret=app.config['AZURE_OAUTH_APPLICATION_SECRET'],
            scope=app.config['AZURE_OAUTH_NERC_ARCTIC_OFFICE_SCOPES']
        )
        arctic_projects_api = Session(
            'https://api.bas.ac.uk/arctic-office-projects/testing',
            request_kwargs={'headers': {'authorization': f"bearer {arctic_projects_api_auth_token['access_token']}"}})

    # Templates
    app.jinja_loader = PrefixLoader({
        'app': PackageLoader('arctic_office_projects_manager'),
        'bas_style_kit': PackageLoader('bas_style_kit_jinja_templates'),
    })
    app.config['bsk_templates'] = BskTemplates()
    app.config['bsk_templates'].site_styles.append({
        'href': 'https://cdn.web.bas.ac.uk/libs/font-awesome-pro/5.3.1/css/all.min.css'
    })
    app.config['bsk_templates'].site_styles.append({
        'href': 'https://cdn.rawgit.com/jpswalsh/academicons/master/css/academicons.min.css'
    })
    app.config['bsk_templates'].site_styles.append({'href': '/static/css/app.css'})
    app.config['bsk_templates'].site_title = 'NERC Arctic Office Projects Manager'
    app.config['bsk_templates'].bsk_site_nav_brand_text = 'NERC Arctic Office Projects Manager'
    app.config['bsk_templates'].site_description = 'Management application for projects in the NERC Arctic Office ' \
                                                   'Projects Database'
    app.config['bsk_templates'].bsk_site_feedback_href = 'mailto:[email protected]'
    app.config['bsk_templates'].bsk_site_nav_primary.append({
        'value': 'Projects',
        'href': '/projects'
    })
    app.config['bsk_templates'].bsk_site_nav_launcher.append({
        'value': 'Arctic Office Website',
        'href': 'https://www.arctic.ac.uk'
    })

    # Routes
    #

    @app.route('/')
    def index():
        return redirect(url_for('projects_index'))

    @app.route('/projects')
    def projects_index():
        projects = arctic_projects_api.get('projects')
        # noinspection PyUnresolvedReferences
        return render_template(f"app/views/projects_index.j2", projects=projects)

    @app.route('/projects/<project_id>')
    def project_details(project_id: str):
        project = arctic_projects_api.get('projects', project_id)

        mermaid_asset = False
        for style in app.config['bsk_templates'].site_scripts:
            if style['href'] == 'https://unpkg.com/[email protected]/dist/mermaid.min.js':
                mermaid_asset = True
        if not mermaid_asset:
            app.config['bsk_templates'].site_scripts.append({
                'href': 'https://unpkg.com/[email protected]/dist/mermaid.min.js'
            })
        # noinspection PyUnresolvedReferences
        return render_template(f"app/views/project_details.j2", project=project, active_nav_item='/projects')

    @app.route('/meta/health/canary', methods=['get', 'options'])
    def meta_healthcheck_canary():
        """
        Returns whether this service is healthy

        This healthcheck checks the application itself (assumed to be healthy if this method can be executed).

        If healthy a 204 No Content response is returned, if unhealthy a 503 Service Unavailable response is returned.
        This healthcheck is binary and does not return any details to reduce payload size and prevent leaking sensitive
        data.

        Other healthcheck's should be used where more details are required. This healthcheck is intended for use with
        load balancers to give early indication of a service not being available.
        """
        return '', HTTPStatus.NO_CONTENT

    return app
Ejemplo n.º 25
0
def setup_flask(level='DEBUG', formatter_cls=JsonFormatter, **kwargs):
    default_handler.setLevel(level)
    default_handler.setFormatter(formatter_cls(
        **kwargs
    ))
Ejemplo n.º 26
0
celery = make_celery(app)

# load config and scorer
dir_path = os.path.dirname(os.path.realpath(__file__))
with open(dir_path + '/config.json', 'r') as f:
    config = json.load(f)
logging.getLogger('mwapi').setLevel(logging.ERROR)  # silence 1.19 warnings
scoring_handler = ScoringHandler(config)

# Log into scores.log
handler = logging.handlers.WatchedFileHandler('scores.log')
formatter = logging.Formatter("[%(asctime)s] %(message)s")
handler.setFormatter(formatter)
app.logger.setLevel(logging.INFO)
app.logger.info(app.import_name)
default_handler.setLevel(logging.ERROR)
app.logger.addHandler(handler)


@celery.task(serializer='json', name='process-next-task')
def score(wiki, rev_id, model, **kwargs):
    try:
        return scoring_handler.perform_scoring('https://' + wiki, str(rev_id),
                                               model, time.time())
    except Exception as e:
        app.logger.info('ERROR:' + wiki + ':' + str(rev_id) + ':' + model +
                        ':' + str(repr(e)))
        raise score.retry(args=[wiki, rev_id, model],
                          exc=e,
                          countdown=2,
                          max_retries=3,