Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Logging in Django

Logging in Django

Jakh Daven

March 21, 2013
Tweet

More Decks by Jakh Daven

Other Decks in Programming

Transcript

  1. Why bother • Archive of events • Hints for debugging

    • Reproducing errors • Statistical Analysis
  2. To log or not to log • Convenient message •

    State variables • logger.debug(“details, details”) • logger.info(“Something wonderful is about to happen...”) • logger.warn(“I have a bad feeling about this”) • logger.error(“oops!”) • logger.critical(“oh no!”)
  3. Teh codez from django.utils.log import getLogger logger = getLogger('django.request') if

    request.method not in request_method_list: logger.warning('Method Not Allowed (%s): %s', request. method, request.path, extra={ 'status_code': 405, 'request': request } )
  4. Log levels • debug • info • warning • error

    • critical Other methods: • logger.log • logger.exception
  5. Configuration - Dictconfig { 'version': 1, 'disable_existing_loggers': False, 'formatters': {

    'standard': { 'format': '%(asctime)s [%(levelname)s] %(name)s: %(message)s' }, }, 'handlers': { 'default': { 'level':'INFO', 'class':'logging.StreamHandler', }, }, 'loggers': { '': { 'handlers': ['default'], 'level': 'INFO', 'propagate': True }, 'django.request': { 'handlers': ['default'], 'level': 'WARN', 'propagate': False }, } }
  6. Configuration - Dictconfig formatters: 'formatters': { 'verbose': { 'format': '%(levelname)s

    %(asctime)s %(module)s % (process)d %(thread)d %(message)s' }, 'simple': { 'format': '%(levelname)s %(message)s' }, }, asctime - 'Sun Jun 20 23:21:05 1993'.
  7. Configuration - Dictconfig filters: 'filters': { 'require_debug_false': { '()': 'django.utils.log.RequireDebugFalse',

    } }, 'handlers': { 'mail_admins': { 'level': 'ERROR', 'filters': ['require_debug_false'], 'class': 'django.utils.log.AdminEmailHandler' } }, class RequireDebugFalse(logging.Filter): def filter(self, record): return not settings.DEBUG
  8. Configuration - Dictconfig handlers: 'handlers': { 'null': { 'level': 'DEBUG',

    'class': 'django.utils.log.NullHandler', }, 'console':{ 'level': 'DEBUG', 'class': 'logging.StreamHandler', 'formatter': 'simple' }, 'mail_admins': { 'level': 'ERROR', 'class': 'django.utils.log.AdminEmailHandler', 'filters': ['special'] } },
  9. Configuration - Dictconfig loggers: 'loggers': { 'django': { 'handlers': ['null'],

    'level': 'INFO', }, 'django.request': { 'handlers': ['mail_admins'], 'level': 'ERROR', 'propagate': False, }, 'foo.bar: { 'handlers': ['console', 'mail_admins'], 'level': 'INFO', 'filters': ['special'] } }
  10. Common uses - debug sql 'handlers': { 'console': { 'level':

    'DEBUG', 'class': 'logging.StreamHandler' } }, 'loggers': { 'django.db.backends': { 'handlers': ['console'], 'level': 'DEBUG', 'propagate': True, }, }
  11. Common uses - filter sql 'filters': { 'sql_inserts': { '()':

    'django.utils.log.CallbackFilter', 'callback': lambda x: 'INSERT' in x.msg } }, 'loggers': { 'django.db.backends': { 'handlers': ['console'], 'level': 'DEBUG', 'propagate': True, 'filters': ['sql_inserts'], }, }
  12. Gotchas Circular import in Django < 1.5 settings.py: ... 'handlers':

    { 'default': { 'level':'INFO', 'class':'foo.bar.FooBarHandler', }, }, ... foo/bar.py: from django.conf import settings class FooBarHandler(object): ...
  13. Gotchas Exception handler leaks secrets from django.views.decorators.debug import sensitive_variables, sensitive_post_parameters

    @sensitive_post_parameters('password', 'credit_card_number') @sensitive_variables('password', 'credit_card_number') def my_super_secret_view(request): password = request.POST['password'] credit_card_number = request.POST['credit_card_number'] ... ---------- DEFAULT_EXCEPTION_REPORTER_FILTER = 'path.to.your.CustomExceptionReporterFilter' SafeExceptionReporterFilter HIDDEN_SETTINGS = re.compile ('API|TOKEN|KEY|SECRET|PASS|PROFANITIES_LIST|SIGNATURE')