You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

106 lines
3.0 KiB

  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 urllib.parse
  6. import odoo.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 = urllib.parse.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 = odoo.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. KEYS = FIELDS = frozenset([
  84. 'session_id',
  85. ])