Logger.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. /**
  3. * This file is part of the Klarna Core module
  4. *
  5. * (c) Klarna Bank AB (publ)
  6. *
  7. * For the full copyright and license information, please view the NOTICE
  8. * and LICENSE files that were distributed with this source code.
  9. */
  10. namespace Klarna\Core\Logger;
  11. use Magento\Framework\App\Config\ScopeConfigInterface;
  12. use Magento\Store\Model\ScopeInterface;
  13. use Magento\Store\Model\StoreManagerInterface;
  14. use Monolog\Logger as MonoLogger;
  15. /**
  16. * Class Logger
  17. *
  18. * @package Klarna\Core\Logger
  19. */
  20. class Logger extends MonoLogger
  21. {
  22. /**
  23. * @var ScopeConfigInterface
  24. */
  25. private $config;
  26. /**
  27. * @var StoreManagerInterface
  28. */
  29. private $storeManager;
  30. /**
  31. * @var Cleanser
  32. */
  33. private $cleanser;
  34. /**
  35. * Logger constructor.
  36. *
  37. * @param string $name
  38. * @param ScopeConfigInterface $config
  39. * @param Cleanser $cleanser
  40. * @param StoreManagerInterface $storeManager
  41. * @param \Monolog\Handler\HandlerInterface[] $handlers
  42. * @param \callable[] $processors
  43. * @codeCoverageIgnore
  44. */
  45. public function __construct(
  46. $name,
  47. ScopeConfigInterface $config,
  48. Cleanser $cleanser,
  49. StoreManagerInterface $storeManager,
  50. array $handlers = [],
  51. array $processors = []
  52. ) {
  53. parent::__construct($name, $handlers, $processors);
  54. $this->config = $config;
  55. $this->cleanser = $cleanser;
  56. $this->storeManager = $storeManager;
  57. }
  58. /**
  59. * @inheritdoc
  60. */
  61. public function addRecord($level, $message, array $context = [])
  62. {
  63. $store = $this->storeManager->getStore();
  64. if (!$this->config->isSetFlag(
  65. 'klarna/api/debug',
  66. ScopeInterface::SCOPE_STORE,
  67. $store
  68. )
  69. ) {
  70. return false;
  71. }
  72. if (is_string($message) || null === $message) {
  73. return parent::addRecord($level, $message, $context);
  74. }
  75. if (!$this->config->isSetFlag('klarna/api/test_mode', ScopeInterface::SCOPE_STORE, $store)) {
  76. // We only need to "clean" the log data if Live
  77. $message = $this->cleanser->checkForSensitiveData($message);
  78. }
  79. if (is_array($message)) {
  80. $message = print_r($message, true);
  81. }
  82. return parent::addRecord($level, $message, $context);
  83. }
  84. }