Django middleware and log filter to attach a unique ID to every log message generated as part of a request
Project description
django-log-labeler
Django middleware and log filter to attach values from the headers to every log message generated as part of a request.
Author: Hermann Stephane Ntsamo
Example
DEBUG [340d34bb4bb91ed4f45c414808a03a65] myproject.apps.myapp.views: Some log message
DEBUG [340d34bb4bb91ed4f45c414808a03a65] myproject.apps.myapp.forms: Some other log message
Installation and usage
First, install the package: pip install django-log-labeler
Add the middleware to your MIDDLEWARE_CLASSES
setting.
MIDDLEWARE_CLASSES = (
'log_labeler.middleware.HeaderToLabelMiddleware',
# ... other middleware goes here
)
Add the log_labeler.filters.HeaderToLabelFilter
to your LOGGING
setting. Update your formatters
to include the header names you want appearing in the log message. Add a handler to output the messages (eg to the console), and finally attach the handler to your application's logger.
An example LOGGING
setting is below:
LOG_LABEL_REQUEST_SETTING = {
"request_id": "HTTP_X_REQUEST_ID"
}
MAX_REQUEST_RESPONSE_SIZE = 500
DEFAULT_LOG_LEVEL = "INFO"
NIM_DJANGO_REQUEST_LOG_LEVEL_NAME = "HTTP_NIM_CORRELATION_ID"
LOG_LABEL_EXCLUDE_LOG_LIST = [
"gunicorn.access"
]
LOG_LABEL_OBFUSCATE = {
"headers": ["Authorization"],
"body": {
"(?i)(\\<(\\w+\\:)?password.*\\>).+(\\<\\/(\\w+\\:)?password.*\\>)": r"\1[--HIDDEN--]\3",
"(?i)(\\<(\\w+\\:)?session.*\\>).+(\\<\\/(\\w+\\:)?session.*\\>)": r"\1[--HIDDEN--]\3",
"(?i)(\\<(\\w+\\:)?token.*\\>).+(\\<\\/(\\w+\\:)?token.*\\>)": r"\1[--HIDDEN--]\3",
"(?i)(\\??password.*)=(.+)(\\&?)": r"\1[--HIDDEN--]\3",
"(?i)(\\??token.*)=(.+)(\\&?)": r"\1[--HIDDEN--]\3",
"(?i)(.+\\\".*token.*\\\"\\s*\\:\\s*\\\")(.+)(\\\"(\\,|\\}).+)": r"\1[--HIDDEN--]\3",
},
"response": {
"(?i)(\\<(\\w+\\:)?password.*\\>).+(\\<\\/(\\w+\\:)?password.*\\>)": r"\1[--HIDDEN--]\3",
"(?i)(\\<(\\w+\\:)?session.*\\>).+(\\<\\/(\\w+\\:)?session.*\\>)": r"\1[--HIDDEN--]\3",
"(?i)(\\<(\\w+\\:)?token.*\\>).+(\\<\\/(\\w+\\:)?token.*\\>)": r"\1[--HIDDEN--]\3",
"(?i)(\\??password.*)=(.+)(\\&?)": r"\1[--HIDDEN--]\3",
"(?i)(\\??token.*)=(.+)(\\&?)": r"\1[--HIDDEN--]\3",
"(?i)(.+\\\".*token.*\\\"\\s*\\:\\s*\\\")(.+)(\\\"(\\,|\\}).+)": r"\1[--HIDDEN--]\3",
}
}
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'filters': {
'request_id': {
'()': 'log_labeler.filters.HeaderToLabelFilter'
}
},
'formatters': {
'standard': {
'format': '%(levelname)-8s [%(asctime)s] [%(request_id)s] %(name)s: %(message)s'
},
},
'handlers': {
'console': {
'level': 'DEBUG',
'class': 'logging.StreamHandler',
'filters': ['request_id'],
'formatter': 'standard',
},
},
'loggers': {
'log_labeler.middleware': {
'handlers': ['console'],
'level': 'DEBUG',
'propagate': False,
},
{
'gunicorn.access': {
'handlers': ['console'],
'level': 'INFO',
'propagate': False,
},
}
}
You can then output log messages as usual:
import logging
logger = logging.getLogger(__name__)
logger.debug("Hello world!")
License
Copyright © 2012-2018, DabApps.
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for django-log-labeler-0.0.43.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | d4dc39caa910d54a3e3a14916f2d0b2d0415511fd278ec758f448d09ad49f8c4 |
|
MD5 | 18ad53258d70b409a945814c2777e217 |
|
BLAKE2b-256 | a7e250b67ca0fb10907231e7c90e538e57b933f517979ed33a3dfeb3406e7033 |
Hashes for django_log_labeler-0.0.43-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 290723a296c96fa12fde1c334675e0978b3d7f98d814806f4449174ad28e6d36 |
|
MD5 | 03f5a6ebda713499b929af81bdd4d760 |
|
BLAKE2b-256 | 99d4a1a368fa6f19a5aa2fc8028279aeb59c69731ac342e50125d226f8012243 |