forked from emfcamp/Website
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloggingmanager.py
More file actions
40 lines (26 loc) · 1.01 KB
/
Copy pathloggingmanager.py
File metadata and controls
40 lines (26 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
"""A middleware to export user IDs for logging.
Werkzeug logs the request after the Flask app context has ended
so we use Werkzeug's Local object to pass the user ID into the
logging formatter.
"""
import logging
import os
from werkzeug.local import Local, LocalManager
local = Local()
local_manager = LocalManager([local])
class ContextFormatter(logging.Formatter):
"""A logging formatter which inserts the user ID
into the logging record."""
def format(self, record):
record.user = getattr(local, "user_id", None)
record.pid = os.getpid()
return logging.Formatter.format(self, record)
class NoStaticFilter(logging.Filter):
"""Disable logging of static content requests."""
def filter(self, record: logging.LogRecord) -> bool:
return "/static/" not in record.getMessage()
def set_user_id(uid):
"""Set the user ID for later use in logging."""
local.user_id = uid
def create_logging_manager(app):
app.wsgi_app = local_manager.make_middleware(app.wsgi_app)