Skip to content

Commit

Permalink
[WD2-282] Add logger handler
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeroen van Leusden committed Sep 17, 2018
1 parent 54fbb48 commit 6559f09
Show file tree
Hide file tree
Showing 20 changed files with 266 additions and 185 deletions.
21 changes: 21 additions & 0 deletions src/Logger/Handler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
/**
* Copyright © Reach Digital (https://www.reachdigital.io/)
* See LICENSE.txt for license details.
*/
declare(strict_types=1);

namespace Ho\Import\Logger;

class Handler extends \Magento\Framework\Logger\Handler\Base
{
/**
* @var int
*/
protected $loggerType = \Monolog\Logger::INFO;

/**
* @var string
*/
protected $fileName = '/var/log/import/info.log';
}
13 changes: 13 additions & 0 deletions src/Logger/Log.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php
/**
* Copyright © Reach Digital (https://www.reachdigital.io/)
* See LICENSE.txt for license details.
*/
declare(strict_types=1);

namespace Ho\Import\Logger;

class Log extends \Monolog\Logger
{
// -
}
42 changes: 27 additions & 15 deletions src/Model/ImportProfile.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,10 @@
namespace Ho\Import\Model;

use Ho\Import\Api\ImportProfileInterface;
use Ho\Import\Logger\Log;
use Magento\Framework\Phrase;
use Magento\Framework\ObjectManagerInterface;


use Magento\Framework\App\ObjectManager\ConfigLoader;
use Magento\Framework\App\ObjectManagerFactory;
use Magento\Framework\App\State as AppState;
use Symfony\Component\Console\Output\ConsoleOutput;
use Symfony\Component\Stopwatch\Stopwatch;

Expand All @@ -40,22 +37,27 @@ abstract class ImportProfile implements ImportProfileInterface
*/
private $objectManagerFactory;

/**
* @var Log
*/
protected $log;

/**
* ImportProfile constructor.
*
* @param ObjectManagerFactory $objectManagerFactory
* @param Stopwatch $stopwatch
* @param ConsoleOutput $consoleOutput
* @param ObjectManagerFactory $objectManagerFactory
* @param Stopwatch $stopwatch
* @param ConsoleOutput $consoleOutput
* @param Log $log
*/
public function __construct(
ObjectManagerFactory $objectManagerFactory,
Stopwatch $stopwatch,
ConsoleOutput $consoleOutput
ConsoleOutput $consoleOutput,
Log $log
) {
$this->objectManagerFactory = $objectManagerFactory;
$this->stopwatch = $stopwatch;
$this->consoleOutput = $consoleOutput;
$this->log = $log;
}


Expand All @@ -78,19 +80,24 @@ public function run()
$errors = $importer->processImport($items);
$stopwatchEvent = $this->stopwatch->stop('importinstance');

$this->consoleOutput->writeln((string) new Phrase(
$output = (string) new Phrase(
'%1 items imported in %2 sec, <info>%3 items / sec</info> (%4mb used)', [
count($items),
round($stopwatchEvent->getDuration() / 1000, 1),
round(count($items) / ($stopwatchEvent->getDuration() / 1000), 1),
round($stopwatchEvent->getMemory() / 1024 / 1024, 1)
]));
]);

$this->consoleOutput->writeln($output);
$this->log->addInfo($output);

$this->consoleOutput->writeln("<error>$errors</error>");
$this->log->addError($errors);

return \Magento\Framework\Console\Cli::RETURN_SUCCESS;
} catch (\Exception $e) {
$this->consoleOutput->writeln($e->getMessage());
$this->log->addCritical($e->getMessage());

return \Magento\Framework\Console\Cli::RETURN_FAILURE;
}
Expand All @@ -99,26 +106,31 @@ public function run()
/**
* Get all items that need to be imported
*
* @return \[]
* @return array
*/
private function getItemsMeasured()
{
$this->stopwatch->start('profileinstance');
$this->consoleOutput->writeln('Getting item data');
$this->log->addInfo('Getting item data');
$items = $this->getItems();
$stopwatchEvent = $this->stopwatch->stop('profileinstance');

if (! $stopwatchEvent->getDuration()) {
return $items;
}

$this->consoleOutput->writeln((string)new Phrase('%1 items processed in %2 sec, <info>%3 items / sec</info> (%4mb used)',
$output = (string) new Phrase('%1 items processed in %2 sec, <info>%3 items / sec</info> (%4mb used)',
[
count($items),
round($stopwatchEvent->getDuration() / 1000, 1),
round(count($items) / ($stopwatchEvent->getDuration() / 1000), 1),
round($stopwatchEvent->getMemory() / 1024 / 1024, 1)
]));
]);

