I have set up papertrail for a Django project but logging fails with OSError: [Errno 9] Bad file descriptor. The weird thing is if I call django.setup() before the logging call it works...
settings.py
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'json': {
'()': 'pythonjsonlogger.jsonlogger.JsonFormatter',
'fmt': '%(levelname)s %(asctime)s %(message)s',
},
},
'handlers': {
'papertrail': {
'level': 'DEBUG',
'class': 'logging.handlers.SysLogHandler',
'formatter': 'json',
'address': ('logsN.papertrailapp.com', 12345)
},
...
},
'loggers': {
'papertrail': {
'handlers': ['papertrail'],
'level': 'ERROR',
'propagate': True,
},
...
/management/commands/logging_test.py
from django.core.management import BaseCommand
import django
import logging
# uncomment this line to make it work
#django.setup()
def test_logging():
logger = logging.getLogger('papertrail')
logger.error(
'TEST LOGGING'
)
class Command(BaseCommand):
def handle(self, *args, **options):
test_logging()
I can call the test_logging function with:
./manage.py test_logging
If I uncomment the line django.setup() it works. I have no idea why.
Have a look at the docs for
django.setup()and you'll see that one of the main roles of this function is to set up logging according to your settings. This portion of the docs it mentions a scenario where using django.setup() is required to utilize logging:I can't tell what your project does, but the test you are running seems to fall into the category of "standalone".
Looking into the source
django.setup()in part calls the followingAnd the source for that function does the following: