Skip to content

Commit 9f71dce

Browse files
authored
Merge pull request #146 from mbillow/logging
Structlog Logging to Database
2 parents 163b39a + 280eb5b commit 9f71dce

15 files changed

+339
-311
lines changed

conditional/__init__.py

+41-2
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@
2222
except (InvalidGitRepository, KeyError):
2323
app.config["GIT_REVISION"] = "unknown"
2424

25+
2526
db = SQLAlchemy(app)
2627
migrate = Migrate(app, db)
27-
logger = structlog.get_logger()
2828
sentry = Sentry(app)
2929

3030
ldap = CSHLDAP(app.config['LDAP_BIND_DN'],
@@ -38,8 +38,47 @@ def start_of_year():
3838
return start
3939

4040
# pylint: disable=C0413
41+
from conditional.models.models import UserLog
42+
43+
# Configure Logging
44+
def request_processor(logger, log_method, event_dict): # pylint: disable=unused-argument, redefined-outer-name
45+
if 'request' in event_dict:
46+
flask_request = event_dict['request']
47+
event_dict['user'] = flask_request.headers.get("x-webauth-user")
48+
event_dict['ip'] = flask_request.remote_addr
49+
event_dict['method'] = flask_request.method
50+
event_dict['blueprint'] = flask_request.blueprint
51+
event_dict['path'] = flask_request.full_path
52+
return event_dict
53+
54+
55+
def database_processor(logger, log_method, event_dict): # pylint: disable=unused-argument, redefined-outer-name
56+
if 'request' in event_dict:
57+
if event_dict['method'] != 'GET':
58+
log = UserLog(
59+
ipaddr=event_dict['ip'],
60+
user=event_dict['user'],
61+
method=event_dict['method'],
62+
blueprint=event_dict['blueprint'],
63+
path=event_dict['path'],
64+
description=event_dict['event']
65+
)
66+
db.session.add(log)
67+
db.session.flush()
68+
db.session.commit()
69+
del event_dict['request']
70+
return event_dict
71+
72+
structlog.configure(processors=[
73+
request_processor,
74+
database_processor,
75+
structlog.processors.KeyValueRenderer()
76+
])
77+
78+
logger = structlog.get_logger()
79+
4180

42-
from conditional.blueprints.dashboard import dashboard_bp
81+
from conditional.blueprints.dashboard import dashboard_bp # pylint: disable=ungrouped-imports
4382
from conditional.blueprints.attendance import attendance_bp
4483
from conditional.blueprints.major_project_submission import major_project_bp
4584
from conditional.blueprints.intro_evals import intro_evals_bp

0 commit comments

Comments
 (0)