$this->consoleOutput->writeln($output);
$this->log->addInfo($output);

return $items;
}

Expand Down
17 changes: 9 additions & 8 deletions src/RowModifier/AbstractRowModifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

namespace Ho\Import\RowModifier;

use Ho\Import\Logger\Log;
use Symfony\Component\Console\Output\ConsoleOutput;

/**
Expand All @@ -15,15 +16,17 @@
*/
abstract class AbstractRowModifier
{

/**
* Log items to the console.
*
* @var ConsoleOutput
*/
protected $consoleOutput;


/**
* @var Log
*/
protected $log;

/**
* Item array with all items to import
Expand All @@ -33,17 +36,15 @@ abstract class AbstractRowModifier
protected $items;

/**
* AbstractRowModifier constructor.
*
* @param ConsoleOutput $consoleOutput
* @param Log $log
*/
public function __construct(
ConsoleOutput $consoleOutput
) {
public function __construct(ConsoleOutput $consoleOutput, Log $log)
{
$this->consoleOutput = $consoleOutput;
$this->log = $log;
}


/**
* Set the data array for fields to import
*
Expand Down
28 changes: 19 additions & 9 deletions src/RowModifier/AttributeOptionCreator.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
<?php
/**
* Copyright (c) 2016 H&O E-commerce specialisten B.V. (http://www.h-o.nl/)
* Copyright © Reach Digital (https://www.reachdigital.io/)
* See LICENSE.txt for license details.
*/

namespace Ho\Import\RowModifier;

use Ho\Import\Logger\Log;
use Magento\Catalog\Api\ProductAttributeOptionManagementInterface as OptionManagement;
use Magento\Eav\Model\Entity\Attribute\OptionFactory;
use Symfony\Component\Console\Output\ConsoleOutput;
Expand All @@ -18,7 +19,6 @@
*/
class AttributeOptionCreator extends AbstractRowModifier
{

/**
* Option management interface
*
Expand All @@ -41,45 +41,53 @@ class AttributeOptionCreator extends AbstractRowModifier
protected $attributes;

/**
* AttributeOptionCreator constructor.
*
* @param OptionManagement $optionManagement
* @param OptionFactory $optionFactory
* @param ConsoleOutput $consoleOutput
* @param Log $log
* @param string[] $attributes
*/
public function __construct(
OptionManagement $optionManagement,
OptionFactory $optionFactory,
ConsoleOutput $consoleOutput,
Log $log,
$attributes = []
) {
parent::__construct($consoleOutput);
parent::__construct($consoleOutput, $log);

$this->optionManagement = $optionManagement;
$this->optionFactory = $optionFactory;
$this->attributes = $attributes;
}


/**
* Automatically create attribute options.
*
* @throws \Magento\Framework\Exception\InputException
* @throws \Magento\Framework\Exception\StateException
*
* @return void
*/
public function process()
{
$attributes = implode(', ', $this->attributes);
$this->consoleOutput->writeln("<info>Creating attribute options for: {$attributes}</info>");
$this->log->addInfo('Creating attribute options for:'. $attributes);

foreach ($this->attributes as $attribute) {
$this->createForAttribute($attribute);
}
}


/**
* Create attribute options
*
* @param string $attributeCode
*
* @throws \Magento\Framework\Exception\InputException
* @throws \Magento\Framework\Exception\StateException
*
* @return void
*/
public function createForAttribute(string $attributeCode)
Expand All @@ -88,7 +96,7 @@ public function createForAttribute(string $attributeCode)

$items = $this->optionManagement->getItems($attributeCode);
foreach ($items as $item) {
if (in_array($item->getLabel(), $uniqueOptions)) {
if (\in_array($item->getLabel(), $uniqueOptions)) {
unset($uniqueOptions[$item->getLabel()]);
}
}
Expand Down Expand Up @@ -118,10 +126,12 @@ protected function getNonExistingAttributes(string $attribute)
continue;
}

if (! is_string($item[$attribute])) {
if (! \is_string($item[$attribute])) {
$this->consoleOutput->writeln(
"<error>AttributeOptionCreator: Invalid value for {$attribute} {$identifier}</error>"
);
$this->log->addError("AttributeOptionCreator: Invalid value for {$attribute} {$identifier}");

$item[$attribute] = '';
continue;
}
Expand Down
Loading

0 comments on commit 6559f09

Please sign in to comment.