Pro Yearly is on sale from $80 to $50! »

Logging Rethought 2: The Actions of Frank Taylor Jr. (DjangoCon Europe 2019)

Cd7648c536b4dbe940246b74044fbc52?s=47 Markus H
April 10, 2019

Logging Rethought 2: The Actions of Frank Taylor Jr. (DjangoCon Europe 2019)

My talk from DjangoCon Europe 2019

A writeup of the talk is available at https://markusholtermann.eu/2019/04/logging-rethought/

Cd7648c536b4dbe940246b74044fbc52?s=128

Markus H

April 10, 2019
Tweet

Transcript

  1. @m_holtermann Logging Rethought 2 The Actions of Frank Taylor Jr.

    markusholtermann.eu • gitlab.com/MarkusH • github.com/MarkusH • @m_holtermann
  2. @m_holtermann Hi, I’m Markus Holtermann • Engineer at • Django

    Core Contributor
  3. @m_holtermann

  4. @m_holtermann

  5. @m_holtermann import logging logger = logging.getLogger(__name__) logger.error( "Login failed because

    the " "connection to the authentication " "provider %s timed out.", provider_name )
  6. @m_holtermann [2019-04-01T10:47:04.139+00:00] [ERROR] [srv01] Login failed because the connection to

    the authentication provider google timed out.
  7. @m_holtermann

  8. @m_holtermann

  9. @m_holtermann

  10. @m_holtermann

  11. @m_holtermann

  12. @m_holtermann

  13. @m_holtermann

  14. @m_holtermann import structlog logger = structlog.get_logger("structlog") logger.error( "auth_provider_failed", provider_name=provider_name, provider_ip=provider_ip,

    timeout=timeout, )
  15. @m_holtermann 2019-04-01 10:47:04.139 [error ] auth_provider_failed provider_ip='8.8.8.8' provider_name='google' server='srv01' timeout=5

  16. @m_holtermann { "event": "auth_provider_failed", "timestamp": 1554115624.139, "level": "error", "provider_name": "google",

    "provider_ip": "8.8.8.8", "timeout": 5 }
  17. @m_holtermann

  18. @m_holtermann

  19. @m_holtermann

  20. @m_holtermann

  21. @m_holtermann

  22. @m_holtermann import uuid, structlog logger = structlog.get_logger("structlog") def structlog_middleware(get_response): def

    _inner(request): request.trace_id = ( request.META.get("HTTP_X_TRACE_ID") or str(uuid.uuid4()) ) log = logger.new(trace_id=request.trace_id) return get_response(request) return _inner
  23. @m_holtermann import structlog logger = structlog.get_logger("structlog") def structlog_user_middleware(get_response): def _inner(request):

    if request.user.is_authenticated: logger.bind(user_id=request.user.pk) return get_response(request) return _inner
  24. @m_holtermann

  25. @m_holtermann

  26. @m_holtermann

  27. @m_holtermann

  28. @m_holtermann # DO logger.error("some_event", foo="bar", lorem="ipsum") # DON'T DO logger.error("some_event",

    **some_object.__dict__)
  29. @m_holtermann

  30. @m_holtermann

  31. @m_holtermann

  32. @m_holtermann

  33. @m_holtermann

  34. @m_holtermann

  35. @m_holtermann

  36. @m_holtermann

  37. @m_holtermann

  38. @m_holtermann

  39. @m_holtermann Thank you!

  40. @m_holtermann