Skip to content

Commit 060eeb9

Browse files
committed
enable Sentry breadcrumbs via Elastica logging
1 parent 4a2c866 commit 060eeb9

File tree

5 files changed

+91
-3
lines changed

5 files changed

+91
-3
lines changed

composer.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@
3232
"phpstan/phpstan-phpunit": "^1.3.13",
3333
"phpstan/phpstan-strict-rules": "^1.5.1",
3434
"phpunit/phpunit": "^10.2.5",
35-
"rector/rector": "^0.17.5"
35+
"psr/log": "^3.0",
36+
"rector/rector": "^0.17.5",
37+
"sentry/sentry": "^3.20.1",
38+
"symfony/http-client": "^6.3.1"
3639
},
3740
"autoload": {
3841
"psr-4": {
@@ -62,7 +65,8 @@
6265
"sort-packages": true,
6366
"allow-plugins": {
6467
"pestphp/pest-plugin": true,
65-
"phpstan/extension-installer": true
68+
"phpstan/extension-installer": true,
69+
"php-http/discovery": true
6670
}
6771
},
6872
"extra": {

config/elastica-bridge.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,7 @@
1010
'events' => [
1111
'listen' => true,
1212
],
13+
'logging' => [
14+
'sentry_breadcrumbs' => false,
15+
],
1316
];

src/Client/ElasticaClient.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Elastica\Client;
88
use Elastica\Index;
9+
use Limenet\LaravelElasticaBridge\Logging\SentryBreadcrumbLogger;
910

1011
class ElasticaClient
1112
{
@@ -15,10 +16,16 @@ class ElasticaClient
1516

1617
public function __construct()
1718
{
18-
$this->client = new Client([
19+
$client = new Client([
1920
'host' => config('elastica-bridge.elasticsearch.host'),
2021
'port' => config('elastica-bridge.elasticsearch.port'),
2122
]);
23+
24+
if (config('elastica-bridge.logging.sentry_breadcrumbs') === true && class_exists('\Sentry\Breadcrumb')) {
25+
$client->setLogger(new SentryBreadcrumbLogger());
26+
}
27+
28+
$this->client = $client;
2229
}
2330

2431
public function getClient(): Client
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Limenet\LaravelElasticaBridge\Logging;
6+
7+
use Psr\Log\LoggerInterface;
8+
use Psr\Log\LoggerTrait;
9+
use Psr\Log\LogLevel;
10+
use Sentry\Breadcrumb;
11+
use Stringable;
12+
13+
class SentryBreadcrumbLogger implements LoggerInterface
14+
{
15+
use LoggerTrait;
16+
17+
/**
18+
* @param LogLevel::*|mixed $level
19+
*/
20+
public function log($level, string|Stringable $message, array $context = []): void
21+
{
22+
if ($message instanceof Stringable) {
23+
$message = $message->__toString();
24+
}
25+
26+
\Sentry\addBreadcrumb(
27+
new Breadcrumb(
28+
match ($level) {
29+
LogLevel::EMERGENCY, LogLevel::CRITICAL => Breadcrumb::LEVEL_FATAL,
30+
LogLevel::ALERT, LogLevel::ERROR => Breadcrumb::LEVEL_ERROR,
31+
LogLevel::WARNING => Breadcrumb::LEVEL_WARNING,
32+
LogLevel::INFO, LogLevel::NOTICE => Breadcrumb::LEVEL_INFO,
33+
LogLevel::DEBUG => Breadcrumb::LEVEL_DEBUG,
34+
default => Breadcrumb::LEVEL_DEBUG,
35+
},
36+
match ($level) {
37+
Breadcrumb::LEVEL_FATAL, Breadcrumb::LEVEL_ERROR => Breadcrumb::TYPE_ERROR,
38+
default => Breadcrumb::TYPE_DEFAULT,
39+
},
40+
'elastica',
41+
$message,
42+
$context
43+
)
44+
);
45+
}
46+
}

tests/Unit/ClientTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44

55
namespace Limenet\LaravelElasticaBridge\Tests\Unit;
66

7+
use Elastica\Client;
78
use Limenet\LaravelElasticaBridge\Client\ElasticaClient;
9+
use Limenet\LaravelElasticaBridge\Logging\SentryBreadcrumbLogger;
10+
use Psr\Log\LoggerInterface;
811

912
class ClientTest extends TestCase
1013
{
@@ -23,5 +26,30 @@ public function test_configured_client(): void
2326

2427
$this->assertSame('localhost', $client->getConfig('host'));
2528
$this->assertEquals(9200, $client->getConfig('port'));
29+
30+
}
31+
32+
public function test_sentry_logger_not_active_by_default(): void
33+
{
34+
$client = $this->elasticaClient->getClient();
35+
36+
$this->assertNotInstanceOf(SentryBreadcrumbLogger::class, $this->getLoggerProperty($client));
37+
}
38+
39+
public function test_sentry_logger_enabled(): void
40+
{
41+
config()->set('elastica-bridge.logging.sentry_breadcrumbs', true);
42+
$client = (new ElasticaClient)->getClient();
43+
44+
$this->assertInstanceOf(SentryBreadcrumbLogger::class, $this->getLoggerProperty($client));
45+
}
46+
47+
private function getLoggerProperty(Client $client): LoggerInterface
48+
{
49+
$reflectedClass = new \ReflectionClass($client);
50+
$reflection = $reflectedClass->getProperty('_logger');
51+
$reflection->setAccessible(true);
52+
53+
return $reflection->getValue($client);
2654
}
2755
}

0 commit comments

Comments
 (0)