Plugin for django to support Google Structured Logger
Project description
Django Google Structured Logger
Django Google Structured Logger is a Django middleware designed to capture and log details from incoming requests and outgoing responses. It offers features to mask sensitive data, set default fields for Google Cloud Logging, and structure logs in a detailed and organized manner.
Contents
Features:
- Detailed Logging: Logs both requests and responses with meticulous details.
- Sensitive Data Masking: Masks sensitive information using customizable regex patterns.
- Google Cloud Logging Support: Formats logs to match Google Cloud Logging standards.
- Configurable Settings: Customize log behavior through Django settings.
Usage:
-
Add
GoogleFormatter
to your Django'sLOGGING
setting. Example:LOGGING = { "version": 1, "disable_existing_loggers": False, "formatters": { "json": { "()": "django_google_structured_logger.formatter.GoogleFormatter", }, }, "handlers": { "google-json-handler": { "class": "logging.StreamHandler", "formatter": "json", }, }, "root": { "handlers": ["google-json-handler"], "level": logging.INFO, } }
-
Add
SetRequestToLoggerMiddleware
to your Django'sMIDDLEWARE
setting.Example for Django middleware:
MIDDLEWARE = [ ... # Ordering is important: "django_google_structured_logger.middlewares.SetUserContextMiddleware", # Set user context to logger. "django_google_structured_logger.middlewares.LogRequestAndResponseMiddleware", # Log request and response. ]
Example for GRAPHENE middleware:
GRAPHENE = { "MIDDLEWARE": [ ... # Ordering is important: "django_google_structured_logger.graphene_middlewares.GrapheneSetUserContextMiddleware", # Set user context to logger. "django_google_structured_logger.graphene_middlewares.GrapheneLogRequestAndResponseMiddleware", # Log request and response. ] }
-
Ensure your Django project has the necessary configurations in the
settings.py
.
Key Components:
1. middleware.py
- SetRequestToLoggerMiddleware: This class contains methods to process incoming requests and outgoing responses and then log them. It supports features like abridging lengthy data and masking sensitive information.
2. formatter.py
- GoogleFormatter: Extends
jsonlogger.JsonFormatter
to format logs specifically for Google Cloud Logging. It sets default fields such as severity, labels, operation, and source location based on Google's logging standards.
3. settings.py
- Provides a list of default sensitive keys for data masking.
- Allows customization of logging behavior with options to specify maximum string length, excluded endpoints, sensitive keys, and more.
Settings:
These are the settings that can be customized for the middleware:
LOG_MAX_STR_LEN
: Maximum string length before data is abridged. Default is200
.LOG_MAX_LIST_LEN
: Maximum list length before data is abridged. Default is10
.LOG_EXCLUDED_ENDPOINTS
: List of endpoints to exclude from logging. Default is anempty list
.LOG_SENSITIVE_KEYS
: Regex patterns for keys which contain sensitive data. DefaultsDEFAULT_SENSITIVE_KEYS
.LOG_MASK_STYLE
: Style for masking sensitive data. Default is"partially"
.LOG_MIDDLEWARE_ENABLED
: Enable or disable the logging middleware. Default isTrue
.LOG_EXCLUDED_HEADERS
: List of request headers to exclude from logging. DefaultsDEFAULT_SENSITIVE_HEADERS
.LOG_USER_ID_FIELD
: Field name for user ID. Default is"id"
.LOG_USER_DISPLAY_FIELD
: Field name for user email. Default is"email"
.LOG_MAX_DEPTH
: Maximum depth for data to be logged. Default is4
.
Note:
- All settings are imported from
django_google_structured_logger.settings
.
Other Notes:
extra
kwargs passed to logger, for example:logger.info("some message", extra={"some_key": "some_data}
will be logged as structured data in thejsonPayload
field in Google Cloud Logging. Any data passed to extra kwargs will not be abridged or masked.extra
kwargs passed to logger may override any default fields set byGoogleFormatter
.
Conclusion:
SetRequestToLoggerMiddleware is a comprehensive solution for those seeking enhanced logging capabilities in their Django projects, with particular attention to sensitive data protection and compatibility with Google Cloud Logging.
To get started, integrate the provided middleware, formatter, and settings into your Django project, customize as needed, and enjoy advanced logging capabilities!
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-google-structured-logger-0.2.3.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8bfe001b8d6f2e3bfca479257b25e3132b9a9642d6ad7fe8fa4906e99a55582f |
|
MD5 | a6133b81b7df6abfb9671ce585e129f7 |
|
BLAKE2b-256 | 8f7f4610dde6d2915de3970e784e13c63bc799a56ec25d08cee8f51fac34b3b1 |
Hashes for django_google_structured_logger-0.2.3-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5e91f00a2a7f6a5d71c41914ab341a669e456bf471363ca85fb232c552f92194 |
|
MD5 | 4386bb7928cb5a5b189196758cfb2170 |
|
BLAKE2b-256 | 17183c900509ee3f1de278de2a57404fecfe9965d56636fce5508102a2c7fc38 |