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.

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