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.

78 lines
2.4 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. from odoo.service import wsgi_server
  6. from odoo.tools import config as odoo_config
  7. from . import const
  8. from .logutils import LoggerNameFilter, OdooSentryHandler
  9. _logger = logging.getLogger(__name__)
  10. HAS_RAVEN = True
  11. try:
  12. import raven
  13. from raven.middleware import Sentry
  14. except ImportError:
  15. HAS_RAVEN = False
  16. _logger.debug('Cannot import "raven". Please make sure it is installed.')
  17. def get_odoo_commit(odoo_dir):
  18. '''Attempts to get Odoo git commit from :param:`odoo_dir`.'''
  19. if not odoo_dir:
  20. return
  21. try:
  22. return raven.fetch_git_sha(odoo_dir)
  23. except raven.exceptions.InvalidGitRepository:
  24. _logger.debug(
  25. u'Odoo directory: "%s" not a valid git repository', odoo_dir)
  26. def initialize_raven(config, client_cls=None):
  27. '''
  28. Setup an instance of :class:`raven.Client`.
  29. :param config: Sentry configuration
  30. :param client: class used to instantiate the raven client.
  31. '''
  32. enabled = config.get('sentry_enabled', False)
  33. if not (HAS_RAVEN and enabled):
  34. return
  35. options = {
  36. 'release': get_odoo_commit(config.get('sentry_odoo_dir')),
  37. }
  38. for option in const.get_sentry_options():
  39. value = config.get('sentry_%s' % option.key, option.default)
  40. if callable(option.converter):
  41. value = option.converter(value)
  42. options[option.key] = value
  43. level = config.get('sentry_logging_level', const.DEFAULT_LOG_LEVEL)
  44. exclude_loggers = const.split_multiple(
  45. config.get('sentry_exclude_loggers', const.DEFAULT_EXCLUDE_LOGGERS)
  46. )
  47. if level not in const.LOG_LEVEL_MAP:
  48. level = const.DEFAULT_LOG_LEVEL
  49. client_cls = client_cls or raven.Client
  50. client = client_cls(**options)
  51. handler = OdooSentryHandler(
  52. config.get('sentry_include_context', True),
  53. client=client,
  54. level=const.LOG_LEVEL_MAP[level],
  55. )
  56. if exclude_loggers:
  57. handler.addFilter(LoggerNameFilter(
  58. exclude_loggers, name='sentry.logger.filter'))
  59. raven.conf.setup_logging(handler)
  60. wsgi_server.application = Sentry(
  61. wsgi_server.application, client=client)
  62. client.captureMessage('Starting Odoo Server')
  63. return client
  64. sentry_client = initialize_raven(odoo_config)