Skip to content

Commit 6559f09

Browse files
author
Jeroen van Leusden
committed
[WD2-282] Add logger handler
1 parent 54fbb48 commit 6559f09

20 files changed

+266
-185
lines changed

src/Logger/Handler.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
/**
3+
* Copyright © Reach Digital (https://www.reachdigital.io/)
4+
* See LICENSE.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Ho\Import\Logger;
9+
10+
class Handler extends \Magento\Framework\Logger\Handler\Base
11+
{
12+
/**
13+
* @var int
14+
*/
15+
protected $loggerType = \Monolog\Logger::INFO;
16+
17+
/**
18+
* @var string
19+
*/
20+
protected $fileName = '/var/log/import/info.log';
21+
}

src/Logger/Log.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
/**
3+
* Copyright © Reach Digital (https://www.reachdigital.io/)
4+
* See LICENSE.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Ho\Import\Logger;
9+
10+
class Log extends \Monolog\Logger
11+
{
12+
// -
13+
}

src/Model/ImportProfile.php

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,10 @@
77
namespace Ho\Import\Model;
88

99
use Ho\Import\Api\ImportProfileInterface;
10+
use Ho\Import\Logger\Log;
1011
use Magento\Framework\Phrase;
1112
use Magento\Framework\ObjectManagerInterface;
12-
13-
14-
use Magento\Framework\App\ObjectManager\ConfigLoader;
1513
use Magento\Framework\App\ObjectManagerFactory;
16-
use Magento\Framework\App\State as AppState;
1714
use Symfony\Component\Console\Output\ConsoleOutput;
1815
use Symfony\Component\Stopwatch\Stopwatch;
1916

@@ -40,22 +37,27 @@ abstract class ImportProfile implements ImportProfileInterface
4037
*/
4138
private $objectManagerFactory;
4239

40+
/**
41+
* @var Log
42+
*/
43+
protected $log;
4344

4445
/**
45-
* ImportProfile constructor.
46-
*
47-
* @param ObjectManagerFactory $objectManagerFactory
48-
* @param Stopwatch $stopwatch
49-
* @param ConsoleOutput $consoleOutput
46+
* @param ObjectManagerFactory $objectManagerFactory
47+
* @param Stopwatch $stopwatch
48+
* @param ConsoleOutput $consoleOutput
49+
* @param Log $log
5050
*/
5151
public function __construct(
5252
ObjectManagerFactory $objectManagerFactory,
5353
Stopwatch $stopwatch,
54-
ConsoleOutput $consoleOutput
54+
ConsoleOutput $consoleOutput,
55+
Log $log
5556
) {
5657
$this->objectManagerFactory = $objectManagerFactory;
5758
$this->stopwatch = $stopwatch;
5859
$this->consoleOutput = $consoleOutput;
60+
$this->log = $log;
5961
}
6062

6163

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

81-
$this->consoleOutput->writeln((string) new Phrase(
83+
$output = (string) new Phrase(
8284
'%1 items imported in %2 sec, <info>%3 items / sec</info> (%4mb used)', [
8385
count($items),
8486
round($stopwatchEvent->getDuration() / 1000, 1),
8587
round(count($items) / ($stopwatchEvent->getDuration() / 1000), 1),
8688
round($stopwatchEvent->getMemory() / 1024 / 1024, 1)
87-
]));
89+
]);
90+
91+
$this->consoleOutput->writeln($output);
92+
$this->log->addInfo($output);
8893

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

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

95102
return \Magento\Framework\Console\Cli::RETURN_FAILURE;
96103
}
@@ -99,26 +106,31 @@ public function run()
99106
/**
100107
* Get all items that need to be imported
101108
*
102-
* @return \[]
109+
* @return array
103110
*/
104111
private function getItemsMeasured()
105112
{
106113
$this->stopwatch->start('profileinstance');
107114
$this->consoleOutput->writeln('Getting item data');
115+
$this->log->addInfo('Getting item data');
108116
$items = $this->getItems();
109117
$stopwatchEvent = $this->stopwatch->stop('profileinstance');
110118

111119
if (! $stopwatchEvent->getDuration()) {
112120
return $items;
113121
}
114122

115-
$this->consoleOutput->writeln((string)new Phrase('%1 items processed in %2 sec, <info>%3 items / sec</info> (%4mb used)',
123+
$output = (string) new Phrase('%1 items processed in %2 sec, <info>%3 items / sec</info> (%4mb used)',
116124
[
117125
count($items),
118126
round($stopwatchEvent->getDuration() / 1000, 1),
119127
round(count($items) / ($stopwatchEvent->getDuration() / 1000), 1),
120128
round($stopwatchEvent->getMemory() / 1024 / 1024, 1)
121-
]));
129+
]);
130+
131+
$this->consoleOutput->writeln($output);
132+
$this->log->addInfo($output);
133+
122134
return $items;
123135
}
124136

src/RowModifier/AbstractRowModifier.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
namespace Ho\Import\RowModifier;
88

