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.

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