Skip to content

Add sentry logger by hooking into monolog setHandlers #184

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions Controller/Adminhtml/Test/Sentry.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
namespace JustBetter\Sentry\Controller\Adminhtml\Test;

use JustBetter\Sentry\Helper\Data;
use JustBetter\Sentry\Plugin\MonologPlugin;
use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use Magento\Framework\Logger\Monolog;
use Magento\Framework\Serialize\Serializer\Json;
use Magento\Framework\View\Result\PageFactory;
use Psr\Log\LoggerInterface;
Expand All @@ -27,15 +27,15 @@ class Sentry extends Action
* @param Json $jsonSerializer
* @param LoggerInterface $logger
* @param Data $helperSentry
* @param MonologPlugin $monologPlugin
* @param Monolog $monolog
*/
public function __construct(
Context $context,
protected PageFactory $resultPageFactory,
private Json $jsonSerializer,
protected LoggerInterface $logger,
private Data $helperSentry,
private MonologPlugin $monologPlugin
private Monolog $monolog
) {
parent::__construct($context);
}
Expand All @@ -54,7 +54,7 @@ public function execute()
if ($activeWithReason['active']) {
try {
if ($this->helperSentry->isPhpTrackingEnabled()) {
$this->monologPlugin->addRecord(\Monolog\Logger::ALERT, 'TEST message from Magento 2', []);
$this->monolog->addRecord(\Monolog\Logger::ALERT, 'TEST message from Magento 2', []);
$result['status'] = true;
$result['content'] = __('Check sentry.io which should hold an alert');
} else {
Expand Down
58 changes: 40 additions & 18 deletions Plugin/MonologPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,52 @@
namespace JustBetter\Sentry\Plugin;

use JustBetter\Sentry\Logger\Handler\Sentry;
use Magento\Framework\Logger\Monolog;
use Monolog\LogRecord;
use Monolog\Processor\ProcessorInterface;
use Monolog\Logger;

class MonologPlugin extends Monolog
class MonologPlugin
{
/**
* @psalm-param array<callable(array): array> $processors
*
* @param string $name The logging channel, a simple descriptive name that is attached to all log records
* @param Sentry $sentryHandler
* @param \Monolog\Handler\HandlerInterface[] $handlers Optional stack of handlers, the first one in the array is called first, etc.
* @param callable[] $processors Optional array of processors
*
* @phpstan-param array<(callable(LogRecord|array): LogRecord|array)|ProcessorInterface> $processors
* @param Sentry $sentryHandler The sentry handler we will add to all Monolog loggers.
*/
public function __construct(
$name,
Sentry $sentryHandler,
array $handlers = [],
array $processors = []
protected Sentry $sentryHandler,
) {
$handlers['sentry'] = $sentryHandler;
}

/**
* Add the Sentry handler to the Monolog logger if it does not already exist.
*
* @param Logger $subject
* @param array $handlers
*
* @return array
*/
public function beforeSetHandlers(
Logger $subject,
array $handlers
): array {
if (!$this->containsHandler($handlers)) {
array_unshift($handlers, $this->sentryHandler);
}

return [$handlers];
}

/**
* Check if the Sentry handler is already in the list of handlers.
*
* @param array $handlers
*
* @return bool
*/
public function containsHandler(array $handlers): bool
{
foreach ($handlers as $handler) {
if ($handler instanceof Sentry) {
return true;
}
}

parent::__construct($name, $handlers, $processors);
return false;
}
}
12 changes: 10 additions & 2 deletions etc/di.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<!-- Cannot use plugin https://github.com/magento/magento2/issues/14950 -->
<preference for="Magento\Framework\Logger\Monolog" type="JustBetter\Sentry\Plugin\MonologPlugin"/>
<type name="Monolog\Logger">
<plugin disabled="false" name="SentryMonologPlugin" type="JustBetter\Sentry\Plugin\MonologPlugin" sortOrder="10"/>
</type>

<type name="JustBetter\Sentry\Plugin\MonologPlugin">
<arguments>
<argument name="sentryHandler" xsi:type="object">JustBetter\Sentry\Logger\Handler\Sentry\Proxy</argument>
</arguments>
</type>

<type name="Magento\Framework\AppInterface">
<plugin disabled="false" name="GlobalExceptionCatcher" type="JustBetter\Sentry\Plugin\GlobalExceptionCatcher"
sortOrder="0"/>
Expand Down