Skip to content

Commit 6066080

Browse files
author
Karpenko, Oleksandr
authored
SWAT-430: Extended debugging information about cache & indexer invali… (#26)
1 parent 11343e9 commit 6066080

File tree

7 files changed

+258
-0
lines changed

7 files changed

+258
-0
lines changed

Model/Cache/InvalidateLogger.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magento\CloudComponents\Model\Cache;
10+
11+
/**
12+
* Log cache invalidation to a file
13+
*/
14+
class InvalidateLogger extends \Magento\Framework\Cache\InvalidateLogger
15+
{
16+
/**
17+
* Log cache invalidation to a file
18+
*
19+
* @param mixed $invalidateInfo
20+
*/
21+
public function execute($invalidateInfo)
22+
{
23+
$invalidateInfo['trace'] = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
24+
parent::execute($invalidateInfo);
25+
}
26+
}

Model/Indexation/Logger.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magento\CloudComponents\Model\Indexation;
10+
11+
use Magento\Framework\Indexer\ActionInterface;
12+
use Psr\Log\LoggerInterface;
13+
14+
/**
15+
* Log full re-indexation to a file
16+
*/
17+
class Logger
18+
{
19+
/**
20+
* @var LoggerInterface
21+
*/
22+
private $logger;
23+
24+
/**
25+
* @param LoggerInterface $logger
26+
*/
27+
public function __construct(LoggerInterface $logger)
28+
{
29+
$this->logger = $logger;
30+
}
31+
32+
/**
33+
* Log full re-indexation to a file
34+
*
35+
* @param ActionInterface $subject
36+
*/
37+
public function afterExecuteFull(ActionInterface $subject)
38+
{
39+
$this->logger->debug(
40+
'full_indexation: ' . get_class($subject),
41+
[
42+
'trace' => debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS)
43+
]
44+
);
45+
}
46+
}

Model/Logger/Handler/Debug.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\CloudComponents\Model\Logger\Handler;
9+
10+
/**
11+
* Debug handler which doesn't require debug mode enabled
12+
*/
13+
class Debug extends \Magento\Framework\Logger\Handler\Debug
14+
{
15+
/**
16+
* @param array $record
17+
* @return mixed
18+
*/
19+
public function isHandling(array $record)
20+
{
21+
return parent::isHandling($record);
22+
}
23+
}

Model/Observer/CacheFlushAll.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magento\CloudComponents\Model\Observer;
10+
11+
use Magento\Framework\Event\Observer;
12+
use Magento\Framework\Event\ObserverInterface;
13+
use Magento\Framework\Cache\InvalidateLogger;
14+
15+
/**
16+
* Log cache flush action to a file
17+
*/
18+
class CacheFlushAll implements ObserverInterface
19+
{
20+
/**
21+
* @var InvalidateLogger
22+
*/
23+
private $logger;
24+
25+
/**
26+
* @param InvalidateLogger $logger
27+
*/
28+
public function __construct(InvalidateLogger $logger)
29+
{
30+
$this->logger = $logger;
31+
}
32+
33+
/**
34+
* Log cache flush action to a file
35+
*
36+
* @param Observer $observer
37+
*/
38+
public function execute(Observer $observer)
39+
{
40+
$this->logger->execute(['tags' => ['all']]);
41+
}
42+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magento\CloudComponents\Model\Observer;
10+
11+
use Magento\Framework\Event\Observer;
12+
use Magento\Framework\Event\ObserverInterface;
13+
use Magento\Framework\Indexer\StateInterface;
14+
use Magento\Framework\Validator\Exception;
15+
use Psr\Log\LoggerInterface;
16+
17+
/**
18+
* Log all indexers invalidations to a file
19+
*/
20+
class IndexerStateSaveAfter implements ObserverInterface
21+
{
22+
/**
23+
* @var LoggerInterface
24+
*/
25+
private $logger;
26+
27+
/**
28+
* @param LoggerInterface $logger
29+
*/
30+
public function __construct(LoggerInterface $logger)
31+
{
32+
$this->logger = $logger;
33+
}
34+
35+
/**
36+
* Log all indexers invalidations to a file
37+
*
38+
* @param Observer $observer
39+
*/
40+
public function execute(Observer $observer)
41+
{
42+
$indexerState = $observer->getData('indexer_state');
43+
if ($indexerState->getData('status') !== $indexerState->getOrigData('status')
44+
&& $indexerState->getData('status') === StateInterface::STATUS_INVALID
45+
) {
46+
$this->logger->debug(
47+
'indexer_invalidation: ' . $indexerState->getData('indexer_id'),
48+
[
49+
'trace' => debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS)
50+
]
51+
);
52+
}
53+
}
54+
}

