Skip to content

Structlog Logging to Database #146

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 41 additions & 2 deletions conditional/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
except (InvalidGitRepository, KeyError):
app.config["GIT_REVISION"] = "unknown"


db = SQLAlchemy(app)
migrate = Migrate(app, db)
logger = structlog.get_logger()
sentry = Sentry(app)

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

# pylint: disable=C0413
from conditional.models.models import UserLog

# Configure Logging
def request_processor(logger, log_method, event_dict): # pylint: disable=unused-argument, redefined-outer-name
if 'request' in event_dict:
flask_request = event_dict['request']
event_dict['user'] = flask_request.headers.get("x-webauth-user")
event_dict['ip'] = flask_request.remote_addr
event_dict['method'] = flask_request.method
event_dict['blueprint'] = flask_request.blueprint
event_dict['path'] = flask_request.full_path
return event_dict


def database_processor(logger, log_method, event_dict): # pylint: disable=unused-argument, redefined-outer-name
if 'request' in event_dict:
if event_dict['method'] != 'GET':
log = UserLog(
ipaddr=event_dict['ip'],
user=event_dict['user'],
method=event_dict['method'],
blueprint=event_dict['blueprint'],
path=event_dict['path'],
description=event_dict['event']
)
db.session.add(log)
db.session.flush()
db.session.commit()
del event_dict['request']
return event_dict

structlog.configure(processors=[
request_processor,
database_processor,
structlog.processors.KeyValueRenderer()
])

logger = structlog.get_logger()


from conditional.blueprints.dashboard import dashboard_bp
from conditional.blueprints.dashboard import dashboard_bp # pylint: disable=ungrouped-imports
from conditional.blueprints.attendance import attendance_bp
from conditional.blueprints.major_project_submission import major_project_bp
from conditional.blueprints.intro_evals import intro_evals_bp
Expand Down
Loading