django-logging-context
Description
django-logging-context is BSD licensed library extending default django
logging context with following additional values:
- request id,
- request duration,
- remote address,
- response length,
- user id,
- username.
Quick start
- install the library:
pip install django-logging-context -y
- Add
django-logging-contextto yourINSTALLED_APPSsetting like this:
INSTALLED_APPS = [
...
'django_logging_context',
]
- Add proxy middleware to your
MIDDLEWAREsetting like this:
MIDDLEWARE = [
'django_logging_context.middlewares.LoggingContextMiddleware',
...
]
It's important to place this LoggingContextMiddleware at the first place in
a MIDDLEWARE to allow to calculate duration of response more precisely.
- If you just want to add info about request duration and request id to your
log records then you can use
LoggingWSGIMiddlewarein yourwsgi.pylike this:
from django_logging_context.wsgi import LoggingWSGIMiddleware
application = LoggingWSGIMiddleware(get_wsgi_application())
- Use this example of logging setting to set up your loggers correctly
import os
LOG_LEVEL = os.environ.get('LOG_LEVEL', 'INFO')
DB_LOG_LEVEL = os.environ.get('DB_LOG_LEVEL', LOG_LEVEL)
REQUESTS_LOG_LEVEL = os.environ.get('REQUESTS_LOG_LEVEL', LOG_LEVEL)
CELERY_LOG_LEVEL = os.environ.get('CELERY_LOG_LEVEL', LOG_LEVEL)
SENTRY_LOG_LEVEL = os.environ.get('SENTRY_LOG_LEVEL', LOG_LEVEL)
LOGGING = {
'version': 1,
'loggers': {
'django': {'level': LOG_LEVEL},
'django.db': {'level': DB_LOG_LEVEL},
'urllib3': {'level': REQUESTS_LOG_LEVEL},
'celery': {'level': CELERY_LOG_LEVEL},
'sentry': {'level': SENTRY_LOG_LEVEL},
},
'root': {
'level': LOG_LEVEL,
'handlers': ['console']
},
'handlers': {
'console': {
'class': 'logging.StreamHandler',
'formatter': 'verbose',
'filters': ['extending_context_filter']
}
},
'filters': {
'extending_context_filter': {
'()': 'django_logging_context.logging.ContextExtendingFilter'
}
},
'formatters': {
'verbose': {
'format': ('[django] %(levelname)s %(asctime)s'
' %(name)s/%(module)s'
' %(process)d/%(thread)d'
' request_id: %(request_id)s'
' remote_addr: %(remote_addr)s'
' user_id: %(user_id)s'
' username: %(username)s'
' duration: %(response_duration)s'
' uri: %(uri)s'
' %(message)s')
},
},
}
Log records example
[django] INFO 2021-04-08 18:12:13,573 django.server/basehttp 47385/123145535799296 request_id: ea9a2dfd-a662-4632-84d0-d0c5151b5422 remote_addr: 127.0.0.1 user_id: 2 username: root duration: 1.548695s uri: http://127.0.0.1:8000/login/?next=/ "GET /admin/ HTTP/1.1" 200 46937
Metadata
Release files for django-logging-context 1.1.4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| django-logging-context-1.1.4.tar.gz | 6.2 kB | Without provenance | Details for django-logging-context-1.1.4.tar.gz |
Release files / django-logging-context-1.1.4.tar.gz
| Download URL | django-logging-context-1.1.4.tar.gz |
|---|---|
| Size | 6.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
b7202bf9c275ffe1dbca176861731283ce86829b8b8f6b7b96f8345b738a6f45
|
|
BLAKE2b-256 checksum How to use checksums |
ea52f4a6d94dde36d52e91df7d7ff34d2d989d3cca8c829cad79af2778e3ba5e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.4.1 importlib_metadata/4.6.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.9.5
|
Security for django-logging-context 1.1.4
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.