Skip to content

Commit

Permalink
Add request and exception log via flask signals.
Browse files Browse the repository at this point in the history
  • Loading branch information
r4ntix committed Mar 23, 2015
1 parent 5180a5a commit 0f299e6
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 13 deletions.
31 changes: 18 additions & 13 deletions website/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
ECS VPN Website.
"""

__version__ = '1.0.0'
__version__ = '1.1.1'

import os

Expand All @@ -15,6 +15,23 @@
app.config.from_object('websiteconfig.default_settings')
app.config.from_pyfile('application.cfg', silent=True)

import logging
from logging import Formatter
from logging.handlers import TimedRotatingFileHandler
website_log = '%s/logs/website.log' % os.path.abspath(os.path.join(os.path.dirname(__file__),
os.path.pardir))
file_handler = TimedRotatingFileHandler(website_log,
'W0', 1, backupCount=7)
file_handler.suffix = '%Y%m%d-%H%M'
file_handler.setLevel(logging.INFO)
file_handler.setFormatter(Formatter('%(asctime)s %(levelname)s: %(message)s'))
app.logger.addHandler(file_handler)

from flask import request_started, got_request_exception
from website.helpers import log_request, log_exception
request_started.connect(log_request, app)
got_request_exception.connect(log_exception, app)

from flask.ext.sqlalchemy import SQLAlchemy
db = SQLAlchemy(app)

Expand All @@ -39,15 +56,3 @@
app.register_blueprint(snat)
app.register_blueprint(api)
app.register_blueprint(docs)

import logging
from logging import Formatter
from logging.handlers import TimedRotatingFileHandler
website_log = '%s/logs/website.log' % os.path.abspath(os.path.join(os.path.dirname(__file__),
os.path.pardir))
file_handler = TimedRotatingFileHandler(website_log,
'W0', 1, backupCount=7)
file_handler.suffix = '%Y%m%d-%H%M'
file_handler.setLevel(logging.INFO)
file_handler.setFormatter(Formatter('%(asctime)s %(levelname)s: %(message)s'))
app.logger.addHandler(file_handler)
21 changes: 21 additions & 0 deletions website/helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# -*- coding: utf-8 -*-
"""
website.helpers
~~~~~~~~~~~~~~~
top level helpers.
"""


from flask import request


def log_request(sender, **extra):
sender.logger.info('Request-Message: %s %s %s',
request.method,
request.url,
request.data)


def log_exception(sender, exception, **extra):
sender.logger.error('Exception-Request: %s', exception)

0 comments on commit 0f299e6

Please sign in to comment.