9+
use Ho\Import\Logger\Log;
910
use Symfony\Component\Console\Output\ConsoleOutput;
1011

1112
/**
@@ -15,15 +16,17 @@
1516
*/
1617
abstract class AbstractRowModifier
1718
{
18-
1919
/**
2020
* Log items to the console.
2121
*
2222
* @var ConsoleOutput
2323
*/
2424
protected $consoleOutput;
2525

26-
26+
/**
27+
* @var Log
28+
*/
29+
protected $log;
2730

2831
/**
2932
* Item array with all items to import
@@ -33,17 +36,15 @@ abstract class AbstractRowModifier
3336
protected $items;
3437

3538
/**
36-
* AbstractRowModifier constructor.
37-
*
3839
* @param ConsoleOutput $consoleOutput
40+
* @param Log $log
3941
*/
40-
public function __construct(
41-
ConsoleOutput $consoleOutput
42-
) {
42+
public function __construct(ConsoleOutput $consoleOutput, Log $log)
43+
{
4344
$this->consoleOutput = $consoleOutput;
45+
$this->log = $log;
4446
}
4547

46-
4748
/**
4849
* Set the data array for fields to import
4950
*

src/RowModifier/AttributeOptionCreator.php

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
<?php
22
/**
3-
* Copyright (c) 2016 H&O E-commerce specialisten B.V. (http://www.h-o.nl/)
3+
* Copyright © Reach Digital (https://www.reachdigital.io/)
44
* See LICENSE.txt for license details.
55
*/
66

77
namespace Ho\Import\RowModifier;
88

9+
use Ho\Import\Logger\Log;
910
use Magento\Catalog\Api\ProductAttributeOptionManagementInterface as OptionManagement;
1011
use Magento\Eav\Model\Entity\Attribute\OptionFactory;
1112
use Symfony\Component\Console\Output\ConsoleOutput;
@@ -18,7 +19,6 @@
1819
*/
1920
class AttributeOptionCreator extends AbstractRowModifier
2021
{
21-
2222
/**
2323
* Option management interface
2424
*
@@ -41,45 +41,53 @@ class AttributeOptionCreator extends AbstractRowModifier
4141
protected $attributes;
4242

4343
/**
44-
* AttributeOptionCreator constructor.
45-
*
4644
* @param OptionManagement $optionManagement
4745
* @param OptionFactory $optionFactory
4846
* @param ConsoleOutput $consoleOutput
47+
* @param Log $log
4948
* @param string[] $attributes
5049
*/
5150
public function __construct(
5251
OptionManagement $optionManagement,
5352
OptionFactory $optionFactory,
5453
ConsoleOutput $consoleOutput,
54+
Log $log,
5555
$attributes = []
5656
) {
57-
parent::__construct($consoleOutput);
57+
parent::__construct($consoleOutput, $log);
58+
5859
$this->optionManagement = $optionManagement;
5960
$this->optionFactory = $optionFactory;
6061
$this->attributes = $attributes;
6162
}
6263

63-
6464
/**
6565
* Automatically create attribute options.
6666
*
67+
* @throws \Magento\Framework\Exception\InputException
68+
* @throws \Magento\Framework\Exception\StateException
69+
*
6770
* @return void
6871
*/
6972
public function process()
7073
{
7174
$attributes = implode(', ', $this->attributes);
7275
$this->consoleOutput->writeln("<info>Creating attribute options for: {$attributes}</info>");
76+
$this->log->addInfo('Creating attribute options for:'. $attributes);
77+
7378
foreach ($this->attributes as $attribute) {
7479
$this->createForAttribute($attribute);
7580
}
7681
}
7782

78-
7983
/**
8084
* Create attribute options
8185
*
8286
* @param string $attributeCode
87+
*
88+
* @throws \Magento\Framework\Exception\InputException
89+
* @throws \Magento\Framework\Exception\StateException
90+
*
8391
* @return void
8492
*/
8593
public function createForAttribute(string $attributeCode)
@@ -88,7 +96,7 @@ public function createForAttribute(string $attributeCode)
8896

8997
$items = $this->optionManagement->getItems($attributeCode);
9098
foreach ($items as $item) {
91-
if (in_array($item->getLabel(), $uniqueOptions)) {
99+
if (\in_array($item->getLabel(), $uniqueOptions)) {
92100
unset($uniqueOptions[$item->getLabel()]);
93101
}
94102
}
@@ -118,10 +126,12 @@ protected function getNonExistingAttributes(string $attribute)
118126
continue;
119127
}
120128

121-
if (! is_string($item[$attribute])) {
129+
if (! \is_string($item[$attribute])) {
122130
$this->consoleOutput->writeln(
123131
"<error>AttributeOptionCreator: Invalid value for {$attribute} {$identifier}</error>"
124132
);
133+
$this->log->addError("AttributeOptionCreator: Invalid value for {$attribute} {$identifier}");
134+
125135
$item[$attribute] = '';
126136
continue;
127137
}

0 commit comments

Comments
 (0)