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.

82 lines
2.6 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. from odoo.service import wsgi_server
  5. from odoo.tools import config as odoo_config
  6. from . import const
  7. from .logutils import LoggerNameFilter, OdooSentryHandler
  8. import collections
  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. '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. if config.get('sentry_odoo_dir') and config.get('sentry_release'):
  36. _logger.debug('Both sentry_odoo_dir and sentry_release defined, choosing sentry_release')
  37. options = {
  38. 'release': config.get('sentry_release', get_odoo_commit(config.get('sentry_odoo_dir'))),
  39. }
  40. for option in const.get_sentry_options():
  41. value = config.get('sentry_%s' % option.key, option.default)
  42. if isinstance(option.converter, collections.Callable):
  43. value = option.converter(value)
  44. options[option.key] = value
  45. level = config.get('sentry_logging_level', const.DEFAULT_LOG_LEVEL)
  46. exclude_loggers = const.split_multiple(
  47. config.get('sentry_exclude_loggers', const.DEFAULT_EXCLUDE_LOGGERS)
  48. )
  49. if level not in const.LOG_LEVEL_MAP:
  50. level = const.DEFAULT_LOG_LEVEL
  51. client_cls = client_cls or raven.Client
  52. client = client_cls(**options)
  53. handler = OdooSentryHandler(
  54. config.get('sentry_include_context', True),
  55. client=client,
  56. level=const.LOG_LEVEL_MAP[level],
  57. )
  58. if exclude_loggers:
  59. handler.addFilter(LoggerNameFilter(
  60. exclude_loggers, name='sentry.logger.filter'))
  61. raven.conf.setup_logging(handler)
  62. wsgi_server.application = Sentry(
  63. wsgi_server.application, client=client)
  64. client.captureMessage('Starting Odoo Server')
  65. return client
  66. sentry_client = initialize_raven(odoo_config)