Skip to content

Commit d52185a

Browse files
authored
Merge pull request #16 from ahacart-adexos/feature/adding_socket_and_updating_plugin
Add Socket Handler, Update Handler Injection, and Processor Compatibility
2 parents e2a78e3 + 1575567 commit d52185a

12 files changed

+145
-231
lines changed

Handler/ConsoleHandler.php

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,29 +15,11 @@
1515

1616
class ConsoleHandler implements MagentoHandlerInterface
1717
{
18-
/**
19-
* @var string
20-
*/
21-
private $isEnabled;
22-
23-
/**
24-
* @var ScopeConfigInterface
25-
*/
26-
private $scopeConfig;
27-
28-
/**
29-
* @var string
30-
*/
31-
private $levelPath;
32-
3318
public function __construct(
34-
ScopeConfigInterface $scopeConfig,
35-
string $isEnabled,
36-
string $levelPath
19+
private ScopeConfigInterface $scopeConfig,
20+
private string $isEnabled,
21+
private string $levelPath
3722
) {
38-
$this->scopeConfig = $scopeConfig;
39-
$this->isEnabled = $isEnabled;
40-
$this->levelPath = $levelPath;
4123
}
4224

4325
/**

Handler/GelfHandler.php

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -15,36 +15,12 @@
1515

1616
class GelfHandler implements MagentoHandlerInterface
1717
{
18-
/**
19-
* @var PublisherInterface
20-
*/
21-
private $publisher;
22-
23-
/**
24-
* @var string
25-
*/
26-
private $isEnabled;
27-
28-
/**
29-
* @var string
30-
*/
31-
private $levelPath;
32-
33-
/**
34-
* @var ScopeConfigInterface
35-
*/
36-
private $scopeConfig;
37-
3818
public function __construct(
39-
PublisherInterface $publisher,
40-
ScopeConfigInterface $scopeConfig,
41-
string $isEnabled,
42-
string $levelPath
19+
private PublisherInterface $publisher,
20+
private ScopeConfigInterface $scopeConfig,
21+
private string $isEnabled,
22+
private string $levelPath
4323
) {
44-
$this->publisher = $publisher;
45-
$this->isEnabled = $isEnabled;
46-
$this->levelPath = $levelPath;
47-
$this->scopeConfig = $scopeConfig;
4824
}
4925

5026
public function getInstance(): HandlerInterface

Handler/MailHandler.php

Lines changed: 6 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -14,50 +14,14 @@
1414

1515
class MailHandler implements MagentoHandlerInterface
1616
{
17-
/**
18-
* @var ScopeConfigInterface
19-
*/
20-
private $scopeConfig;
21-
22-
/**
23-
* @var string
24-
*/
25-
private $isEnabled;
26-
27-
/**
28-
* @var string
29-
*/
30-
private $toPath;
31-
32-
/**
33-
* @var string
34-
*/
35-
private $subjectPath;
36-
37-
/**
38-
* @var string
39-
*/
40-
private $fromPath;
41-
42-
/**
43-
* @var string
44-
*/
45-
private $levelPath;
46-
4717
public function __construct(
48-
ScopeConfigInterface $scopeConfig,
49-
string $isEnabled,
50-
string $toPath,
51-
string $subjectPath,
52-
string $fromPath,
53-
string $levelPath
18+
private ScopeConfigInterface $scopeConfig,
19+
private string $isEnabled,
20+
private string $toPath,
21+
private string $subjectPath,
22+
private string $fromPath,
23+
private string $levelPath
5424
) {
55-
$this->scopeConfig = $scopeConfig;
56-
$this->isEnabled = $isEnabled;
57-
$this->toPath = $toPath;
58-
$this->subjectPath = $subjectPath;
59-
$this->fromPath = $fromPath;
60-
$this->levelPath = $levelPath;
6125
}
6226

6327
public function getInstance(): HandlerInterface

Handler/RotatingFileHandler.php

Lines changed: 6 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -15,50 +15,14 @@
1515

1616
class RotatingFileHandler implements MagentoHandlerInterface
1717
{
18-
/**
19-
* @var ScopeConfigInterface
20-
*/
21-
private $scopeConfig;
22-
23-
/**
24-
* @var DirectoryList
25-
*/
26-
private $directoryList;
27-
28-
/**
29-
* @var string
30-
*/
31-
private $isEnabled;
32-
33-
/**
34-
* @var string
35-
*/
36-
private $levelPath;
37-
38-
/**
39-
* @var string
40-
*/
41-
private $filenamePath;
42-
43-
/**
44-
* @var string
45-
*/
46-
private $maxFilesPath;
47-
4818
public function __construct(
49-
ScopeConfigInterface $scopeConfig,
50-
DirectoryList $directoryList,
51-
string $isEnabled,
52-
string $levelPath,
53-
string $filenamePath,
54-
string $maxFilesPath
19+
private ScopeConfigInterface $scopeConfig,
20+
private DirectoryList $directoryList,
21+
private string $isEnabled,
22+
private string $levelPath,
23+
private string $filenamePath,
24+
private string $maxFilesPath
5525
) {
56-
$this->scopeConfig = $scopeConfig;
57-
$this->directoryList = $directoryList;
58-
$this->isEnabled = $isEnabled;
59-
$this->levelPath = $levelPath;
60-
$this->filenamePath = $filenamePath;
61-
$this->maxFilesPath = $maxFilesPath;
6226
}
6327

6428
/**

Handler/SlackHandler.php

Lines changed: 11 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -15,85 +15,19 @@
1515

1616
class SlackHandler implements MagentoHandlerInterface
1717
{
18-
/**
19-
* @var ScopeConfigInterface
20-
*/
21-
private $scopeConfig;
22-
23-
/**
24-
* @var string
25-
*/
26-
private $isEnabled;
27-
28-
/**
29-
* @var string
30-
*/
31-
private $levelPath;
32-
33-
/**
34-
* @var string
35-
*/
36-
private $tokenPath;
37-
38-
/**
39-
* @var string
40-
*/
41-
private $channelPath;
42-
43-
/**
44-
* @var string
45-
*/
46-
private $usernamePath;
47-
48-
/**
49-
* @var string
50-
*/
51-
private $useAttachmentPath;
52-
53-
/**
54-
* @var string
55-
*/
56-
private $iconEmojiPath;
57-
58-
/**
59-
* @var string
60-
*/
61-
private $bubblePath;
62-
63-
/**
64-
* @var string
65-
*/
66-
private $useShortAttachmentPath;
67-
68-
/**
69-
* @var string
70-
*/
71-
private $includeContextAndExtraPath;
72-
7318
public function __construct(
74-
ScopeConfigInterface $scopeConfig,
75-
string $isEnabled,
76-
string $levelPath,
77-
string $tokenPath,
78-
string $channelPath,
79-
string $usernamePath,
80-
string $useAttachmentPath,
81-
string $iconEmojiPath,
82-
string $bubblePath,
83-
string $useShortAttachmentPath,
84-
string $includeContextAndExtraPath
19+
private ScopeConfigInterface $scopeConfig,
20+
private string $isEnabled,
21+
private string $levelPath,
22+
private string $tokenPath,
23+
private string $channelPath,
24+
private string $usernamePath,
25+
private string $useAttachmentPath,
26+
private string $iconEmojiPath,
27+
private string $bubblePath,
28+
private string $useShortAttachmentPath,
29+
private string $includeContextAndExtraPath
8530
) {
86-
$this->scopeConfig = $scopeConfig;
87-
$this->isEnabled = $isEnabled;
88-
$this->levelPath = $levelPath;
89-
$this->tokenPath = $tokenPath;
90-
$this->channelPath = $channelPath;
91-
$this->usernamePath = $usernamePath;
92-
$this->useAttachmentPath = $useAttachmentPath;
93-
$this->iconEmojiPath = $iconEmojiPath;
94-
$this->bubblePath = $bubblePath;
95-
$this->useShortAttachmentPath = $useShortAttachmentPath;
96-
$this->includeContextAndExtraPath = $includeContextAndExtraPath;
9731
}
9832

9933
/**

Handler/SocketHandler.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
/**
3+
* Copyright © OpenGento, All rights reserved.
4+
* See LICENSE bundled with this library for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Opengento\Logger\Handler;
10+
11+
use Exception;
12+
use Magento\Framework\App\Config\ScopeConfigInterface;
13+
use Monolog\Handler\HandlerInterface;
14+
use Monolog\Handler\SocketHandler as MonologSocketHandler;
15+
use Monolog\Formatter\JsonFormatter;
16+
use RuntimeException;
17+
18+
class SocketHandler implements MagentoHandlerInterface
19+
{
20+
public function __construct(
21+
private ScopeConfigInterface $scopeConfig,
22+
private string $isEnabled,
23+
private string $levelPath,
24+
private string $endpoint
25+
) {
26+
}
27+
28+
/**
29+
* @throws RuntimeException
30+
*/
31+
public function getInstance(): HandlerInterface
32+
{
33+
$endpointUrl = trim((string) $this->scopeConfig->getValue($this->endpoint));
34+
if ($endpointUrl === '') {
35+
throw new RuntimeException(sprintf(
36+
'Config key "%s" is missing or empty.',
37+
$this->endpoint
38+
));
39+
}
40+
41+
$handler = new MonologSocketHandler($endpointUrl, $this->scopeConfig->getValue($this->levelPath));
42+
$handler->setFormatter(new JsonFormatter(JsonFormatter::BATCH_MODE_NEWLINES));
43+
44+
return $handler;
45+
}
46+
47+
public function isEnabled(): bool
48+
{
49+
return $this->scopeConfig->isSetFlag($this->isEnabled);
50+
}
51+
}

Plugin/MonologPlugin.php

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,31 @@
88

99
namespace Opengento\Logger\Plugin;
1010

11-
use Magento\Framework\Logger\Monolog;
12-
use Monolog\Handler\HandlerInterface;
11+
use Monolog\Logger;
1312
use Opengento\Logger\Handler\MagentoHandlerInterface;
1413

1514
class MonologPlugin
1615
{
17-
public function beforeSetHandlers(Monolog $subject, array $handlers): array
16+
/**
17+
* @param MagentoHandlerInterface[] $magentoHandlers
18+
*/
19+
public function __construct(private array $magentoHandlers)
1820
{
19-
$magentoHandlers = [];
20-
foreach ($handlers as $key => $handler) {
21-
if ($handler instanceof MagentoHandlerInterface && $handler->isEnabled()) {
22-
$magentoHandlers[$key] = $handler->getInstance();
23-
} elseif ($handler instanceof HandlerInterface) {
24-
$magentoHandlers[$key] = $handler;
21+
}
22+
23+
/**
24+
* @param Logger $subject
25+
* @param array $handlers
26+
* @return array
27+
*/
28+
public function beforeSetHandlers(Logger $subject, array $handlers): array
29+
{
30+
foreach ($this->magentoHandlers as $handler) {
31+
if ($handler->isEnabled()) {
32+
array_unshift($handlers, $handler->getInstance());
2533
}
2634
}
2735

28-
return [$magentoHandlers];
36+
return [$handlers];
2937
}
3038
}

Processor/CustomContextProcessor.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Magento\Framework\App\Config\ScopeConfigInterface;
1212
use Magento\Framework\Serialize\Serializer\Json;
1313
use Magento\Store\Model\ScopeInterface;
14+
use Monolog\LogRecord;
1415
use Monolog\Processor\ProcessorInterface;
1516

1617
class CustomContextProcessor implements ProcessorInterface
@@ -31,7 +32,7 @@ public function __construct(ScopeConfigInterface $scopeConfig, Json $serializer)
3132
$this->serializer = $serializer;
3233
}
3334

34-
public function __invoke(array $records): array
35+
public function __invoke(LogRecord $records): LogRecord
3536
{
3637
foreach ($this->resolveTypesLogger() as $value) {
3738
$records['context'][$value['custom_logger_key']] = $value['custom_logger_value'];

0 commit comments

Comments
 (0)