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.

41 lines
1.4 KiB

  1. # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
  2. # Copyright (c) ACSONE SA/NV 2019
  3. import logging
  4. import os
  5. import time
  6. import psycopg2
  7. from odoo import sql_db
  8. from odoo.tools import config
  9. _logger = logging.getLogger(__name__)
  10. # TODO Odoo option? server_environment?
  11. LOG_MIN_DURATION_STATEMENT = int(config.get("log_min_duration_statement", "-1"))
  12. ENV_VAR = "ODOO_LOG_MIN_DURATION_STATEMENT"
  13. if ENV_VAR in os.environ:
  14. LOG_MIN_DURATION_STATEMENT = int(os.environ.get(ENV_VAR, "-1"))
  15. class SlowStatementLoggingCursor(sql_db.Cursor):
  16. def execute(self, query, params=None, log_exceptions=None):
  17. if LOG_MIN_DURATION_STATEMENT >= 0:
  18. start = time.perf_counter()
  19. res = super().execute(query, params, log_exceptions)
  20. duration = (time.perf_counter() - start) * 1000.0
  21. if duration >= LOG_MIN_DURATION_STATEMENT:
  22. # same logging technique as Odoo in sql_log mode
  23. encoding = psycopg2.extensions.encodings[self.connection.encoding]
  24. _logger.debug(
  25. "duration: %.3f ms statement: %s",
  26. duration,
  27. self._obj.mogrify(query, params).decode(encoding, "replace"),
  28. )
  29. return res
  30. else:
  31. return super().execute(query, params, log_exceptions)
  32. sql_db.Cursor = SlowStatementLoggingCursor