Adding logging capabilities with syslog

This commit is contained in:
Denis-Cosmin Nutiu 2017-04-22 20:31:50 +03:00
parent 5acf92155d
commit b8d2add858
3 changed files with 37 additions and 7 deletions

View file

@ -16,6 +16,8 @@
along with scoreboard-benchmark . If not, see <http://www.gnu.org/licenses/>. along with scoreboard-benchmark . If not, see <http://www.gnu.org/licenses/>.
""" """
import os import os
import logging
from logging.handlers import SysLogHandler
from src.models import db from src.models import db
basedir = os.path.abspath(os.path.dirname(__file__)) basedir = os.path.abspath(os.path.dirname(__file__))
@ -41,9 +43,13 @@ class Config:
MAX_RESULTS_PER_PAGE = 50 MAX_RESULTS_PER_PAGE = 50
MAX_PAGES = 2 MAX_PAGES = 2
# Unix logging settings
syslog_handler = SysLogHandler()
syslog_handler.setLevel(logging.WARNING)
@staticmethod @staticmethod
def init_app(app): def init_app(app):
pass app.logger.addHandler(Config.syslog_handler)
class DevelopmentConfig(Config): class DevelopmentConfig(Config):
@ -53,6 +59,7 @@ class DevelopmentConfig(Config):
@staticmethod @staticmethod
def init_app(app): def init_app(app):
app.logger.addHandler(Config.syslog_handler)
with app.app_context(): with app.app_context():
db.create_all() db.create_all()
@ -71,6 +78,7 @@ class ProductionConfig(Config):
@staticmethod @staticmethod
def init_app(app): def init_app(app):
app.logger.addHandler(Config.syslog_handler)
with app.app_context(): with app.app_context():
db.create_all() db.create_all()
@ -82,6 +90,7 @@ class TestingConfig(Config):
@staticmethod @staticmethod
def init_app(app): def init_app(app):
app.logger.addHandler(Config.syslog_handler)
with app.app_context(): with app.app_context():
db.drop_all() db.drop_all()
db.create_all() db.create_all()

View file

@ -19,22 +19,40 @@ import flask
error_pages = flask.Blueprint('error_pages', __name__, template_folder='templates') 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) @error_pages.app_errorhandler(404)
def page_not_found_error(e): 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 return flask.render_template("404.html"), 404
@error_pages.app_errorhandler(500) @error_pages.app_errorhandler(500)
def internal_server_error(e): 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 return flask.render_template("500.html"), 500
@error_pages.app_errorhandler(405) @error_pages.app_errorhandler(405)
def method_not_allowed_error(e): 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 return flask.render_template("405.html"), 405
@error_pages.app_errorhandler(400) @error_pages.app_errorhandler(400)
def bad_request_error(e): 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 return flask.render_template("400.html"), 400

View file

@ -31,12 +31,13 @@ def upload():
""" """
return flask.render_template('upload.html') return flask.render_template('upload.html')
# @scoreboard.route("/result", methods=['GET'])
# def result_get(): @scoreboard.route("/result", methods=['GET'])
# """ def result_get():
# Instead of method not allowed we redirect to scoreboard.upload """
# """ Instead of method not allowed we redirect to scoreboard.upload
# return flask.redirect(flask.url_for('scoreboard.upload')) """
return flask.redirect(flask.url_for('scoreboard.upload'))
@scoreboard.route("/result", methods=['POST']) @scoreboard.route("/result", methods=['POST'])
@ -69,6 +70,8 @@ def result_post():
db.session.add(entry) db.session.add(entry)
db.session.commit() db.session.commit()
flask.current_app.logger.info("{} added result with id: {}.".format(flask.request.remote_addr, entry.id))
location = "/result/{}".format(entry.id) location = "/result/{}".format(entry.id)
return flask.jsonify({'success': True}), 201, {'location': location} return flask.jsonify({'success': True}), 201, {'location': location}