Skip to content

Commit 971bb24

Browse files
author
Karpenko, Oleksandr
authored
Logging improvements (#40)
1 parent ef54bc2 commit 971bb24

File tree

3 files changed

+174
-11
lines changed

3 files changed

+174
-11
lines changed

Model/Cache/InvalidateLogger.php

Lines changed: 74 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,93 @@
88

99
namespace Magento\CloudComponents\Model\Cache;
1010

11+
use Magento\Framework\App\Request\Http as HttpRequest;
12+
use Psr\Log\LoggerInterface as Logger;
13+
use Magento\CloudComponents\Model\DebugTrace;
14+
1115
/**
1216
* Log cache invalidation to a file
1317
*/
1418
class InvalidateLogger extends \Magento\Framework\Cache\InvalidateLogger
1519
{
20+
/**
21+
* @var string[]
22+
*/
23+
private $tagsToLog = [
24+
'cat_p',
25+
'cat_c',
26+
'PRODUCT_PRICE',
27+
'cms_b',
28+
'cms_p',
29+
'config_scopes',
30+
'eav',
31+
'eav_attribute',
32+
'fpc',
33+
'review_block',
34+
'SEARCH_QUERY',
35+
'search_query',
36+
'store_group',
37+
'store',
38+
'store_relations',
39+
'website',
40+
'CORE_DESIGN',
41+
'core_design',
42+
'WEBSERVICE',
43+
'webservice',
44+
'banner',
45+
'catalog_event',
46+
'config',
47+
'block_html',
48+
'COLLECTION_DATA',
49+
'collection_data',
50+
'collections',
51+
'layout_general_cache_tag',
52+
'layout',
53+
'compiled_config',
54+
'acl_cache',
55+
'reflection',
56+
'db_ddl',
57+
'all'
58+
];
59+
60+
/**
61+
* @var DebugTrace
62+
*/
63+
private $debugTrace;
64+
65+
/**
66+
* @param HttpRequest $request
67+
* @param Logger $logger
68+
* @param DebugTrace $debugTrace
69+
*/
70+
public function __construct(
71+
HttpRequest $request,
72+
Logger $logger,
73+
DebugTrace $debugTrace
74+
) {
75+
parent::__construct($request, $logger);
76+
$this->debugTrace = $debugTrace;
77+
}
78+
1679
/**
1780
* Log cache invalidation to a file
1881
*
1982
* @param mixed $invalidateInfo
2083
*/
2184
public function execute($invalidateInfo)
2285
{
23-
if (is_array($invalidateInfo)) {
24-
$invalidateInfo['trace'] = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
25-
} elseif (is_string($invalidateInfo)) {
26-
$invalidateInfo = [
27-
'main' => $invalidateInfo,
28-
'trace' => debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS)
29-
];
30-
}
86+
$needTrace = false;
87+
if (is_array($invalidateInfo) && isset($invalidateInfo['tags'])) {
88+
foreach ($invalidateInfo['tags'] as $tag) {
89+
if (in_array(strtolower($tag), $this->tagsToLog)) {
90+
$needTrace = true;
91+
}
92+
}
3193

94+
if ($needTrace) {
95+
$invalidateInfo['trace'] = $this->debugTrace->getTrace();
96+
}
97+
}
3298
parent::execute($invalidateInfo);
3399
}
34100
}

Model/DebugTrace.php

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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;
10+
11+
/**
12+
* Class to get compressed debug back trace
13+
*/
14+
class DebugTrace
15+
{
16+
/**
17+
* List of useless classes
18+
*
19+
* @var string[]
20+
*/
21+
private $notAllowedClasses = [
22+
'Symfony\Component\Console\Application',
23+
'Magento\Framework\Console\Cli',
24+
'Symfony\Component\Console\Command\Command',
25+
'Magento\Staging\Model\Event\Manager\Proxy',
26+
'Magento\Staging\Model\Event\Manager',
27+
'Magento\Framework\Event\Invoker\InvokerDefault',
28+
'Magento\CloudComponents\Model\Observer\CacheFlushAll',
29+
'Magento\CloudComponents\Model\Cache\InvalidateLogger',
30+
'Magento\CloudComponents\Model\Cache\InvalidateLogger',
31+
'Magento\CloudComponents\Model\Indexation\Logger',
32+
'Magento\Framework\Cache\Frontend\Decorator\Logger',
33+
'Magento\Framework\Cache\Frontend\Decorator\Bare',
34+
'Magento\Framework\App\Cache\Type\AccessProxy',
35+
'Magento\Framework\Cache\Frontend\Decorator\TagScope',
36+
'Magento\Framework\ObjectManager\ObjectManager',
37+
'Magento\Framework\ObjectManager\Config\Compiled',
38+
'Magento\Framework\ObjectManager\Config\Config',
39+
'Magento\Framework\ObjectManager\Factory\Dynamic\Developer',
40+
'Magento\Framework\ObjectManager\Factory\Dynamic\Production'
41+
];
42+
43+
/**
44+
* List of useless functions
45+
*
46+
* @var string[]
47+
*/
48+
private $notAllowedFunctions = [
49+
'___callPlugins',
50+
'___callParent',
51+
'Magento\Framework\Interception\{closure}'
52+
];
53+
54+
/**
55+
* Returns debug back trace
56+
*
57+
* @return array
58+
*/
59+
public function getTrace()
60+
{
61+
$trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
62+
foreach ($trace as $index => $line) {
63+
if (!isset($line['function'], $line['class'], $line['file'])) {
64+
continue;
65+
}
66+
if (in_array($line['function'], $this->notAllowedFunctions)
67+
|| in_array($line['class'], $this->notAllowedClasses)
68+
|| strpos($line['file'], 'Interceptor.php') !== false
69+
) {
70+
unset($trace[$index]);
71+
}
72+
unset($trace[$index]['type']);
73+
}
74+
75+
if (function_exists('gzcompress')) {
76+
return bin2hex(
77+
gzcompress(
78+
print_r(
79+
$trace,
80+
true
81+
)
82+
)
83+
);
84+
}
85+
return $trace;
86+
}
87+
}

Model/Indexation/Logger.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
namespace Magento\CloudComponents\Model\Indexation;
1010

11+
use Magento\CloudComponents\Model\DebugTrace;
1112
use Magento\Framework\Indexer\ActionInterface;
1213
use Psr\Log\LoggerInterface;
1314

@@ -21,12 +22,21 @@ class Logger
2122
*/
2223
private $logger;
2324

25+
/**
26+
* @var DebugTrace
27+
*/
28+
private $debugTrace;
29+
2430
/**
2531
* @param LoggerInterface $logger
32+
* @param DebugTrace $debugTrace
2633
*/
27-
public function __construct(LoggerInterface $logger)
28-
{
34+
public function __construct(
35+
LoggerInterface $logger,
36+
DebugTrace $debugTrace
37+
) {
2938
$this->logger = $logger;
39+
$this->debugTrace = $debugTrace;
3040
}
3141

3242
/**
@@ -39,7 +49,7 @@ public function afterExecuteFull(ActionInterface $subject)
3949
$this->logger->debug(
4050
'full_indexation: ' . get_class($subject),
4151
[
42-
'trace' => debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS)
52+
'trace' => $this->debugTrace->getTrace()
4353
]
4454
);
4555
}

0 commit comments

Comments
 (0)