Django logger to log request response
Project description
request-response-logging
This logs drf request and response and also helps in generating request_id for each execution
Installation
You can install it using pip:
pip install request-response-logging
Usage
In Djangos settings file use following attributes for controlling logger. All these are optional
# Add following middleware
request_logging.middleware.LoggingMiddleware
# request header containing request id defaults X-REQUEST-ID if not provided
REQUEST_RESPONSE_ID_HEADER_KEY='X-REQUEST-ID'
# list of endpoints to ignore logging
REQUEST_RESPONSE_LOGGING_IGNORE_LIST= ['/get/user-details/', '/get/aadhar']
# list of path patterns where request body is not logged (other request/response details are still logged). Use regex (e.g. r'/shared/testing/.*$') or exact path. Logged body shows BODY_EXCLUDED for these.
REQUEST_RESPONSE_LOGGING_EXCLUDE_REQUEST_BODY_PATHS = [r'/api/sensitive/.*$']
# list of request/response field to mask in logging in case of JSONResponse(Fields are case insesitive)
REQUEST_RESPONSE_LOGGING_MASKING_FIELDS=['pan', 'mobile']
# list of response fields to be popped in case of JSONResponse at root level(Fields are case sensitive)
REQUEST_RESPONSE_LOGGING_POP_RESPONSE_KEYS=['error','traceback']
# health check api endpoint. this will return 200 status code with content OK
REQUEST_RESPONSE_LOGGING_HEALTH_CHECK_API='/api/health'
# default logger name is request_middleware_logger, in case of overwrite this attribute can be set
REQUEST_RESPONSE_LOGGING_LOGGER_NAME='django'
# list of request.META fields to be logged. By default request.META REQUEST_METHOD, CONTENT_LENGTH, HTTP_X_FORWARDED_FOR or REMOTE_ADDR, REMOTE_HOST, SERVER_NAME, SERVER_PORT is being logged in request.
REQUEST_RESPONSE_LOGGING_META_OPTIONS = ['HTTP_HEADER_FIELD1', 'HTTP_HEADER_FIELD2']
# set this to true if you want to log REQUEST_RESPONSE_LOGGING_META_OPTIONS in response as well. Defaults to False
REQUEST_RESPONSE_LOGGING_META_OPTIONS_IN_RESPONSE = True
# set this to true if you want to log 2XX response. Defaults to False
REQUEST_RESPONSE_LOGGING_LOG_2XX_RESPONSES = True
# list of headers to log. Case insensitive
REQUEST_RESPONSE_LOGGING_HEADERS_TO_LOG = ['X-App-Version', 'X-Model']
# In case only conditional 2XX needs to be logged based on some key in response object. This will only work if REQUEST_RESPONSE_LOGGING_LOG_2XX_RESPONSES=True
REQUEST_RESPONSE_LOGGING_2XX_CONDITIONAL_KEY = 'code'
# list of values to check for key mentioned in REQUEST_RESPONSE_LOGGING_2XX_CONDITIONAL_KEY. It's equality check
REQUEST_RESPONSE_LOGGING_2XX_CONDITIONAL_VALUES = [0, -1]
Sample logger in settings file. request_id filter is to add request_id and masking filter is used to mask fields in request and response
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'filters': {
'request_id': {
'()': 'request_logging.Filters.LoggingFilters.RequestIdFilter'
},
'masking': {
'()': 'request_logging.Filters.LoggingFilters.MaskingFilter'
}
},
'formatters': {
'open_search': {
'format': '{"request_id": "%(request_id)s", '
'"time": "%(asctime)s", "level": "%(levelname)s", '
'"file_path": "%(filename)s", "line_no": %(lineno)s, '
'"open_search_message": %(message)s}',
'datefmt': "%Y-%m-%d %H:%M:%S",
},
},
'handlers': {
'request_file': {
'level': 'INFO',
'class': 'logging.FileHandler',
'filename': 'logs/' + os.path.basename(BASE_DIR) + '.log',
'formatter': 'open_search',
'mode': 'a',
'filters': ['request_id', 'masking'] # Add the filters as needed
},
'console': {
'class': 'logging.StreamHandler',
'level': 'INFO',
'formatter': 'open_search',
'filters': ['request_id', 'masking'] # Add the filters as needed
},
'job_file': {
'level': 'INFO',
'class': 'logging.FileHandler',
'filename': 'logs/' + os.path.basename(BASE_DIR) + '-job.log',
'formatter': 'open_search',
'mode': 'a',
'filters': ['request_id', 'masking'] # Add the filters as needed
},
},
'loggers': {
'request_middleware_logger': {
'level': 'INFO',
'handlers': ['request_file', 'console'],
'propagate': True,
},
'job_logger': {
'level': 'INFO',
'handlers': ['job_file', 'console'],
'propagate': True,
},
}
}
In case you want to pass same request id across different functions in same context, use decorator generate_request_id. contextvar can be used in case of calling in delay along with decorator generate_request_id
from request_logging.decorators import generate_request_id
from request_logging.middleware.RequestResponseLogging import ctx_request_id
@app.task
@generate_request_id
def a(context_var=None):
try:
logger.info("Some message")
except Exception as e:
logger.error(f"Exception occured {str(traceback.format_exc())}")
# the decorator will generate request_id and bind it to given context if not already present in context
@generate_request_id
def a(context_var=None):
try:
logger.info("Some message")
a.delay(context_var=ctx_request_id.get(None))
except Exception as e:
logger.error(f"Exception occured {str(traceback.format_exc())}")
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file request_response_logging-1.0.7.tar.gz.
File metadata
- Download URL: request_response_logging-1.0.7.tar.gz
- Upload date:
- Size: 8.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
df47d5d7cd70979338bfeccba03d1ccf6fe4221b141d1ff5ee38d447c0a76bd7
|
|
| MD5 |
d57978b449b334e4c20ac54fe78e37d5
|
|
| BLAKE2b-256 |
f315bf6d67a52350c4dfb6f40e87e04d32dfa01d3f4a6a3c53272adf53ff47c9
|
File details
Details for the file request_response_logging-1.0.7-py3-none-any.whl.
File metadata
- Download URL: request_response_logging-1.0.7-py3-none-any.whl
- Upload date:
- Size: 8.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
98222eeadbb6e84ef6a3e5299baad604ea25e9a39f66ce8812bdde5da7f10679
|
|
| MD5 |
0b52a833567927ce18ae3616ff16e09c
|
|
| BLAKE2b-256 |
13eb9e0e4831d9cdc5b657ae6a0bdfa43831c3d9ef09169d107293b47df24785
|