Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create custom logger and save to /var/log/pagseguro.log #3

Merged
merged 1 commit into from
Nov 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 16 additions & 14 deletions Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,15 @@ public function __construct(
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Magento\Checkout\Model\Session $checkoutSession,
\Magento\Customer\Model\Customer $customer,
\Magento\Framework\App\Helper\Context $context
\Magento\Framework\App\Helper\Context $context,
\Psr\Log\LoggerInterface $customLogger

) {
$this->storeManager = $storeManager;
$this->checkoutSession = $checkoutSession;
$this->customerRepo = $customer;
$this->_customLogger = $customLogger;

parent::__construct($context);
}

Expand All @@ -82,7 +85,6 @@ public function __construct(
*/
public function getSessionId()
{

$url = $this->getWsUrl('sessions');
$ch = curl_init($url);
$params['email'] = $this->getMerchantEmail();
Expand Down Expand Up @@ -164,9 +166,9 @@ public function writeLog($obj)
{
if ($this->isDebugActive()) {
if (is_string($obj)) {
$this->_logger->debug($obj);
$this->_customLogger->debug($obj);
} else {
$this->_logger->debug(json_encode($obj));
$this->_customLogger->debug(json_encode($obj));
}
}
}
Expand Down Expand Up @@ -238,7 +240,7 @@ public function getPaymentHash($param = null)
$psPayment = $this->checkoutSession->getData('PsPayment');

$psPayment = unserialize($psPayment);
// $this->writeLog('getPaymentHash'.json_encode($psPayment));
// $this->writeLog('getPaymentHash'.json_encode($psPayment));
if (is_null($param)) {
return $psPayment;
}
Expand Down Expand Up @@ -336,9 +338,9 @@ public function callApi($params, $payment, $type='transactions')
$params = $paramsObj->getParams();
$paramsString = $this->convertToCURLString($params);

//$this->writeLog('Parameters being sent to API (/'.$type.'): '. var_export($params, true));
$this->writeLog('Parameters being sent to API (/'.$type.'): '. var_export($params, true));
Copy link
Owner

@r-martins r-martins Nov 12, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm almost sure that you don't need the var_export, and $params can be passed as the second argument of writeLog method.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@r-martins there's only 1 parameter in writeLog function. var_export is needed since $param was concatenated into a string, Otherwise the log will look like this:

[2018-11-13 08:02:38] main.DEBUG: Parameters being sent to API (/transactions): [] []


//$this->writeLog('WSDL URL:'.$this->getWsUrl($type));
$this->writeLog('WSDL URL:'.$this->getWsUrl($type));

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->getWsUrl($type));
Expand All @@ -360,7 +362,7 @@ public function callApi($params, $payment, $type='transactions')
}
curl_close($ch);

// $this->writeLog('Retorno PagSeguro (/'.$type.'): ' . var_export($response, true));
$this->writeLog('Retorno PagSeguro (/'.$type.'): ' . var_export($response, true));
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here



$xml = \SimpleXML_Load_String(trim($response));
Expand All @@ -384,16 +386,16 @@ public function callApi($params, $payment, $type='transactions')
if (false === $xml) {
switch($response){
case 'Unauthorized':
// $this->writeLog(
// 'Token / email not authorized by PagSeguro. Check your settings on the panel.'
// );
$this->writeLog(
'Token / email not authorized by PagSeguro. Check your settings on the panel.'
);
break;
case 'Forbidden':
//$this->writeLog('Unauthorized access to Api Pagseguro. Make sure you have permission to use this service. Return: ' . var_export($response, true)
// );
$this->writeLog('Unauthorized access to Api Pagseguro. Make sure you have permission to use this service. Return: ' . var_export($response, true)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and here

);
break;
default:
// $this->writeLog('Unexpected return of PagSeguro. Return: ' . $response);
$this->writeLog('Unexpected return of PagSeguro. Return: ' . $response);
}
throw new \Magento\Framework\Validator\Exception(
'There was a problem processing your request / payment. Please contact us.'
Expand Down
20 changes: 20 additions & 0 deletions Logger/Handler/Custom.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php


namespace RicardoMartins\PagSeguro\Logger\Handler;

use Monolog\Logger;
use Magento\Framework\Logger\Handler\Base;

class Custom extends Base
{
/**
* @var string
*/
protected $fileName = '/var/log/pagseguro.log';
/**
* @var int
*/
protected $loggerType = Logger::DEBUG;

}
24 changes: 24 additions & 0 deletions etc/di.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0"?>
<!--
/**
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">

<virtualType name="PagSeguroLogger" type="Magento\Framework\Logger\Monolog">
<arguments>
<argument name="handlers" xsi:type="array">
<item name="debug" xsi:type="object">RicardoMartins\PagSeguro\Logger\Handler\Custom</item>
</argument>
</arguments>
</virtualType>

<type name="RicardoMartins\PagSeguro\Helper\Data">
<arguments>
<argument name="customLogger" xsi:type="object">PagSeguroLogger</argument>
</arguments>
</type>

</config>