Skip to main content

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

request_response_logging-1.0.7.tar.gz (8.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

request_response_logging-1.0.7-py3-none-any.whl (8.0 kB view details)

Uploaded Python 3

File details

Details for the file request_response_logging-1.0.7.tar.gz.

File metadata

File hashes

Hashes for request_response_logging-1.0.7.tar.gz
Algorithm Hash digest
SHA256 df47d5d7cd70979338bfeccba03d1ccf6fe4221b141d1ff5ee38d447c0a76bd7
MD5 d57978b449b334e4c20ac54fe78e37d5
BLAKE2b-256 f315bf6d67a52350c4dfb6f40e87e04d32dfa01d3f4a6a3c53272adf53ff47c9

See more details on using hashes here.

File details

Details for the file request_response_logging-1.0.7-py3-none-any.whl.

File metadata

File hashes

Hashes for request_response_logging-1.0.7-py3-none-any.whl
Algorithm Hash digest
SHA256 98222eeadbb6e84ef6a3e5299baad604ea25e9a39f66ce8812bdde5da7f10679
MD5 0b52a833567927ce18ae3616ff16e09c
BLAKE2b-256 13eb9e0e4831d9cdc5b657ae6a0bdfa43831c3d9ef09169d107293b47df24785

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page