Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add logger integration #89

Merged
merged 4 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
19 changes: 15 additions & 4 deletions src/ClientFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -48,6 +49,8 @@ public function __construct(
private ?ClientInterface $client,
private ?StreamFactoryInterface $streamFactory,
private ?RequestFactoryInterface $requestFactory,
private ?string $logLevel,
private ?LoggerInterface $logger,
) {
}

Expand All @@ -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) {
Expand All @@ -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,
Expand All @@ -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();
Expand Down Expand Up @@ -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')
),
};
}

Expand Down
5 changes: 5 additions & 0 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()
Expand Down
4 changes: 3 additions & 1 deletion src/DependencyInjection/Neo4jExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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')
Expand All @@ -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')
Expand Down
1 change: 1 addition & 0 deletions tests/App/config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 19 additions & 0 deletions tests/Functional/IntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
*
Expand Down
Loading