File.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\DB\Logger;
  7. use Magento\Framework\App\Filesystem\DirectoryList;
  8. use Magento\Framework\Filesystem;
  9. use Magento\Framework\Filesystem\Directory\WriteInterface;
  10. /**
  11. * Logging to file
  12. */
  13. class File extends LoggerAbstract
  14. {
  15. /**
  16. * @var WriteInterface
  17. */
  18. private $dir;
  19. /**
  20. * Path to SQL debug data log
  21. *
  22. * @var string
  23. */
  24. protected $debugFile;
  25. /**
  26. * @param Filesystem $filesystem
  27. * @param string $debugFile
  28. * @param bool $logAllQueries
  29. * @param float $logQueryTime
  30. * @param bool $logCallStack
  31. */
  32. public function __construct(
  33. Filesystem $filesystem,
  34. $debugFile = 'debug/db.log',
  35. $logAllQueries = false,
  36. $logQueryTime = 0.05,
  37. $logCallStack = false
  38. ) {
  39. parent::__construct($logAllQueries, $logQueryTime, $logCallStack);
  40. $this->dir = $filesystem->getDirectoryWrite(DirectoryList::VAR_DIR);
  41. $this->debugFile = $debugFile;
  42. }
  43. /**
  44. * {@inheritdoc}
  45. */
  46. public function log($str)
  47. {
  48. $str = '## ' . date('Y-m-d H:i:s') . "\r\n" . $str;
  49. $stream = $this->dir->openFile($this->debugFile, 'a');
  50. $stream->lock();
  51. $stream->write($str);
  52. $stream->unlock();
  53. $stream->close();
  54. }
  55. /**
  56. * {@inheritdoc}
  57. */
  58. public function logStats($type, $sql, $bind = [], $result = null)
  59. {
  60. $stats = $this->getStats($type, $sql, $bind, $result);
  61. if ($stats) {
  62. $this->log($stats);
  63. }
  64. }
  65. /**
  66. * {@inheritdoc}
  67. */
  68. public function critical(\Exception $e)
  69. {
  70. $this->log("EXCEPTION \n$e\n\n");
  71. }
  72. }