Skip to content

Commit f7bda4b

Browse files
committed
feat: Add logger integration
1 parent c35e322 commit f7bda4b

File tree

5 files changed

+47
-8
lines changed

5 files changed

+47
-8
lines changed

src/ClientFactory.php

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use Psr\Http\Client\ClientInterface;
2222
use Psr\Http\Message\RequestFactoryInterface;
2323
use Psr\Http\Message\StreamFactoryInterface;
24+
use Psr\Log\LoggerInterface;
2425

2526
/**
2627
* @psalm-import-type SessionConfigArray from Configuration
@@ -33,9 +34,9 @@
3334
class ClientFactory
3435
{
3536
/**
36-
* @param DriverConfigArray|null $driverConfig
37-
* @param SessionConfigArray|null $sessionConfiguration
38-
* @param TransactionConfigArray|null $transactionConfiguration
37+
* @param DriverConfigArray|null $driverConfig
38+
* @param SessionConfigArray|null $sessionConfiguration
39+
* @param TransactionConfigArray|null $transactionConfiguration
3940
* @param list<DriverRegistrationArray> $connections
4041
*/
4142
public function __construct(
@@ -48,6 +49,8 @@ public function __construct(
4849
private ?ClientInterface $client,
4950
private ?StreamFactoryInterface $streamFactory,
5051
private ?RequestFactoryInterface $requestFactory,
52+
private ?string $logLevel,
53+
private ?LoggerInterface $logger,
5154
) {
5255
}
5356

@@ -57,7 +60,9 @@ public function create(): SymfonyClient
5760
$builder = ClientBuilder::create();
5861

5962
if (null !== $this->driverConfig) {
60-
$builder = $builder->withDefaultDriverConfiguration($this->makeDriverConfig());
63+
$builder = $builder->withDefaultDriverConfiguration(
64+
$this->makeDriverConfig($this->logLevel, $this->logger)
65+
);
6166
}
6267

6368
if (null !== $this->sessionConfiguration) {
@@ -84,7 +89,7 @@ public function create(): SymfonyClient
8489
return new SymfonyClient($builder->build(), $this->eventHandler);
8590
}
8691

87-
private function makeDriverConfig(): DriverConfiguration
92+
private function makeDriverConfig(?string $logLevel = null, ?LoggerInterface $logger = null): DriverConfiguration
8893
{
8994
$config = new DriverConfiguration(
9095
userAgent: $this->driverConfig['user_agent'] ?? null,
@@ -94,6 +99,8 @@ private function makeDriverConfig(): DriverConfiguration
9499
cache: null,
95100
acquireConnectionTimeout: $this->driverConfig['acquire_connection_timeout'] ?? null,
96101
semaphore: null,
102+
logLevel: $logLevel,
103+
logger: $logger,
97104
);
98105

99106
$bindings = new HttpPsrBindings();
@@ -145,10 +152,14 @@ private function createAuth(?array $auth, string $dsn): AuthenticateInterface
145152
$auth['username'] ?? throw new \InvalidArgumentException('Missing username for basic authentication'),
146153
$auth['password'] ?? throw new \InvalidArgumentException('Missing password for basic authentication')
147154
),
148-
'kerberos' => Authenticate::kerberos($auth['token'] ?? throw new \InvalidArgumentException('Missing token for kerberos authentication')),
155+
'kerberos' => Authenticate::kerberos(
156+
$auth['token'] ?? throw new \InvalidArgumentException('Missing token for kerberos authentication')
157+
),
149158
'dsn', null => Authenticate::fromUrl(Uri::create($dsn)),
150159
'none' => Authenticate::disabled(),
151-
'oid' => Authenticate::oidc($auth['token'] ?? throw new \InvalidArgumentException('Missing token for oid authentication')),
160+
'oid' => Authenticate::oidc(
161+
$auth['token'] ?? throw new \InvalidArgumentException('Missing token for oid authentication')
162+
),
152163
};
153164
}
154165

src/DependencyInjection/Configuration.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Neo4j\Neo4jBundle\DependencyInjection;
66

