Here are my findings,
I created the Django project and added your testapp from the sandbox as an app
and updated the settings.py
as well. This is how my settings.py
file looks like,
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
# apps
'LoggingTest',
'testapp.apps.TestAppConfig',
# 3rd party packages
'rest_framework',
'cid.apps.CidAppConfig',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
# 3rd party middlewares
'cid.middleware.CidMiddleware',
]
LOGGING = {
'version': 1,
'formatters': {
'verbose': {
'format': '[cid: %(cid)s] %(levelname)s %(asctime)s %(module)s %(process)d %(thread)d %(message)s'
},
'simple': {
'format': '[cid: %(cid)s] %(levelname)s %(message)s'
},
},
'handlers': {
'console': {
'level': 'INFO',
'class': 'logging.StreamHandler',
'formatter': 'verbose',
},
},
'filters': {
'correlation': {
'()': 'cid.log.CidContextFilter'
},
},
'loggers': {
'testapp': {
'handlers': ['console'],
'filters': ['correlation'],
'propagate': True,
},
},
}
Everything similar to the sandbox/settings.py except CID_GENERATE = True
part. I didn't add that.
I tested your ok
API and got the result as expected like below,
I tried several times but the logged details are displayed without cid
value, it's None
always.
Performing system checks...
System check identified no issues (0 silenced).
You have 1 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): testapp.
Run 'python manage.py migrate' to apply them.
September 25, 2020 - 16:51:14
Django version 3.1.1, using settings 'SampleDjangoProject.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
[cid: None] WARNING 2020-09-25 16:51:15,271 views 10879 139911689578240 This is a warning from django-cid test application.
[cid: None] WARNING 2020-09-25 16:51:16,158 views 10879 139911689578240 This is a warning from django-cid test application.
[cid: None] WARNING 2020-09-25 16:51:17,001 views 10879 139911689578240 This is a warning from django-cid test application.
[cid: None] WARNING 2020-09-25 16:51:17,835 views 10879 139911689578240 This is a warning from django-cid test application.
[cid: None] WARNING 2020-09-25 16:51:18,572 views 10879 139911689578240 This is a warning from django-cid test application.
According to the django-cid
documentation,
The middleware takes care of getting the correlation from the HTTP request header. By default it looks for a header named X_CORRELATION_ID, but you can change this with the CID_HEADER setting:
CID_HEADER = 'X_MY_CID_HEADER'
Then I added CID_HEADER
as X_CORRELATION_ID
to the settings.py
file. Still the problem is same, so just updated CID_HEADER
as HTTP_X_CORRELATION_ID
, then it worked with X-Correlation-Id
API header.
Performing system checks...
System check identified no issues (0 silenced).
You have 1 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): testapp.
Run 'python manage.py migrate' to apply them.
September 25, 2020 - 16:48:38
Django version 3.1.1, using settings 'SampleDjangoProject.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
[cid: 123-321] WARNING 2020-09-25 16:48:44,157 views 10497 139905992644352 This is a warning from django-cid test application.
[cid: 123-321] WARNING 2020-09-25 16:48:44,610 views 10497 139905992644352 This is a warning from django-cid test application.
[cid: 123-321] WARNING 2020-09-25 16:48:45,346 views 10497 139905992644352 This is a warning from django-cid test application.
[cid: 123-321] WARNING 2020-09-25 16:48:45,941 views 10497 139905992644352 This is a warning from django-cid test application.
[cid: 123-321] WARNING 2020-09-25 16:48:46,668 views 10497 139905992644352 This is a warning from django-cid test application.
So I need to add this logging
to the other app
as well, so I changed testapp
to ''
in loggers
in LOGGING
like below,
LOGGING = {
...
'loggers': {
'': {
'handlers': ['console'],
'filters': ['correlation'],
'propagate': True,
},
},
}
After that I didn't get any logging
details, here what Django official documentation mentioned about Configuring logging,
If the disable_existing_loggers key in the LOGGING dictConfig is set to True (which is the dictConfig default if the key is missing) then all loggers from the default configuration will be disabled. Disabled loggers are not the same as removed; the logger will still exist, but will silently discard anything logged to it, not even propagating entries to a parent logger. Thus you should be very careful using 'disable_existing_loggers': True; itβs probably not what you want. Instead, you can set disable_existing_loggers to False and redefine some or all of the default loggers; or you can set LOGGING_CONFIG to None and handle logging config yourself.
So just added disable_existing_loggers
as False
to the LOGGING
like below,
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
...
}
After that I got the following error,
Watching for file changes with StatReloader
--- Logging error ---
Traceback (most recent call last):
File "/usr/lib/python3.6/logging/__init__.py", line 994, in emit
msg = self.format(record)
File "/usr/lib/python3.6/logging/__init__.py", line 840, in format
return fmt.format(record)
File "/usr/lib/python3.6/logging/__init__.py", line 580, in format
s = self.formatMessage(record)
File "/usr/lib/python3.6/logging/__init__.py", line 549, in formatMessage
return self._style.format(record)
File "/usr/lib/python3.6/logging/__init__.py", line 391, in format
return self._fmt % record.__dict__
KeyError: 'cid'
Call stack:
File "manage.py", line 22, in <module>
main()
File "manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "/home/wasdkiller/PycharmProjects/SampleDjangoProject/venv/lib/python3.6/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
utility.execute()
File "/home/wasdkiller/PycharmProjects/SampleDjangoProject/venv/lib/python3.6/site-packages/django/core/management/__init__.py", line 395, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/wasdkiller/PycharmProjects/SampleDjangoProject/venv/lib/python3.6/site-packages/django/core/management/base.py", line 330, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/wasdkiller/PycharmProjects/SampleDjangoProject/venv/lib/python3.6/site-packages/django/core/management/commands/runserver.py", line 61, in execute
super().execute(*args, **options)
File "/home/wasdkiller/PycharmProjects/SampleDjangoProject/venv/lib/python3.6/site-packages/django/core/management/base.py", line 371, in execute
output = self.handle(*args, **options)
File "/home/wasdkiller/PycharmProjects/SampleDjangoProject/venv/lib/python3.6/site-packages/django/core/management/commands/runserver.py", line 96, in handle
self.run(**options)
File "/home/wasdkiller/PycharmProjects/SampleDjangoProject/venv/lib/python3.6/site-packages/django/core/management/commands/runserver.py", line 103, in run
autoreload.run_with_reloader(self.inner_run, **options)
File "/home/wasdkiller/PycharmProjects/SampleDjangoProject/venv/lib/python3.6/site-packages/django/utils/autoreload.py", line 612, in run_with_reloader
logger.info('Watching for file changes with %s', reloader.__class__.__name__)
Message: 'Watching for file changes with %s'
Arguments: ('StatReloader',)
Performing system checks...
System check identified no issues (0 silenced).
You have 1 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): testapp.
Run 'python manage.py migrate' to apply them.
September 25, 2020 - 16:46:23
Django version 3.1.1, using settings 'SampleDjangoProject.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
In the last Example they've added filters
to the handlers
. I did the same and modified like below,
LOGGING = {
...
'handlers': {
'console': {
'level': 'INFO',
'class': 'logging.StreamHandler',
'formatter': 'verbose',
'filters': ['correlation'],
},
},
...
}
That's it, now everything works fine. Please update django-cid
documentation accordingly.
I faced the issues on below environment,
- Django --> 3.1.1
- django-cid --> 2.1
- Python --> 3.6.9