Hi, I have a Django project using Django 2.2.28, and python 3.5.2.
The live site runs fine, except for one function I'm trying to debug.
I've used logging before, but for some reason it's not working now.
Here's my code...
I call this file in wsgi.py. It's settings all work as they should, but LOGGING
isn't.
from .settings import *
DEBUG = False
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'handlers': {
'file': {
'level': 'DEBUG',
'class': 'logging.FileHandler',
'filename': '/home/myuser/proj_container/debug.log',
},
},
'loggers': {
'django.request': {
'handlers': ['file'],
'level': 'DEBUG',
'propagate': False,
},
},
}
import logging
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)
def some_func(request):
...
if some_condition:
logger.debug('In some_condition block')
elif some_other_condition:
logger.debug('In some_other_condition block')
I've tried the above with just the settings in production_settings.py
... Nothing shows up.
Same as 1, but with logging
imported and called at every important code block.
I read the docs for logger.debug()
, as well as this answer that I implemented above as you can see.
I also reread the Django logging docs.
I moved LOGGING
from production_settings.py
to settings.py
... Yesterday I fixed a bug where django wasn't overriding the DATABASES
variable in settings.py
from production_settings.py
, and was instead making a sqlite3 database and giving errors from settings.py
, and the only way it got fixed was by doing this.
For every change I've made above: I've always restarted the wsgi and the website (the bound unix socket service, because there is more than 1 site running on this server, and both sites work fine except for this problem on one of them). The cached values for them are not conflicting, my file paths are correct, etc.
Why isn't this working? What am I missing?
This has worked before just fine on other projects, but I have no idea why it's not now.
Thanks!
Here's a work around. I tested and tested my code above again, and I see nothing wrong with it, even re-read the entire relative doc pages again, so it's probably just a bug on that version.
Anyways, here's a solution for anyone who comes across this problem too.
Just make sure that your Linux user has permissions to write to the file path below.
import logging
logging.basicConfig(level=logging.DEBUG, filename='/home/myuser/path/to/debug.log') # will append by default, docs allow other values too.
logger = logging.getLogger(__name__)
def some_func(request):
...
if some_condition:
logger.debug('In some_condition block')
logger.debug('Object type == {}'.format(type(some_condition.attribute['key']['nested_key'])))
logger.debug('\n\n')
# etc
elif some_other_condition:
logger.debug('In some_other_condition block')
# LOGGING = {...} # is not necessary since the above does it fine. Probably just a bug like I mentioned with DATABASES.