Skip to content
Closed
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
Tweak to make request_id attribute configurable
The use case I have for this is logging to Systemd's Journal. The
logging handler they provide can pass extra record attributes as
KEY=value to the journal, but only if the key is uppercase. See
https://www.freedesktop.org/software/systemd/python-systemd/journal.html#journalhandler-class
  • Loading branch information
ipmb committed Nov 15, 2017
commit c6be8fa7ee4f3d3f251cc37b1bfbf0a07c8c5598
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ LOGGING = {
'filters': {
'request_id': {
'()': 'log_request_id.filters.RequestIDFilter'
# Optionally change the default `request_id` attribute
# 'request_id_attr': 'REQUEST_UID',
}
},
'formatters': {
Expand Down
7 changes: 6 additions & 1 deletion log_request_id/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@

class RequestIDFilter(logging.Filter):

def __init__(self, name='', request_id_attr='request_id'):
self.request_id_attr = request_id_attr
super(RequestIDFilter, self).__init__(name)

def filter(self, record):
record.request_id = getattr(local, 'request_id', NO_REQUEST_ID)
setattr(record, self.request_id_attr,
getattr(local, 'request_id', NO_REQUEST_ID))
return True