File tree Expand file tree Collapse file tree 5 files changed +91
-3
lines changed Expand file tree Collapse file tree 5 files changed +91
-3
lines changed Original file line number Diff line number Diff line change 32
32
"phpstan/phpstan-phpunit" : " ^1.3.13" ,
33
33
"phpstan/phpstan-strict-rules" : " ^1.5.1" ,
34
34
"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"
36
39
},
37
40
"autoload" : {
38
41
"psr-4" : {
62
65
"sort-packages" : true ,
63
66
"allow-plugins" : {
64
67
"pestphp/pest-plugin" : true ,
65
- "phpstan/extension-installer" : true
68
+ "phpstan/extension-installer" : true ,
69
+ "php-http/discovery" : true
66
70
}
67
71
},
68
72
"extra" : {
Original file line number Diff line number Diff line change 10
10
'events ' => [
11
11
'listen ' => true ,
12
12
],
13
+ 'logging ' => [
14
+ 'sentry_breadcrumbs ' => false ,
15
+ ],
13
16
];
Original file line number Diff line number Diff line change 6
6
7
7
use Elastica \Client ;
8
8
use Elastica \Index ;
9
+ use Limenet \LaravelElasticaBridge \Logging \SentryBreadcrumbLogger ;
9
10
10
11
class ElasticaClient
11
12
{
@@ -15,10 +16,16 @@ class ElasticaClient
15
16
16
17
public function __construct ()
17
18
{
18
- $ this -> client = new Client ([
19
+ $ client = new Client ([
19
20
'host ' => config ('elastica-bridge.elasticsearch.host ' ),
20
21
'port ' => config ('elastica-bridge.elasticsearch.port ' ),
21
22
]);
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 ;
22
29
}
23
30
24
31
public function getClient (): Client
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change 4
4
5
5
namespace Limenet \LaravelElasticaBridge \Tests \Unit ;
6
6
7
+ use Elastica \Client ;
7
8
use Limenet \LaravelElasticaBridge \Client \ElasticaClient ;
9
+ use Limenet \LaravelElasticaBridge \Logging \SentryBreadcrumbLogger ;
10
+ use Psr \Log \LoggerInterface ;
8
11
9
12
class ClientTest extends TestCase
10
13
{
@@ -23,5 +26,30 @@ public function test_configured_client(): void
23
26
24
27
$ this ->assertSame ('localhost ' , $ client ->getConfig ('host ' ));
25
28
$ 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 );
26
54
}
27
55
}
You can’t perform that action at this time.
0 commit comments