From f7bda4bf761ad32e2b703d1934989d8898d292d6 Mon Sep 17 00:00:00 2001 From: exaby73 Date: Mon, 25 Nov 2024 12:06:56 +0530 Subject: [PATCH 1/4] feat: Add logger integration --- src/ClientFactory.php | 25 ++++++++++++++++------ src/DependencyInjection/Configuration.php | 5 +++++ src/DependencyInjection/Neo4jExtension.php | 4 +++- tests/App/config/default.yml | 1 + tests/Functional/IntegrationTest.php | 20 +++++++++++++++++ 5 files changed, 47 insertions(+), 8 deletions(-) diff --git a/src/ClientFactory.php b/src/ClientFactory.php index 7d8d419..90a7830 100644 --- a/src/ClientFactory.php +++ b/src/ClientFactory.php @@ -21,6 +21,7 @@ use Psr\Http\Client\ClientInterface; use Psr\Http\Message\RequestFactoryInterface; use Psr\Http\Message\StreamFactoryInterface; +use Psr\Log\LoggerInterface; /** * @psalm-import-type SessionConfigArray from Configuration @@ -33,9 +34,9 @@ class ClientFactory { /** - * @param DriverConfigArray|null $driverConfig - * @param SessionConfigArray|null $sessionConfiguration - * @param TransactionConfigArray|null $transactionConfiguration + * @param DriverConfigArray|null $driverConfig + * @param SessionConfigArray|null $sessionConfiguration + * @param TransactionConfigArray|null $transactionConfiguration * @param list $connections */ public function __construct( @@ -48,6 +49,8 @@ public function __construct( private ?ClientInterface $client, private ?StreamFactoryInterface $streamFactory, private ?RequestFactoryInterface $requestFactory, + private ?string $logLevel, + private ?LoggerInterface $logger, ) { } @@ -57,7 +60,9 @@ public function create(): SymfonyClient $builder = ClientBuilder::create(); if (null !== $this->driverConfig) { - $builder = $builder->withDefaultDriverConfiguration($this->makeDriverConfig()); + $builder = $builder->withDefaultDriverConfiguration( + $this->makeDriverConfig($this->logLevel, $this->logger) + ); } if (null !== $this->sessionConfiguration) { @@ -84,7 +89,7 @@ public function create(): SymfonyClient return new SymfonyClient($builder->build(), $this->eventHandler); } - private function makeDriverConfig(): DriverConfiguration + private function makeDriverConfig(?string $logLevel = null, ?LoggerInterface $logger = null): DriverConfiguration { $config = new DriverConfiguration( userAgent: $this->driverConfig['user_agent'] ?? null, @@ -94,6 +99,8 @@ private function makeDriverConfig(): DriverConfiguration cache: null, acquireConnectionTimeout: $this->driverConfig['acquire_connection_timeout'] ?? null, semaphore: null, + logLevel: $logLevel, + logger: $logger, ); $bindings = new HttpPsrBindings(); @@ -145,10 +152,14 @@ private function createAuth(?array $auth, string $dsn): AuthenticateInterface $auth['username'] ?? throw new \InvalidArgumentException('Missing username for basic authentication'), $auth['password'] ?? throw new \InvalidArgumentException('Missing password for basic authentication') ), - 'kerberos' => Authenticate::kerberos($auth['token'] ?? throw new \InvalidArgumentException('Missing token for kerberos authentication')), + 'kerberos' => Authenticate::kerberos( + $auth['token'] ?? throw new \InvalidArgumentException('Missing token for kerberos authentication') + ), 'dsn', null => Authenticate::fromUrl(Uri::create($dsn)), 'none' => Authenticate::disabled(), - 'oid' => Authenticate::oidc($auth['token'] ?? throw new \InvalidArgumentException('Missing token for oid authentication')), + 'oid' => Authenticate::oidc( + $auth['token'] ?? throw new \InvalidArgumentException('Missing token for oid authentication') + ), }; } diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index a713ba2..409b92f 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -5,6 +5,7 @@ namespace Neo4j\Neo4jBundle\DependencyInjection; use Laudis\Neo4j\Databags\DriverConfiguration; +use Psr\Log\LogLevel; use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition; use Symfony\Component\Config\Definition\Builder\TreeBuilder; use Symfony\Component\Config\Definition\ConfigurationInterface; @@ -65,6 +66,10 @@ public function getConfigTreeBuilder(): TreeBuilder ->append($this->decorateDriverConfig()) ->append($this->decorateSessionConfig()) ->append($this->decorateTransactionConfig()) + ->scalarNode('log_level') + ->info('Psr log level to use. Default is "error".') + ->defaultValue(LogLevel::ERROR) + ->end() ->scalarNode('default_driver') ->info('The default driver to use. Default is the first configured driver.') ->end() diff --git a/src/DependencyInjection/Neo4jExtension.php b/src/DependencyInjection/Neo4jExtension.php index 339bf49..fdc3d16 100644 --- a/src/DependencyInjection/Neo4jExtension.php +++ b/src/DependencyInjection/Neo4jExtension.php @@ -12,6 +12,7 @@ use Psr\Http\Client\ClientInterface; use Psr\Http\Message\RequestFactoryInterface; use Psr\Http\Message\StreamFactoryInterface; +use Psr\Log\LoggerInterface; use Symfony\Component\Config\FileLocator; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -34,7 +35,6 @@ public function load(array $configs, ContainerBuilder $container): ContainerBuil $loader->load('services.php'); $defaultAlias = $mergedConfig['default_driver'] ?? $mergedConfig['drivers'][0]['alias'] ?? 'default'; - $container->setDefinition('neo4j.event_handler', new Definition(EventHandler::class)) ->setAutowired(true) ->addTag('neo4j.event_handler') @@ -55,6 +55,8 @@ public function load(array $configs, ContainerBuilder $container): ContainerBuil 8, new Reference(RequestFactoryInterface::class, ContainerInterface::NULL_ON_INVALID_REFERENCE) ) + ->setArgument(9, $mergedConfig['log_level'] ?? null) + ->setArgument(10, new Reference(LoggerInterface::class, ContainerInterface::NULL_ON_INVALID_REFERENCE)) ->setAbstract(false); $container->getDefinition('neo4j.driver') diff --git a/tests/App/config/default.yml b/tests/App/config/default.yml index 4b7102d..e902b6c 100644 --- a/tests/App/config/default.yml +++ b/tests/App/config/default.yml @@ -33,6 +33,7 @@ parameters: neo4j.dsn.simple: bolt://test:test@localhost neo4j: + log_level: warning default_driver: neo4j-test default_driver_config: acquire_connection_timeout: 10 diff --git a/tests/Functional/IntegrationTest.php b/tests/Functional/IntegrationTest.php index 8020681..a270e4d 100644 --- a/tests/Functional/IntegrationTest.php +++ b/tests/Functional/IntegrationTest.php @@ -17,8 +17,10 @@ use Laudis\Neo4j\Enum\SslMode; use Laudis\Neo4j\Neo4j\Neo4jConnectionPool; use Laudis\Neo4j\Neo4j\Neo4jDriver; +use Neo4j\Neo4jBundle\SymfonyClient; use Neo4j\Neo4jBundle\Tests\App\TestKernel; use Psr\Http\Message\UriInterface; +use Psr\Log\NullLogger; use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; class IntegrationTest extends KernelTestCase @@ -232,6 +234,24 @@ public function testPriority(): void $this->assertSame($extractedValue['priority'], 1000); } + public function testDefaultLogLevel(): void + { + static::bootKernel(); + $container = static::getContainer(); + + /** + * @var SymfonyClient $client + */ + $client = $container->get('neo4j.client'); + /** @var Neo4jDriver $driver */ + $driver = $client->getDriver('default'); + /** @var Neo4jConnectionPool $pool */ + $pool = $this->getPrivateProperty($driver, 'pool'); + $level = $pool->getLogger()->getLevel(); + + $this->assertSame('warning', $level); + } + /** * @template T * From 82bf580b5b38d3b654f93ca30f6894be711d8a58 Mon Sep 17 00:00:00 2001 From: exaby73 Date: Mon, 25 Nov 2024 12:27:38 +0530 Subject: [PATCH 2/4] style: Run CS fixer --- src/ClientFactory.php | 6 +++--- tests/Functional/IntegrationTest.php | 1 - 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/ClientFactory.php b/src/ClientFactory.php index 90a7830..c2eb70c 100644 --- a/src/ClientFactory.php +++ b/src/ClientFactory.php @@ -34,9 +34,9 @@ class ClientFactory { /** - * @param DriverConfigArray|null $driverConfig - * @param SessionConfigArray|null $sessionConfiguration - * @param TransactionConfigArray|null $transactionConfiguration + * @param DriverConfigArray|null $driverConfig + * @param SessionConfigArray|null $sessionConfiguration + * @param TransactionConfigArray|null $transactionConfiguration * @param list $connections */ public function __construct( diff --git a/tests/Functional/IntegrationTest.php b/tests/Functional/IntegrationTest.php index a270e4d..9bd0f7c 100644 --- a/tests/Functional/IntegrationTest.php +++ b/tests/Functional/IntegrationTest.php @@ -20,7 +20,6 @@ use Neo4j\Neo4jBundle\SymfonyClient; use Neo4j\Neo4jBundle\Tests\App\TestKernel; use Psr\Http\Message\UriInterface; -use Psr\Log\NullLogger; use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; class IntegrationTest extends KernelTestCase From 3dad140148fbd35afcc65617db78500d3bd07cdf Mon Sep 17 00:00:00 2001 From: exaby73 Date: Tue, 3 Dec 2024 12:18:37 +0530 Subject: [PATCH 3/4] feat: Update Neo4j dep --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 1bf4ae2..9c446f8 100644 --- a/composer.json +++ b/composer.json @@ -12,7 +12,7 @@ ], "require": { "php": ">=8.1", - "laudis/neo4j-php-client": "^3.1", + "laudis/neo4j-php-client": "^3.2", "twig/twig": "^3.0", "ext-json": "*", "symfony/dependency-injection": "^5.4 || ^6.0 || ^7.0", From 2d3db7d9f467a7b7e9b783f00095d78fad9f82e1 Mon Sep 17 00:00:00 2001 From: exaby73 Date: Tue, 3 Dec 2024 12:23:30 +0530 Subject: [PATCH 4/4] feat: Changed log_level to min_log_level in Neo4jExtension --- src/DependencyInjection/Configuration.php | 4 ++-- src/DependencyInjection/Neo4jExtension.php | 2 +- tests/App/config/default.yml | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index 409b92f..b21b8e9 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -66,8 +66,8 @@ public function getConfigTreeBuilder(): TreeBuilder ->append($this->decorateDriverConfig()) ->append($this->decorateSessionConfig()) ->append($this->decorateTransactionConfig()) - ->scalarNode('log_level') - ->info('Psr log level to use. Default is "error".') + ->scalarNode('min_log_level') + ->info('Minimum severity the driver will log. Follows Psr LogLevel. Default is "error".') ->defaultValue(LogLevel::ERROR) ->end() ->scalarNode('default_driver') diff --git a/src/DependencyInjection/Neo4jExtension.php b/src/DependencyInjection/Neo4jExtension.php index fdc3d16..3f03457 100644 --- a/src/DependencyInjection/Neo4jExtension.php +++ b/src/DependencyInjection/Neo4jExtension.php @@ -55,7 +55,7 @@ public function load(array $configs, ContainerBuilder $container): ContainerBuil 8, new Reference(RequestFactoryInterface::class, ContainerInterface::NULL_ON_INVALID_REFERENCE) ) - ->setArgument(9, $mergedConfig['log_level'] ?? null) + ->setArgument(9, $mergedConfig['min_log_level'] ?? null) ->setArgument(10, new Reference(LoggerInterface::class, ContainerInterface::NULL_ON_INVALID_REFERENCE)) ->setAbstract(false); diff --git a/tests/App/config/default.yml b/tests/App/config/default.yml index e902b6c..2e635f1 100644 --- a/tests/App/config/default.yml +++ b/tests/App/config/default.yml @@ -33,7 +33,7 @@ parameters: neo4j.dsn.simple: bolt://test:test@localhost neo4j: - log_level: warning + min_log_level: warning default_driver: neo4j-test default_driver_config: acquire_connection_timeout: 10