etc/di.xml

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,56 @@
1515
</argument>
1616
</arguments>
1717
</type>
18+
19+
<!-- Log cache invalidation event to separate file -->
20+
<type name="Magento\CloudComponents\Model\Logger\Handler\Debug">
21+
<arguments>
22+
<argument name="fileName" xsi:type="string">/var/log/cache.log</argument>
23+
</arguments>
24+
</type>
25+
<virtualType name="Magento\CloudComponents\Model\Logger\CacheInvalidate" type="Magento\Framework\Logger\Monolog">
26+
<arguments>
27+
<argument name="name" xsi:type="string">main</argument>
28+
<argument name="handlers" xsi:type="array">
29+
<item name="debug" xsi:type="object">Magento\CloudComponents\Model\Logger\Handler\Debug</item>
30+
</argument>
31+
</arguments>
32+
</virtualType>
33+
<type name="Magento\CloudComponents\Model\Cache\InvalidateLogger">
34+
<arguments>
35+
<argument name="logger" xsi:type="object">Magento\CloudComponents\Model\Logger\CacheInvalidate</argument>
36+
</arguments>
37+
</type>
38+
<preference for="Magento\Framework\Cache\InvalidateLogger" type="Magento\CloudComponents\Model\Cache\InvalidateLogger"/>
39+
40+
<!-- Log full reindex action -->
41+
<virtualType name="Magento\CloudComponents\Model\Logger\Handler\Debug\Indexation" type="Magento\CloudComponents\Model\Logger\Handler\Debug">
42+
<arguments>
43+
<argument name="fileName" xsi:type="string">/var/log/indexation.log</argument>
44+
</arguments>
45+
</virtualType>
46+
<virtualType name="Magento\CloudComponents\Model\Logger\Monolog\Indexation" type="Magento\Framework\Logger\Monolog">
47+
<arguments>
48+
<argument name="name" xsi:type="string">main</argument>
49+
<argument name="handlers" xsi:type="array">
50+
<item name="debug" xsi:type="object">Magento\CloudComponents\Model\Logger\Handler\Debug\Indexation</item>
51+
</argument>
52+
</arguments>
53+
</virtualType>
54+
<type name="Magento\CloudComponents\Model\Indexation\Logger">
55+
<arguments>
56+
<argument name="logger" xsi:type="object">Magento\CloudComponents\Model\Logger\Monolog\Indexation</argument>
57+
</arguments>
58+
</type>
59+
60+
61+
<!-- Log Index invalidation action -->
62+
<type name="Magento\CloudComponents\Model\Observer\IndexerStateSaveAfter">
63+
<arguments>
64+
<argument name="logger" xsi:type="object">Magento\CloudComponents\Model\Logger\Monolog\Indexation</argument>
65+
</arguments>
66+
</type>
67+
<type name="Magento\Framework\Indexer\ActionInterface">
68+
<plugin name="cache_cleaner_after_reindex" type="Magento\CloudComponents\Model\Indexation\Logger" />
69+
</type>
1870
</config>

etc/events.xml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
9+
<event name="indexer_state_save_after">
10+
<observer name="log_indexer_invalidation_action" instance="Magento\CloudComponents\Model\Observer\IndexerStateSaveAfter" />
11+
</event>
12+
<event name="adminhtml_cache_flush_all">
13+
<observer name="log_cache_flush_action" instance="Magento\CloudComponents\Model\Observer\CacheFlushAll" />
14+
</event>
15+
</config>

0 commit comments

Comments
 (0)