AbstractModel.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\ImportExport\Model;
  7. use Magento\Framework\App\Filesystem\DirectoryList;
  8. /**
  9. * Operation abstract class
  10. *
  11. * @author Magento Core Team <core@magentocommerce.com>
  12. */
  13. abstract class AbstractModel extends \Magento\Framework\DataObject
  14. {
  15. /**
  16. * Enable logging
  17. *
  18. * @var bool
  19. */
  20. protected $_debugMode = false;
  21. /**
  22. * Fields that should be replaced in debug with '***'
  23. *
  24. * @var string[]
  25. */
  26. protected $_debugReplacePrivateDataKeys = [];
  27. /**
  28. * Contains all log information
  29. *
  30. * @var string[]
  31. */
  32. protected $_logTrace = [];
  33. /**
  34. * @var \Psr\Log\LoggerInterface
  35. */
  36. protected $_logger;
  37. /**
  38. * @var \Magento\Framework\Filesystem\Directory\WriteInterface
  39. */
  40. protected $_varDirectory;
  41. /**
  42. * @param \Psr\Log\LoggerInterface $logger
  43. * @param \Magento\Framework\Filesystem $filesystem
  44. * @param array $data
  45. */
  46. public function __construct(
  47. \Psr\Log\LoggerInterface $logger,
  48. \Magento\Framework\Filesystem $filesystem,
  49. array $data = []
  50. ) {
  51. $this->_logger = $logger;
  52. $this->_varDirectory = $filesystem->getDirectoryWrite(DirectoryList::VAR_DIR);
  53. parent::__construct($data);
  54. }
  55. /**
  56. * Log debug data to file.
  57. * Log file dir: var/log/import_export/%Y/%m/%d/%time%_%operation_type%_%entity_type%.log
  58. *
  59. * @param mixed $debugData
  60. * @return $this
  61. */
  62. public function addLogComment($debugData)
  63. {
  64. if (is_array($debugData)) {
  65. $this->_logTrace = array_merge($this->_logTrace, $debugData);
  66. } else {
  67. $this->_logTrace[] = $debugData;
  68. }
  69. if ($this->_debugMode) {
  70. $this->_logger->debug(var_export($debugData, true));
  71. }
  72. return $this;
  73. }
  74. /**
  75. * Return human readable debug trace.
  76. *
  77. * @return string
  78. */
  79. public function getFormatedLogTrace()
  80. {
  81. $trace = '';
  82. $lineNumber = 1;
  83. foreach ($this->_logTrace as &$info) {
  84. $trace .= $lineNumber++ . ': ' . $info . "\n";
  85. }
  86. return $trace;
  87. }
  88. }