Add flask-oslolog to Nemesis project for logging

Change-Id: I9b1c666e9563bf03971342e55f043504532bbd08
This commit is contained in:
Robert Putt 2017-04-14 15:09:14 +01:00
parent 18632f314d
commit d78c005ff3
6 changed files with 95 additions and 4 deletions

View File

View File

@ -0,0 +1,30 @@
# -*- coding: utf-8 -*-
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from flask import Blueprint
from python_nemesis.exceptions import general_handler
from python_nemesis.exceptions import NemesisException
V1_API = Blueprint('v1_api', __name__)
@V1_API.errorhandler(NemesisException)
def handle_exception(error):
return general_handler(error)
@V1_API.route('/v1')
def api_definition():
return ""

View File

@ -16,7 +16,7 @@ from oslo_config import cfg
from python_nemesis.config import collect_sqlalchemy_opts
from python_nemesis.config import register_opts
from python_nemesis.extensions import db
# from python_nemesis.extensions import log
from python_nemesis.extensions import log
def configure_blueprints(app, blueprints):
@ -63,7 +63,7 @@ def configure_extensions(app):
:type app: :py:class:`flask.Flask`
"""
db.init_app(app)
# log.init_app(app)
log.init_app(app)
def create_app(app_name=None, blueprints=None):
@ -83,6 +83,11 @@ def create_app(app_name=None, blueprints=None):
configure_app(app)
configure_extensions(app)
# Here we register the application blueprints.
from python_nemesis.api.v1 import V1_API
blueprints = [V1_API]
configure_blueprints(app, blueprints)
return app

View File

@ -0,0 +1,55 @@
# -*- coding: utf-8 -*-
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from flask import request
import json
from python_nemesis.extensions import log
def general_handler(error):
log.logger.exception("Hit exception during %s" % request)
try:
status_code = error.status_code
ret_data = json.dumps(error.to_dict())
except Exception:
status_code = 500
ret_data = {"title": "Internal Server Error",
"code": status_code,
"message": ""}
ret_data = json.dumps(ret_data)
return ret_data, status_code
class NemesisException(Exception):
status_code = 500
title = "Internal Server Error"
message = ""
def __init__(self, title, message, status_code=None, payload=None):
Exception.__init__(self)
self.title = title
self.message = message
if status_code is not None:
self.status_code = status_code
self.payload = payload
def to_dict(self):
rv = dict(self.payload or ())
rv['code'] = self.status_code
rv['title'] = self.title
rv['message'] = self.message
return rv

View File

@ -1,6 +1,6 @@
# from flask_oslolog import OsloLog
from flask_oslolog import OsloLog
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()
# log = OsloLog()
log = OsloLog()

View File

@ -6,6 +6,7 @@ pbr>=2.0.0 # Apache-2.0
alembic>=0.8.10 # MIT
Flask!=0.11,<1.0,>=0.10 # BSD
Flask-SQLAlchemy>=2.0 # BSD
flask-oslolog # Apache-2.0
oslo.config>=3.22.0 # Apache-2.0
oslo.messaging>=5.19.0 # Apache-2.0
oslo.log>=3.11.0 # Apache-2.0