Skip to content

Commit 0854f98

Browse files
committed
MAGETWO-90327: Dedicated debug logging files for Payment and Shipping activity
1 parent b18ce4a commit 0854f98

File tree

14 files changed

+891
-473
lines changed

14 files changed

+891
-473
lines changed

app/code/Magento/Dhl/Model/Carrier.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1533,7 +1533,7 @@ protected function _doRequest()
15331533

15341534
$responseBody = $this->_getCachedQuotes($request);
15351535
if ($responseBody === null) {
1536-
$debugData = ['request' => $request];
1536+
$debugData = ['request' => $this->filterDebugData($request)];
15371537
try {
15381538
/** @var \Magento\Framework\HTTP\ZendClient $client */
15391539
$client = $this->_httpClientFactory->create();
@@ -1542,7 +1542,7 @@ protected function _doRequest()
15421542
$client->setRawData($request);
15431543
$responseBody = $client->request(\Magento\Framework\HTTP\ZendClient::POST)->getBody();
15441544
$responseBody = utf8_decode($responseBody);
1545-
$debugData['result'] = $responseBody;
1545+
$debugData['result'] = $this->filterDebugData($responseBody);
15461546
$this->_setCachedQuotes($request, $responseBody);
15471547
} catch (\Exception $e) {
15481548
$this->_errors[$e->getCode()] = $e->getMessage();
@@ -1727,15 +1727,15 @@ protected function _getXMLTracking($trackings)
17271727

17281728
$responseBody = $this->_getCachedQuotes($request);
17291729
if ($responseBody === null) {
1730-
$debugData = ['request' => $request];
1730+
$debugData = ['request' => $this->filterDebugData($request)];
17311731
try {
17321732
/** @var \Magento\Framework\HTTP\ZendClient $client */
17331733
$client = $this->_httpClientFactory->create();
17341734
$client->setUri((string)$this->getConfigData('gateway_url'));
17351735
$client->setConfig(['maxredirects' => 0, 'timeout' => 30]);
17361736
$client->setRawData($request);
17371737
$responseBody = $client->request(\Magento\Framework\HTTP\ZendClient::POST)->getBody();
1738-
$debugData['result'] = $responseBody;
1738+
$debugData['result'] = $this->filterDebugData($responseBody);
17391739
$this->_setCachedQuotes($request, $responseBody);
17401740
} catch (\Exception $e) {
17411741
$this->_errors[$e->getCode()] = $e->getMessage();

app/code/Magento/Dhl/Test/Unit/Model/CarrierTest.php

Lines changed: 256 additions & 146 deletions
Large diffs are not rendered by default.

app/code/Magento/Fedex/Model/Carrier.php

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -716,7 +716,7 @@ protected function _getXmlQuotes()
716716

717717
$responseBody = $this->_getCachedQuotes($request);
718718
if ($responseBody === null) {
719-
$debugData = ['request' => $request];
719+
$debugData = ['request' => $this->filterDebugData($request)];
720720
try {
721721
$url = $this->getConfigData('gateway_url');
722722
if (!$url) {
@@ -731,7 +731,7 @@ protected function _getXmlQuotes()
731731
$responseBody = curl_exec($ch);
732732
curl_close($ch);
733733

734-
$debugData['result'] = $responseBody;
734+
$debugData['result'] = $this->filterDebugData($responseBody);
735735
$this->_setCachedQuotes($request, $responseBody);
736736
} catch (\Exception $e) {
737737
$debugData['result'] = ['error' => $e->getMessage(), 'code' => $e->getCode()];
@@ -1071,7 +1071,7 @@ protected function _getXMLTracking($tracking)
10711071
];
10721072
$requestString = $this->serializer->serialize($trackRequest);
10731073
$response = $this->_getCachedQuotes($requestString);
1074-
$debugData = ['request' => $trackRequest];
1074+
$debugData = ['request' => $this->filterDebugData($trackRequest)];
10751075
if ($response === null) {
10761076
try {
10771077
$client = $this->_createTrackSoapClient();
@@ -1398,6 +1398,7 @@ protected function _doShipmentRequest(\Magento\Framework\DataObject $request)
13981398
$result = new \Magento\Framework\DataObject();
13991399
$client = $this->_createShipSoapClient();
14001400
$requestClient = $this->_formShipmentRequest($request);
1401+
$debugData['request'] = $this->filterDebugData($requestClient);
14011402
$response = $client->processShipment($requestClient);
14021403

14031404
if ($response->HighestSeverity != 'FAILURE' && $response->HighestSeverity != 'ERROR') {
@@ -1407,13 +1408,10 @@ protected function _doShipmentRequest(\Magento\Framework\DataObject $request)
14071408
);
14081409
$result->setShippingLabelContent($shippingLabelContent);
14091410
$result->setTrackingNumber($trackingNumber);
1410-
$debugData = ['request' => $client->__getLastRequest(), 'result' => $client->__getLastResponse()];
1411+
$debugData['result'] = $client->__getLastResponse();
14111412
$this->_debug($debugData);
14121413
} else {
1413-
$debugData = [
1414-
'request' => $client->__getLastRequest(),
1415-
'result' => ['error' => '', 'code' => '', 'xml' => $client->__getLastResponse()],
1416-
];
1414+
$debugData['result'] = ['error' => '', 'code' => '', 'xml' => $client->__getLastResponse()];
14171415
if (is_array($response->Notifications)) {
14181416
foreach ($response->Notifications as $notification) {
14191417
$debugData['result']['code'] .= $notification->Code . '; ';

app/code/Magento/Fedex/Test/Unit/Model/CarrierTest.php

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Magento\Framework\App\Config\ScopeConfigInterface;
1717
use Magento\Framework\Module\Dir\Reader;
1818
use Magento\Framework\Pricing\PriceCurrencyInterface;
19+
use Magento\Framework\Serialize\Serializer\Json;
1920
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
2021
use Magento\Framework\Xml\Security;
2122
use Magento\Quote\Model\Quote\Address\RateRequest;
@@ -87,13 +88,18 @@ class CarrierTest extends \PHPUnit\Framework\TestCase
8788
/**
8889
* @var \SoapClient|MockObject
8990
*/
90-
private $soapClientMock;
91+
private $soapClient;
9192

9293
/**
93-
* @var \Magento\Framework\Serialize\Serializer\Json|MockObject
94+
* @var Json|MockObject
9495
*/
9596
private $serializer;
9697

98+
/**
99+
* @var LoggerInterface|MockObject
100+
*/
101+
private $logger;
102+
97103
protected function setUp()
98104
{
99105
$this->helper = new ObjectManager($this);
@@ -151,17 +157,19 @@ protected function setUp()
151157
->disableOriginalConstructor()
152158
->getMock();
153159

154-
$this->serializer = $this->getMockBuilder(\Magento\Framework\Serialize\Serializer\Json::class)
160+
$this->serializer = $this->getMockBuilder(Json::class)
155161
->disableOriginalConstructor()
156162
->getMock();
157163

164+
$this->logger = $this->getMockForAbstractClass(LoggerInterface::class);
165+
158166
$this->carrier = $this->getMockBuilder(Carrier::class)
159167
->setMethods(['_createSoapClient'])
160168
->setConstructorArgs(
161169
[
162170
'scopeConfig' => $this->scope,
163171
'rateErrorFactory' => $this->errorFactory,
164-
'logger' => $this->createMock(LoggerInterface::class),
172+
'logger' => $this->logger,
165173
'xmlSecurity' => new Security(),
166174
'xmlElFactory' => $elementFactory,
167175
'rateFactory' => $rateFactory,
@@ -181,12 +189,12 @@ protected function setUp()
181189
'serializer' => $this->serializer,
182190
]
183191
)->getMock();
184-
$this->soapClientMock = $this->getMockBuilder(\SoapClient::class)
192+
$this->soapClient = $this->getMockBuilder(\SoapClient::class)
185193
->disableOriginalConstructor()
186194
->setMethods(['getRates', 'track'])
187195
->getMock();
188196
$this->carrier->method('_createSoapClient')
189-
->willReturn($this->soapClientMock);
197+
->willReturn($this->soapClient);
190198
}
191199

192200
public function testSetRequestWithoutCity()
@@ -214,21 +222,19 @@ public function testSetRequestWithCity()
214222
}
215223

216224
/**
217-
* Callback function, emulates getValue function
218-
* @param $path
219-
* @return null|string
225+
* Callback function, emulates getValue function.
226+
*
227+
* @param string $path
228+
* @return string|null
220229
*/
221-
public function scopeConfigGetValue($path)
230+
public function scopeConfigGetValue(string $path)
222231
{
223-
switch ($path) {
224-
case 'carriers/fedex/showmethod':
225-
return 1;
226-
break;
227-
case 'carriers/fedex/allowed_methods':
228-
return 'ServiceType';
229-
break;
230-
}
231-
return null;
232+
$pathMap = [
233+
'carriers/fedex/showmethod' => 1,
234+
'carriers/fedex/allowed_methods' => 'ServiceType',
235+
'carriers/fedex/debug' => 1,
236+
];
237+
return isset($pathMap[$path]) ? $pathMap[$path] : null;
232238
}
233239

234240
/**
@@ -271,7 +277,7 @@ public function testCollectRatesRateAmountOriginBased($amount, $rateType, $expec
271277
->disableOriginalConstructor()
272278
->getMock();
273279

274-
$this->soapClientMock->expects($this->exactly($callNum))
280+
$this->soapClient->expects($this->exactly($callNum))
275281
->method('getRates')
276282
->willReturn($response);
277283

@@ -447,7 +453,7 @@ public function testGetTracking($tracking, $shipTimeStamp, $expectedDate, $expec
447453
$response->CompletedTrackDetails->TrackDetails = [$trackDetails];
448454
// @codingStandardsIgnoreEnd
449455

450-
$this->soapClientMock->expects($this->exactly($callNum))
456+
$this->soapClient->expects($this->exactly($callNum))
451457
->method('track')
452458
->willReturn($response);
453459

@@ -570,7 +576,7 @@ public function testGetTrackingWithEvents($tracking, $shipTimeStamp, $expectedDa
570576
$response->CompletedTrackDetails->TrackDetails = $trackDetails;
571577
// @codingStandardsIgnoreEnd
572578

573-
$this->soapClientMock->expects($this->exactly($callNum))
579+
$this->soapClient->expects($this->exactly($callNum))
574580
->method('track')
575581
->willReturn($response);
576582

app/code/Magento/Payment/Model/Method/Logger.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,11 @@
55
*/
66
namespace Magento\Payment\Model\Method;
77

8+
use Magento\Payment\Gateway\ConfigInterface;
89
use Psr\Log\LoggerInterface;
910

1011
/**
11-
* Class Logger for payment related information (request, response, etc.) which is used for debug
12-
*
13-
* @author Magento Core Team <core@magentocommerce.com>
12+
* Class Logger for payment related information (request, response, etc.) which is used for debug.
1413
*
1514
* @api
1615
* @since 100.0.2
@@ -25,17 +24,17 @@ class Logger
2524
protected $logger;
2625

2726
/**
28-
* @var \Magento\Payment\Gateway\ConfigInterface
27+
* @var ConfigInterface
2928
*/
3029
private $config;
3130

3231
/**
3332
* @param LoggerInterface $logger
34-
* @param \Magento\Payment\Gateway\ConfigInterface $config
33+
* @param ConfigInterface|null $config
3534
*/
3635
public function __construct(
3736
LoggerInterface $logger,
38-
\Magento\Payment\Gateway\ConfigInterface $config = null
37+
ConfigInterface $config = null
3938
) {
4039
$this->logger = $logger;
4140
$this->config = $config;
@@ -69,7 +68,7 @@ public function debug(array $data, array $maskKeys = null, $forceDebug = null)
6968
*/
7069
private function getDebugReplaceFields()
7170
{
72-
if ($this->config and $this->config->getValue('debugReplaceKeys')) {
71+
if ($this->config && $this->config->getValue('debugReplaceKeys')) {
7372
return explode(',', $this->config->getValue('debugReplaceKeys'));
7473
}
7574
return [];
@@ -82,7 +81,7 @@ private function getDebugReplaceFields()
8281
*/
8382
private function isDebugOn()
8483
{
85-
return $this->config and (bool)$this->config->getValue('debug');
84+
return $this->config && (bool)$this->config->getValue('debug');
8685
}
8786

8887
/**

app/code/Magento/Payment/etc/di.xml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,22 @@
3636
<argument name="config" xsi:type="object">Magento\Payment\Gateway\Config\Config</argument>
3737
</arguments>
3838
</type>
39+
40+
<virtualType name="Magento\Payment\Model\Method\VirtualDebug" type="Magento\Framework\Logger\Handler\Base">
41+
<arguments>
42+
<argument name="fileName" xsi:type="string">/var/log/payment.log</argument>
43+
</arguments>
44+
</virtualType>
45+
<virtualType name="Magento\Payment\Model\Method\VirtualLogger" type="Magento\Framework\Logger\Monolog">
46+
<arguments>
47+
<argument name="handlers" xsi:type="array">
48+
<item name="debug" xsi:type="object">Magento\Payment\Model\Method\VirtualDebug</item>
49+
</argument>
50+
</arguments>
51+
</virtualType>
52+
<type name="Magento\Payment\Model\Method\Logger">
53+
<arguments>
54+
<argument name="logger" xsi:type="object">Magento\Payment\Model\Method\VirtualLogger</argument>
55+
</arguments>
56+
</type>
3957
</config>

app/code/Magento/Shipping/etc/di.xml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,22 @@
99
<preference for="Magento\Quote\Model\Quote\Address\RateCollectorInterface" type="Magento\Shipping\Model\Shipping" />
1010
<preference for="Magento\Shipping\Model\CarrierFactoryInterface" type="Magento\Shipping\Model\CarrierFactory" />
1111
<preference for="Magento\Shipping\Model\Carrier\Source\GenericInterface" type="Magento\Shipping\Model\Carrier\Source\GenericDefault" />
12+
13+
<virtualType name="Magento\Shipping\Model\Carrier\VirtualDebug" type="Magento\Framework\Logger\Handler\Base">
14+
<arguments>
15+
<argument name="fileName" xsi:type="string">/var/log/shipping.log</argument>
16+
</arguments>
17+
</virtualType>
18+
<virtualType name="Magento\Shipping\Model\Method\VirtualLogger" type="Magento\Framework\Logger\Monolog">
19+
<arguments>
20+
<argument name="handlers" xsi:type="array">
21+
<item name="debug" xsi:type="object">Magento\Shipping\Model\Carrier\VirtualDebug</item>
22+
</argument>
23+
</arguments>
24+
</virtualType>
25+
<type name="Magento\Shipping\Model\Carrier\AbstractCarrier">
26+
<arguments>
27+
<argument name="logger" xsi:type="object">Magento\Shipping\Model\Method\VirtualLogger</argument>
28+
</arguments>
29+
</type>
1230
</config>

0 commit comments

Comments
 (0)