Adding logging capabilities with syslog
This commit is contained in:
parent
5acf92155d
commit
b8d2add858
3 changed files with 37 additions and 7 deletions
|
@ -16,6 +16,8 @@
|
|||
along with scoreboard-benchmark . If not, see <http://www.gnu.org/licenses/>.
|
||||
"""
|
||||
import os
|
||||
import logging
|
||||
from logging.handlers import SysLogHandler
|
||||
from src.models import db
|
||||
|
||||
basedir = os.path.abspath(os.path.dirname(__file__))
|
||||
|
@ -41,9 +43,13 @@ class Config:
|
|||
MAX_RESULTS_PER_PAGE = 50
|
||||
MAX_PAGES = 2
|
||||
|
||||
# Unix logging settings
|
||||
syslog_handler = SysLogHandler()
|
||||
syslog_handler.setLevel(logging.WARNING)
|
||||
|
||||
@staticmethod
|
||||
def init_app(app):
|
||||
pass
|
||||
app.logger.addHandler(Config.syslog_handler)
|
||||
|
||||
|
||||
class DevelopmentConfig(Config):
|
||||
|
@ -53,6 +59,7 @@ class DevelopmentConfig(Config):
|
|||
|
||||
@staticmethod
|
||||
def init_app(app):
|
||||
app.logger.addHandler(Config.syslog_handler)
|
||||
with app.app_context():
|
||||
db.create_all()
|
||||
|
||||
|
@ -71,6 +78,7 @@ class ProductionConfig(Config):
|
|||
|
||||
@staticmethod
|
||||
def init_app(app):
|
||||
app.logger.addHandler(Config.syslog_handler)
|
||||
with app.app_context():
|
||||
db.create_all()
|
||||
|
||||
|
@ -82,6 +90,7 @@ class TestingConfig(Config):
|
|||
|
||||
@staticmethod
|
||||
def init_app(app):
|
||||
app.logger.addHandler(Config.syslog_handler)
|
||||
with app.app_context():
|
||||
db.drop_all()
|
||||
db.create_all()
|
||||
|
|
|
@ -19,22 +19,40 @@ import flask
|
|||
|
||||
error_pages = flask.Blueprint('error_pages', __name__, template_folder='templates')
|
||||
|
||||
report_string = "===============\n" \
|
||||
"{headers}" \
|
||||
"Path: {path}\n" \
|
||||
"Ip: {ip}\n" \
|
||||
"Message: {message}\n"
|
||||
|
||||
|
||||
@error_pages.app_errorhandler(404)
|
||||
def page_not_found_error(e):
|
||||
error = report_string.format(headers=flask.request.headers, ip=flask.request.remote_addr,
|
||||
message=e, path=flask.request.path)
|
||||
flask.current_app.logger.info(error)
|
||||
return flask.render_template("404.html"), 404
|
||||
|
||||
|
||||
@error_pages.app_errorhandler(500)
|
||||
def internal_server_error(e):
|
||||
error = report_string.format(headers=flask.request.headers, ip=flask.request.remote_addr,
|
||||
message=e, path=flask.request.path)
|
||||
flask.current_app.logger.error(error)
|
||||
return flask.render_template("500.html"), 500
|
||||
|
||||
|
||||
@error_pages.app_errorhandler(405)
|
||||
def method_not_allowed_error(e):
|
||||
error = report_string.format(headers=flask.request.headers, ip=flask.request.remote_addr,
|
||||
message=e, path=flask.request.path)
|
||||
flask.current_app.logger.warning(error)
|
||||
return flask.render_template("405.html"), 405
|
||||
|
||||
|
||||
@error_pages.app_errorhandler(400)
|
||||
def bad_request_error(e):
|
||||
error = report_string.format(headers=flask.request.headers, ip=flask.request.remote_addr,
|
||||
message=e, path=flask.request.path)
|
||||
flask.current_app.logger.info(error)
|
||||
return flask.render_template("400.html"), 400
|
||||
|
|
|
@ -31,12 +31,13 @@ def upload():
|
|||
"""
|
||||
return flask.render_template('upload.html')
|
||||
|
||||
# @scoreboard.route("/result", methods=['GET'])
|
||||
# def result_get():
|
||||
# """
|
||||
# Instead of method not allowed we redirect to scoreboard.upload
|
||||
# """
|
||||
# return flask.redirect(flask.url_for('scoreboard.upload'))
|
||||
|
||||
@scoreboard.route("/result", methods=['GET'])
|
||||
def result_get():
|
||||
"""
|
||||
Instead of method not allowed we redirect to scoreboard.upload
|
||||
"""
|
||||
return flask.redirect(flask.url_for('scoreboard.upload'))
|
||||
|
||||
|
||||
@scoreboard.route("/result", methods=['POST'])
|
||||
|
@ -69,6 +70,8 @@ def result_post():
|
|||
db.session.add(entry)
|
||||
db.session.commit()
|
||||
|
||||
flask.current_app.logger.info("{} added result with id: {}.".format(flask.request.remote_addr, entry.id))
|
||||
|
||||
location = "/result/{}".format(entry.id)
|
||||
|
||||
return flask.jsonify({'success': True}), 201, {'location': location}
|
||||
|
|
Loading…
Reference in a new issue