Skip to content
Snippets Groups Projects
Commit b6605995 authored by Christel Loftus's avatar Christel Loftus
Browse files

Add custom logger - monolog

parent ff3e9382
No related branches found
No related tags found
1 merge request!6Rates at checkout
Pipeline #64963 passed
<?php
namespace BobGroup\BobGo\Logger;
class CustomLogger extends \Monolog\Logger
{
}
<?php
namespace BobGroup\BobGo\Logger;
use Monolog\Logger;
use Magento\Framework\Logger\Handler\Base;
class Handler extends Base
{
protected $fileName = '/var/log/bobgo.log';
protected $loggerType = Logger::DEBUG;
}
...@@ -1179,7 +1179,6 @@ ...@@ -1179,7 +1179,6 @@
//} //}
<?php
namespace BobGroup\BobGo\Model\Carrier; namespace BobGroup\BobGo\Model\Carrier;
use Magento\Framework\App\Config\ScopeConfigInterface; use Magento\Framework\App\Config\ScopeConfigInterface;
...@@ -1188,7 +1187,7 @@ use Magento\Quote\Model\Quote\Address\RateRequest; ...@@ -1188,7 +1187,7 @@ use Magento\Quote\Model\Quote\Address\RateRequest;
use Magento\Shipping\Model\Carrier\AbstractCarrierOnline; use Magento\Shipping\Model\Carrier\AbstractCarrierOnline;
use Magento\Shipping\Model\Carrier\CarrierInterface; use Magento\Shipping\Model\Carrier\CarrierInterface;
use Magento\Shipping\Model\Rate\Result; use Magento\Shipping\Model\Rate\Result;
use Psr\Log\LoggerInterface; use BobGroup\BobGo\Logger\CustomLogger;
class BobGo extends AbstractCarrierOnline implements CarrierInterface class BobGo extends AbstractCarrierOnline implements CarrierInterface
{ {
...@@ -1197,31 +1196,32 @@ class BobGo extends AbstractCarrierOnline implements CarrierInterface ...@@ -1197,31 +1196,32 @@ class BobGo extends AbstractCarrierOnline implements CarrierInterface
protected $scopeConfig; protected $scopeConfig;
protected $httpClientFactory; protected $httpClientFactory;
protected $logger; protected $customLogger;
public function __construct( public function __construct(
ScopeConfigInterface $scopeConfig, ScopeConfigInterface $scopeConfig,
\Magento\Quote\Model\Quote\Address\RateResult\ErrorFactory $rateErrorFactory, \Magento\Quote\Model\Quote\Address\RateResult\ErrorFactory $rateErrorFactory,
LoggerInterface $logger, CustomLogger $customLogger,
ZendClientFactory $httpClientFactory, ZendClientFactory $httpClientFactory,
array $data = [] array $data = []
) { )
{
$this->scopeConfig = $scopeConfig; $this->scopeConfig = $scopeConfig;
$this->httpClientFactory = $httpClientFactory; $this->httpClientFactory = $httpClientFactory;
$this->logger = $logger; $this->customLogger = $customLogger;
parent::__construct($scopeConfig, $rateErrorFactory, $logger, $data); parent::__construct($scopeConfig, $rateErrorFactory, $customLogger, $data);
} }
public function collectRates(RateRequest $request) public function collectRates(RateRequest $request)
{ {
$this->logger->info('BobGo collectRates method called'); $this->customLogger->info('BobGo collectRates method called');
if (!$this->getConfigFlag('active')) { if (!$this->getConfigFlag('active')) {
$this->logger->info('BobGo is not active'); $this->customLogger->info('BobGo is not active');
return false; return false;
} }
$this->logger->info('BobGo is active, proceeding with rate collection'); $this->customLogger->info('BobGo is active, proceeding with rate collection');
$result = $this->_rateFactory->create(); $result = $this->_rateFactory->create();
...@@ -1235,11 +1235,11 @@ class BobGo extends AbstractCarrierOnline implements CarrierInterface ...@@ -1235,11 +1235,11 @@ class BobGo extends AbstractCarrierOnline implements CarrierInterface
'package_qty' => $request->getPackageQty(), 'package_qty' => $request->getPackageQty(),
]; ];
$this->logger->info('Request parameters: ' . json_encode($params)); $this->customLogger->info('Request parameters: ' . json_encode($params));
try { try {
$apiResponse = $this->_fetchRatesFromApi($params); $apiResponse = $this->_fetchRatesFromApi($params);
$this->logger->info('API response: ' . json_encode($apiResponse)); $this->customLogger->info('API response: ' . json_encode($apiResponse));
if ($apiResponse && isset($apiResponse['rates']) && !empty($apiResponse['rates'])) { if ($apiResponse && isset($apiResponse['rates']) && !empty($apiResponse['rates'])) {
foreach ($apiResponse['rates'] as $rateData) { foreach ($apiResponse['rates'] as $rateData) {
...@@ -1253,7 +1253,7 @@ class BobGo extends AbstractCarrierOnline implements CarrierInterface ...@@ -1253,7 +1253,7 @@ class BobGo extends AbstractCarrierOnline implements CarrierInterface
$result->append($rate); $result->append($rate);
} }
} else { } else {
$this->logger->info('No rates returned from API'); $this->customLogger->info('No rates returned from API');
$error = $this->_rateErrorFactory->create(); $error = $this->_rateErrorFactory->create();
$error->setCarrier($this->_code); $error->setCarrier($this->_code);
$error->setCarrierTitle($this->getConfigData('title')); $error->setCarrierTitle($this->getConfigData('title'));
...@@ -1261,7 +1261,7 @@ class BobGo extends AbstractCarrierOnline implements CarrierInterface ...@@ -1261,7 +1261,7 @@ class BobGo extends AbstractCarrierOnline implements CarrierInterface
return $error; return $error;
} }
} catch (\Exception $e) { } catch (\Exception $e) {
$this->logger->error('Error fetching rates from API: ' . $e->getMessage()); $this->customLogger->error('Error fetching rates from API: ' . $e->getMessage());
$error = $this->_rateErrorFactory->create(); $error = $this->_rateErrorFactory->create();
$error->setCarrier($this->_code); $error->setCarrier($this->_code);
$error->setCarrierTitle($this->getConfigData('title')); $error->setCarrierTitle($this->getConfigData('title'));
...@@ -1287,10 +1287,10 @@ class BobGo extends AbstractCarrierOnline implements CarrierInterface ...@@ -1287,10 +1287,10 @@ class BobGo extends AbstractCarrierOnline implements CarrierInterface
if ($response->isSuccessful()) { if ($response->isSuccessful()) {
return json_decode($response->getBody(), true); return json_decode($response->getBody(), true);
} else { } else {
$this->logger->error('API request failed with status: ' . $response->getStatus()); $this->customLogger->error('API request failed with status: ' . $response->getStatus());
} }
} catch (\Exception $e) { } catch (\Exception $e) {
$this->logger->error('Exception during API request: ' . $e->getMessage()); $this->customLogger->error('Exception during API request: ' . $e->getMessage());
} }
return false; return false;
...@@ -1302,3 +1302,6 @@ class BobGo extends AbstractCarrierOnline implements CarrierInterface ...@@ -1302,3 +1302,6 @@ class BobGo extends AbstractCarrierOnline implements CarrierInterface
} }
} }
<?php
return [
'backend' => [
'frontName' => 'admin'
],
'install' => [
'date' => 'Wed, 15 Jan 2020 00:00:00 +0000'
],
'system' => [
'default' => [
'dev' => [
'log' => [
'active' => '1'
]
]
]
],
// Other configurations...
];
<?xml version="1.0"?>
<!--<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">-->
<!-- <type name="Magento\Checkout\Block\Checkout\LayoutProcessor">-->
<!-- <plugin name="add_custom_field_checkout_shipping_form" type="BobGroup\BobGo\Plugin\Checkout\Block\LayoutProcessorPlugin" sortOrder="10"/>-->
<!-- <arguments>-->
<!-- <argument name="layoutProcessors" xsi:type="array">-->
<!-- <item name="orderattribute" xsi:type="object">BobGroup\BobGo\Plugin\Checkout\Block\LayoutProcessorPlugin</item>-->
<!-- </argument>-->
<!-- </arguments>-->
<!-- </type>-->
<!--</config>-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Checkout\Block\Checkout\LayoutProcessor"> <type name="BobGroup\BobGo\Logger\CustomLogger">
<plugin name="add_custom_field_checkout_shipping_form" type="BobGroup\BobGo\Plugin\Checkout\Block\LayoutProcessorPlugin" sortOrder="10"/>
<arguments> <arguments>
<argument name="layoutProcessors" xsi:type="array"> <argument name="name" xsi:type="string">bobgo</argument>
<item name="orderattribute" xsi:type="object">BobGroup\BobGo\Plugin\Checkout\Block\LayoutProcessorPlugin</item> <argument name="handlers" xsi:type="array">
<item name="stream" xsi:type="object">BobGroup\BobGo\Logger\Handler</item>
</argument> </argument>
</arguments> </arguments>
</type> </type>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment