Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Log.php
Go to the documentation of this file.
1 <?php
7 
8 use Psr\Log\LoggerInterface;
9 use Exception;
10 
14 class Log implements DebuggerInterface
15 {
19  private $logger;
20 
26  public function __construct(LoggerInterface $logger)
27  {
28  $this->logger = $logger;
29  }
30 
34  public function success($requestUrl, $requestData, $responseStatus, $responseBody)
35  {
36  $requestInfo = $this->buildRequestInfo($requestUrl, $requestData);
37  $responseInfo = $this->buildResponseInfo($responseStatus, $responseBody);
38 
40  . $responseInfo;
41 
42  $this->writeToLog($info);
43  }
44 
48  public function failure($requestUrl, $requestData, Exception $exception)
49  {
50  $requestInfo = $this->buildRequestInfo($requestUrl, $requestData);
51  $exceptionInfo = $this->buildExceptionInfo($exception);
52 
54  . $exceptionInfo;
55 
56  $this->writeToLog($info);
57  }
58 
66  private function buildRequestInfo($requestUrl, $requestData)
67  {
68  $infoContent = $this->buildInfoSection('URL', $requestUrl)
69  . $this->buildInfoSection('Body', $requestData);
70 
71  $info = $this->buildInfoSection('Request', $infoContent);
72  return $info;
73  }
74 
82  private function buildResponseInfo($responseStatus, $responseBody)
83  {
84  $infoContent = $this->buildInfoSection('Status', $responseStatus)
85  . $this->buildInfoSection('Body', $responseBody);
86 
87  $info = $this->buildInfoSection('Response', $infoContent);
88  return $info;
89  }
90 
97  private function buildExceptionInfo(Exception $exception)
98  {
99  $infoContent = (string)$exception;
100  $info = $this->buildInfoSection('Exception', $infoContent);
101  return $info;
102  }
103 
110  private function writeToLog($info)
111  {
112  $logMessage = $this->buildInfoSection('Signifyd API integration debug info', $info);
113  $this->logger->debug($logMessage);
114  }
115 
123  private function buildInfoSection($title, $content)
124  {
125  $formattedInfo = $title . ":\n"
126  . $this->addIndent($content) . "\n";
127  return $formattedInfo;
128  }
129 
137  private function addIndent($content, $indent = ' ')
138  {
139  $contentLines = explode("\n", $content);
140  $contentLinesWithIndent = array_map(function ($line) use ($indent) {
141  return $indent . $line;
142  }, $contentLines);
143  $content = implode("\n", $contentLinesWithIndent);
144  return $content;
145  }
146 }
$title
Definition: default.phtml:14
failure($requestUrl, $requestData, Exception $exception)
Definition: Log.php:48
success($requestUrl, $requestData, $responseStatus, $responseBody)
Definition: Log.php:34
$logger
foreach($optionCollection as $option) $requestInfo
foreach( $_productCollection as $_product)() ?>" class $info
Definition: listing.phtml:52