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", diff --git a/src/ClientFactory.php b/src/ClientFactory.php index 7d8d419..c2eb70c 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 @@ -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..b21b8e9 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('min_log_level') + ->info('Minimum severity the driver will log. Follows Psr LogLevel. 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..3f03457 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['min_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..2e635f1 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: + min_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..9bd0f7c 100644 --- a/tests/Functional/IntegrationTest.php +++ b/tests/Functional/IntegrationTest.php @@ -17,6 +17,7 @@ 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 Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; @@ -232,6 +233,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 *