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.

105 lines
3.0 KiB

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