logutils.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2016-2017 Versada <https://versada.eu/>
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  4. import logging
  5. import urlparse
  6. import openerp.http
  7. _logger = logging.getLogger(__name__)
  8. try:
  9. from raven.handlers.logging import SentryHandler
  10. from raven.processors import SanitizePasswordsProcessor
  11. from raven.utils.wsgi import get_environ, get_headers
  12. except ImportError:
  13. _logger.debug('Cannot import "raven". Please make sure it is installed.')
  14. SentryHandler = object
  15. SanitizePasswordsProcessor = object
  16. def get_request_info(request):
  17. '''
  18. Returns context data extracted from :param:`request`.
  19. Heavily based on flask integration for Sentry: https://git.io/vP4i9.
  20. '''
  21. urlparts = urlparse.urlsplit(request.url)
  22. return {
  23. 'url': '%s://%s%s' % (urlparts.scheme, urlparts.netloc, urlparts.path),
  24. 'query_string': urlparts.query,
  25. 'method': request.method,
  26. 'headers': dict(get_headers(request.environ)),
  27. 'env': dict(get_environ(request.environ)),
  28. }
  29. def get_extra_context():
  30. '''
  31. Extracts additional context from the current request (if such is set).
  32. '''
  33. request = openerp.http.request
  34. try:
  35. session = getattr(request, 'session', {})
  36. except RuntimeError:
  37. ctx = {}
  38. else:
  39. ctx = {
  40. 'tags': {
  41. 'database': session.get('db', None),
  42. },
  43. 'user': {
  44. 'login': session.get('login', None),
  45. 'uid': session.get('uid', None),
  46. },
  47. 'extra': {
  48. 'context': session.get('context', {}),
  49. },
  50. }
  51. if request.httprequest:
  52. ctx.update({
  53. 'request': get_request_info(request.httprequest),
  54. })
  55. return ctx
  56. class LoggerNameFilter(logging.Filter):
  57. '''
  58. Custom :class:`logging.Filter` which allows to filter loggers by name.
  59. '''
  60. def __init__(self, loggers, name=''):
  61. super(LoggerNameFilter, self).__init__(name=name)
  62. self._exclude_loggers = set(loggers)
  63. def filter(self, event):
  64. return event.name not in self._exclude_loggers
  65. class OdooSentryHandler(SentryHandler):
  66. '''
  67. Customized :class:`raven.handlers.logging.SentryHandler`.
  68. Allows to add additional Odoo and HTTP request data to the event which is
  69. sent to Sentry.
  70. '''
  71. def __init__(self, include_extra_context, *args, **kwargs):
  72. super(OdooSentryHandler, self).__init__(*args, **kwargs)
  73. self.include_extra_context = include_extra_context
  74. def emit(self, record):
  75. if self.include_extra_context:
  76. self.client.context.merge(get_extra_context())
  77. return super(OdooSentryHandler, self).emit(record)
  78. class SanitizeOdooCookiesProcessor(SanitizePasswordsProcessor):
  79. '''
  80. Custom :class:`raven.processors.Processor`.
  81. Allows to sanitize sensitive Odoo cookies, namely the "session_id" cookie.
  82. '''
  83. # `FIELDS` was renamed to `KEYS` in raven 6.4.0.
  84. # Keep `FIELDS` for backwards compatibility.
  85. # See also issue #1096 on OCA/server-tools.
  86. KEYS = FIELDS = frozenset([
  87. 'session_id',
  88. ])