Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable log messages from the Requests library in Django

I seen How do I disable log messages from the Requests library? and I was wondering, how could i use django to manage these settings? Essentially, the problem is the Requests library is overly verbose and I want to tune it's logging level to warning or even error.

Our logs are FILLED with these:

INFO - requests.packages.urllib3.connectionpool (_new_conn:735) 
        Starting new HTTPS connection (1): www.example.com
INFO - requests.packages.urllib3.connectionpool (_new_conn:735) 
        Starting new HTTPS connection (1): localhost

Here is my current Django logging configuration with what I would think would be the fix to the library being overly verbose. But it isn't working.

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'verbose': {
            'format': ' '.join(['%(asctime)s', '%(levelname)8s', '- %(name)s',
                                '(%(funcName)s:%(lineno)d)', os.sep, ' ' * 4, '%(message)s']),
            'datefmt': DATEFORMAT,
        },
        'default': {
            'format': ' '.join(['%(asctime)s', '%(levelname)8s', '- %(message)s']),
            'datefmt': DATEFORMAT,
        },
        'syslog': {
            'format': ' '.join([PROCTITLE + '[%(process)d]', '[%(levelname)s]', '(%(module)s %(funcName)s:%(lineno)d)',
                                '- %(message)s']),
            'datefmt': DATEFORMAT,
        },
    },
    'filters': {
        'require_debug_false': {
            '()': 'django.utils.log.RequireDebugFalse'
        }
    },
    'handlers': {
        'null': {
            'level': HANDLER_LEVEL,
            'class': 'django.utils.log.NullHandler',
        },
        'mail_admins': {
            'level': 'ERROR',
            'filters': ['require_debug_false'],
            'class': 'django.utils.log.AdminEmailHandler',
        },
        'console_handler': {
            'level': parser.get('logging', 'handler.console.level'),
            'formatter': 'verbose',
            'class': 'ppcc.lib.logs.TwistedStreamHandler',
        },
        'default': {
            'level': HANDLER_LEVEL,
            'formatter': 'syslog',
            'class': 'logging.handlers.SysLogHandler',
            'address': '/dev/log',
            'facility': 20,
        },
    },
    'loggers': {
        'django': {
            'handlers': ['default'],
            'propagate': True,
            'level': parser.get_log_level('django'),
        },
        'django.request': {
            'handlers': ['mail_admins', 'default'],
            'level': 'ERROR',
            'propagate': False,
        },
        'boto': {
            'handlers': DEFAULT_HANDLERS,
            'level': 'WARNING',
        },
        'requests': {
            # The requests library is too verbose in it's logging, reducing the verbosity in our logs.
            'handlers': DEFAULT_HANDLERS,
            'level': 'WARNING',
            'propagate': True,
        },
        'urllib3': {
            'handers': DEFAULT_HANDLERS,
            'level': 'WARNING',
            'propagate': True
        },
        'cassandra': {
            'handlers': DEFAULT_HANDLERS,
            'level': 'WARNING',
        },
        'cqlengine': {
            'handlers': DEFAULT_HANDLERS,
            'propagate': True,
            'level': 'WARNING',
        },
        '': {
            'handlers': DEFAULT_HANDLERS,
            'level': LOG_LEVEL,
        },
    }
}
like image 450
Jared Mackey Avatar asked Jan 13 '16 22:01

Jared Mackey


2 Answers

I believe the following would work:

import logging
logging.getLogger('requests.packages.urllib3.connectionpool').setLevel(logging.ERROR)

You can retrieve the existing logger and manipulate it. I similarly had a mouthy submodule, and another option just to shut it up entirely is to set propagate = False on the logger.

like image 151
jimjkelly Avatar answered Nov 10 '22 04:11

jimjkelly


The correct solution was what I had thought it was, just adding it to the LOGGING setting in `settings.py'

    'requests': {
        # The requests library is too verbose in it's logging, reducing the verbosity in our logs.
        'handlers': DEFAULT_HANDLERS,
        'level': 'WARNING',
    },
like image 38
Jared Mackey Avatar answered Nov 10 '22 06:11

Jared Mackey