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.

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