77
use Laudis\Neo4j\Databags\DriverConfiguration;
8+
use Psr\Log\LogLevel;
89
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
910
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
1011
use Symfony\Component\Config\Definition\ConfigurationInterface;
@@ -65,6 +66,10 @@ public function getConfigTreeBuilder(): TreeBuilder
6566
->append($this->decorateDriverConfig())
6667
->append($this->decorateSessionConfig())
6768
->append($this->decorateTransactionConfig())
69+
->scalarNode('log_level')
70+
->info('Psr log level to use. Default is "error".')
71+
->defaultValue(LogLevel::ERROR)
72+
->end()
6873
->scalarNode('default_driver')
6974
->info('The default driver to use. Default is the first configured driver.')
7075
->end()

src/DependencyInjection/Neo4jExtension.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Psr\Http\Client\ClientInterface;
1313
use Psr\Http\Message\RequestFactoryInterface;
1414
use Psr\Http\Message\StreamFactoryInterface;
15+
use Psr\Log\LoggerInterface;
1516
use Symfony\Component\Config\FileLocator;
1617
use Symfony\Component\DependencyInjection\ContainerBuilder;
1718
use Symfony\Component\DependencyInjection\ContainerInterface;
@@ -34,7 +35,6 @@ public function load(array $configs, ContainerBuilder $container): ContainerBuil
3435
$loader->load('services.php');
3536

3637
$defaultAlias = $mergedConfig['default_driver'] ?? $mergedConfig['drivers'][0]['alias'] ?? 'default';
37-
3838
$container->setDefinition('neo4j.event_handler', new Definition(EventHandler::class))
3939
->setAutowired(true)
4040
->addTag('neo4j.event_handler')
@@ -55,6 +55,8 @@ public function load(array $configs, ContainerBuilder $container): ContainerBuil
5555
8,
5656
new Reference(RequestFactoryInterface::class, ContainerInterface::NULL_ON_INVALID_REFERENCE)
5757
)
58+
->setArgument(9, $mergedConfig['log_level'] ?? null)
59+
->setArgument(10, new Reference(LoggerInterface::class, ContainerInterface::NULL_ON_INVALID_REFERENCE))
5860
->setAbstract(false);
5961

6062
$container->getDefinition('neo4j.driver')

tests/App/config/default.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ parameters:
3333
neo4j.dsn.simple: bolt://test:test@localhost
3434

3535
neo4j:
36+
log_level: warning
3637
default_driver: neo4j-test
3738
default_driver_config:
3839
acquire_connection_timeout: 10

tests/Functional/IntegrationTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@
1717
use Laudis\Neo4j\Enum\SslMode;
1818
use Laudis\Neo4j\Neo4j\Neo4jConnectionPool;
1919
use Laudis\Neo4j\Neo4j\Neo4jDriver;
20+
use Neo4j\Neo4jBundle\SymfonyClient;
2021
use Neo4j\Neo4jBundle\Tests\App\TestKernel;
2122
use Psr\Http\Message\UriInterface;
23+
use Psr\Log\NullLogger;
2224
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
2325

2426
class IntegrationTest extends KernelTestCase
@@ -232,6 +234,24 @@ public function testPriority(): void
232234
$this->assertSame($extractedValue['priority'], 1000);
233235
}
234236

237+
public function testDefaultLogLevel(): void
238+
{
239+
static::bootKernel();
240+
$container = static::getContainer();
241+
242+
/**
243+
* @var SymfonyClient $client
244+
*/
245+
$client = $container->get('neo4j.client');
246+
/** @var Neo4jDriver $driver */
247+
$driver = $client->getDriver('default');
248+
/** @var Neo4jConnectionPool $pool */
249+
$pool = $this->getPrivateProperty($driver, 'pool');
250+
$level = $pool->getLogger()->getLevel();
251+
252+
$this->assertSame('warning', $level);
253+
}
254+
235255
/**
236256
* @template T
237257
*

0 commit comments

Comments
 (0)