Skip to content

Commit

Permalink
Dynamically set redis log_level to 7 (#5)
Browse files Browse the repository at this point in the history
Dynamically set redis `log_level` to 7
  • Loading branch information
convenient authored Dec 4, 2022
1 parent dc1c559 commit 4f7d4ba
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,14 @@ Compatible with Magento 2.4.1 and higher.
- Bundled in this module is a cache decorator which will add to the vanilla offering of logging `cache_invalidate` with `cache_load` and `cache_save` information.
- This uses a virtual type `Ampersand\VerboseLogRequest\Logger\VerboseDebugLogger` which calls to `->debug()` on the `Psr\Log\LoggerInterface`.
- See `src/CacheDecorator/VerboseLogger.php`
- Set redis session log level to debug mode (level 7)
- Allows you to see redis lock acquisitions, lock waits, zombie processes, etc
- https://github.com/colinmollenhour/Cm_RedisSession#configuration-example

## Example Usage

On the system you want to debug run the following command to get the current key

```
$ php bin/magento ampersand:verbose-log-request:get-key
The current key is: d07c0ee76154d48c2974516ef22c1ec0
Expand Down
43 changes: 43 additions & 0 deletions src/Plugin/AdjustRedisLogLevel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php
declare(strict_types=1);
namespace Ampersand\VerboseLogRequest\Plugin;

use Ampersand\VerboseLogRequest\Service\IsVerbose;
use Magento\Framework\Session\SaveHandler\Redis\Config as RedisConfig;

class AdjustRedisLogLevel
{
/**
* @var IsVerbose
*/
private IsVerbose $isVerbose;

/**
* @param IsVerbose $isVerbose
*/
public function __construct(
IsVerbose $isVerbose
) {
$this->isVerbose = $isVerbose;
}

/**
* Bump redis log level to 7 when on a verbose request.
*
* @param RedisConfig $subject
* @param string $result
* @return mixed|string
*/
public function afterGetLogLevel(RedisConfig $subject, $result)
{
if ($this->isVerbose->isVerbose()) {
/**
* 7 (debug: the most information for development/testing)
*
* @link https://github.com/colinmollenhour/Cm_RedisSession#configuration-example
*/
return '7';
}
return $result;
}
}
26 changes: 26 additions & 0 deletions src/Test/Integration/RedisLogLevelTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
declare(strict_types=1);

namespace Ampersand\VerboseLogRequest\Test\Integration;

use Ampersand\VerboseLogRequest\Service\IsVerbose\Storage;
use Magento\Framework\Session\SaveHandler\Redis\Config as RedisConfig;
use Magento\TestFramework\Helper\Bootstrap;
use PHPUnit\Framework\TestCase;

class RedisLogLevelTest extends TestCase
{
public function testGetDefaultLogLevel()
{
$redisConfig = Bootstrap::getObjectManager()->get(RedisConfig::class);
$this->assertNull($redisConfig->getLogLevel(), 'Default log level is not null');
}

public function testGetVerboseLogLevel()
{
Storage::setFlag(true, true);
$redisConfig = Bootstrap::getObjectManager()->get(RedisConfig::class);
$this->assertEquals('7', $redisConfig->getLogLevel(), 'Default log level is not 7');
Storage::reset(true);
}
}
5 changes: 5 additions & 0 deletions src/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,9 @@
</argument>
</arguments>
</type>

<type name="Cm\RedisSession\Handler\ConfigInterface">
<plugin name="ampersand_verboselogrequest_adjust_redis_log_level" type="\Ampersand\VerboseLogRequest\Plugin\AdjustRedisLogLevel" sortOrder="1" disabled="false" />
</type>

</config>

0 comments on commit 4f7d4ba

Please sign in to comment.