Skip to content

Commit b7eda4b

Browse files
authored
Merge pull request #89 from neo4j-php/feat/logger-integration
feat: Add logger integration
2 parents c35e322 + 2d3db7d commit b7eda4b

File tree

6 files changed

+44
-6
lines changed

6 files changed

+44
-6
lines changed

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
],
1313
"require": {
1414
"php": ">=8.1",
15-
"laudis/neo4j-php-client": "^3.1",
15+
"laudis/neo4j-php-client": "^3.2",
1616
"twig/twig": "^3.0",
1717
"ext-json": "*",
1818
"symfony/dependency-injection": "^5.4 || ^6.0 || ^7.0",

src/ClientFactory.php

+15-4
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
@@ -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

+5
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('min_log_level')
70+
->info('Minimum severity the driver will log. Follows Psr LogLevel. 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

+3-1
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['min_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

+1
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+
min_log_level: warning
3637
default_driver: neo4j-test
3738
default_driver_config:
3839
acquire_connection_timeout: 10

tests/Functional/IntegrationTest.php

+19
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
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;
2223
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
@@ -232,6 +233,24 @@ public function testPriority(): void
232233
$this->assertSame($extractedValue['priority'], 1000);
233234
}
234235

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

0 commit comments

Comments
 (0)