Skip to content

Commit 657c269

Browse files
authored
add log in content writer and notmodifiercontentfilter (#35)
* add log in content writer and notmodifiercontentfilter * add changelog and readme
1 parent 6f1a719 commit 657c269

4 files changed

Lines changed: 36 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Version 2.3.0
22

33
* Added a button to display exceptions / log in a modal
4+
* Add log in `CodeRhapsodie\EzDataflowBundle\Filter\NotModifiedContentFilter` and `CodeRhapsodie\EzDataflowBundle\Writer\ContentWriter`
45

56
# Version 2.2.0
67

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,8 @@ class MyDataflowType extends AbstractDataflowType
206206
ContentStructureFactoryInterface::MODE_INSERT_OR_UPDATE //Optional value. Other choice : ContentStructureFactoryInterface::MODE_INSERT_ONLY or ContentStructureFactoryInterface::MODE_UPDATE_ONLY
207207
);
208208
});
209+
// If you want the writer log
210+
$this->contentWriter->setLogger($this->logger);
209211
$builder->addWriter($this->contentWriter);
210212
}
211213
}
@@ -228,6 +230,8 @@ public function __construct(NotModifiedContentFilter $notModifiedContentFilter)
228230
protected function buildDataflow(DataflowBuilder $builder, array $options): void
229231
{
230232
//[...]
233+
// If you want the filter log
234+
$this->notModifiedContentFilter->setLogger($this->logger);
231235
$builder->addStep($this->notModifiedContentFilter);
232236
//[...]
233237
}

src/Filter/NotModifiedContentFilter.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,22 @@
77
use CodeRhapsodie\EzDataflowBundle\Core\FieldComparator\FieldComparatorInterface;
88
use CodeRhapsodie\EzDataflowBundle\Model\ContentUpdateStructure;
99
use eZ\Publish\API\Repository\ContentService;
10+
use Psr\Log\LoggerAwareTrait;
1011

1112
/**
1213
* Filters ContentUpdateStructure that would not result in any actual changes in the content.
1314
*/
1415
class NotModifiedContentFilter
1516
{
17+
use LoggerAwareTrait;
18+
1619
/** @var ContentService */
1720
private $contentService;
1821

1922
/** @var FieldComparatorInterface */
2023
private $comparator;
2124

25+
2226
public function __construct(ContentService $contentService, FieldComparatorInterface $comparator)
2327
{
2428
$this->contentService = $contentService;
@@ -46,6 +50,15 @@ public function __invoke($data)
4650
}
4751

4852
// All fields are identical, filter this item out.
53+
$this->log('info', 'Not modified content skipped', ['id' => $data->getId(), 'remote_id' => $data->getRemoteId()]);
4954
return false;
5055
}
56+
57+
private function log(string $level, string $message, array $context = [])
58+
{
59+
if ($this->logger === null) {
60+
return;
61+
}
62+
$this->logger->log($level, $message, $context);
63+
}
5164
}

src/Writer/ContentWriter.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,12 @@
1111
use CodeRhapsodie\EzDataflowBundle\Model\ContentStructure;
1212
use CodeRhapsodie\EzDataflowBundle\Model\ContentUpdateStructure;
1313

14+
use Psr\Log\LoggerAwareTrait;
15+
1416
class ContentWriter extends RepositoryWriter implements DelegateWriterInterface
1517
{
18+
use LoggerAwareTrait;
19+
1620
/** @var ContentCreatorInterface */
1721
private $creator;
1822

@@ -31,14 +35,20 @@ public function __construct(ContentCreatorInterface $creator, ContentUpdaterInte
3135
public function write($item)
3236
{
3337
if (!$item instanceof ContentStructure) {
38+
$this->log('warning', "Data is not a ContentStucture");
3439
return;
3540
}
3641

3742
if ($item instanceof ContentCreateStructure) {
43+
$this->log('info', 'Save content', [
44+
'content_type' => $item->getContentTypeIdentifier(),
45+
'content_location' => $item->getLocations()
46+
]);
3847
return $this->creator->createFromStructure($item);
3948
}
4049

4150
if ($item instanceof ContentUpdateStructure) {
51+
$this->log('info', 'Update content', ['id' => $item->getId(), 'remote_id' => $item->getRemoteId()]);
4252
return $this->updater->updateFromStructure($item);
4353
}
4454
}
@@ -50,4 +60,12 @@ public function supports($item): bool
5060
{
5161
return $item instanceof ContentStructure;
5262
}
63+
64+
private function log(string $level, string $message, array $context = [])
65+
{
66+
if ($this->logger === null) {
67+
return;
68+
}
69+
$this->logger->log($level, $message, $context);
70+
}
5371
}

0 commit comments

Comments
 (0)