QueryLogger.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  5. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  6. *
  7. * Licensed under The MIT License
  8. * For full copyright and license information, please see the LICENSE.txt
  9. * Redistributions of files must retain the above copyright notice.
  10. *
  11. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  12. * @link https://cakephp.org CakePHP(tm) Project
  13. * @since 3.0.0
  14. * @license https://opensource.org/licenses/mit-license.php MIT License
  15. */
  16. namespace Cake\Database\Log;
  17. use Cake\Log\Engine\BaseLog;
  18. use Cake\Log\Log;
  19. /**
  20. * This class is a bridge used to write LoggedQuery objects into a real log.
  21. * by default this class use the built-in CakePHP Log class to accomplish this
  22. *
  23. * @internal
  24. */
  25. class QueryLogger extends BaseLog
  26. {
  27. /**
  28. * Constructor.
  29. *
  30. * @param array<string, mixed> $config Configuration array
  31. */
  32. public function __construct(array $config = [])
  33. {
  34. $this->_defaultConfig['scopes'] = ['queriesLog'];
  35. $this->_defaultConfig['connection'] = '';
  36. parent::__construct($config);
  37. }
  38. /**
  39. * @inheritDoc
  40. */
  41. public function log($level, $message, array $context = [])
  42. {
  43. $context['scope'] = $this->scopes() ?: ['queriesLog'];
  44. $context['connection'] = $this->getConfig('connection');
  45. if ($context['query'] instanceof LoggedQuery) {
  46. $context = $context['query']->getContext() + $context;
  47. $message = 'connection={connection} role={role} duration={took} rows={numRows} ' . $message;
  48. }
  49. Log::write('debug', $message, $context);
  50. }
  51. }