Skip to content

Commit 23d674b

Browse files
committed
feat(logger): Implement custom logger injection for Typesense client
1 parent 05adecd commit 23d674b

2 files changed

Lines changed: 20 additions & 45 deletions

File tree

src/Lib/Configuration.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,15 @@ public function __construct(array $config)
111111
$this->retryIntervalSeconds = (float)($config['retry_interval_seconds'] ?? 1.0);
112112

113113
// Allow custom logger injection
114-
if (isset($config['logger']) && $config['logger'] instanceof LoggerInterface) {
114+
if (isset($config['logger'])) {
115+
if (!$config['logger'] instanceof LoggerInterface) {
116+
throw new \InvalidArgumentException('Logger must implement Psr\Log\LoggerInterface');
117+
}
118+
119+
if (isset($config['log_level'])) {
120+
throw new \InvalidArgumentException('Setting log_level is not allowed when a custom logger is provided.');
121+
}
122+
115123
$this->logger = $config['logger'];
116124
} else {
117125
$this->logLevel = $config['log_level'] ?? Logger::WARNING;

tests/Feature/ConfigurationTest.php

Lines changed: 11 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
use Monolog\Handler\StreamHandler;
66
use Monolog\Logger;
77
use PHPUnit\Framework\TestCase;
8-
use Psr\Log\LoggerInterface;
98
use Typesense\Exceptions\ConfigError;
109
use Typesense\Lib\Configuration;
1110

@@ -33,7 +32,6 @@ public function testConfigurationWithDefaultLogger(): void
3332

3433
$logger = $config->getLogger();
3534

36-
$this->assertInstanceOf(LoggerInterface::class, $logger);
3735
$this->assertInstanceOf(Logger::class, $logger);
3836
}
3937

@@ -53,7 +51,6 @@ public function testConfigurationWithCustomLogger(): void
5351
$logger = $config->getLogger();
5452

5553
// Assert that the logger is the same instance we passed
56-
$this->assertInstanceOf(LoggerInterface::class, $logger);
5754
$this->assertSame($customLogger, $logger);
5855
$this->assertEquals('custom-test-logger', $logger->getName());
5956
}
@@ -68,66 +65,36 @@ public function testConfigurationWithCustomLogLevel(): void
6865
$config = new Configuration($configWithLogLevel);
6966

7067
$logger = $config->getLogger();
71-
72-
$this->assertInstanceOf(LoggerInterface::class, $logger);
7368
$this->assertInstanceOf(Logger::class, $logger);
7469
}
7570

76-
public function testConfigurationWithCustomLoggerOverridesLogLevel(): void
71+
public function testConfigurationWithCustomLoggerThrowsExceptionWhenLogLevelIsAlsoProvided(): void
7772
{
78-
// Create a custom logger
73+
$this->expectException(\InvalidArgumentException::class);
74+
$this->expectExceptionMessage('Setting log_level is not allowed when a custom logger is provided.');
75+
7976
$customLogger = new Logger('custom-logger-with-level');
8077
$customLogger->pushHandler(new StreamHandler('php://stdout', Logger::ERROR));
8178

82-
// Add both custom logger and log level to config (logger should win)
8379
$configWithBoth = array_merge($this->baseConfig, [
8480
'logger' => $customLogger,
85-
'log_level' => Logger::DEBUG, // This should be ignored
81+
'log_level' => Logger::DEBUG,
8682
]);
8783

88-
$config = new Configuration($configWithBoth);
89-
90-
$logger = $config->getLogger();
91-
92-
// Assert that the custom logger is used, not a new one with log_level
93-
$this->assertSame($customLogger, $logger);
94-
$this->assertEquals('custom-logger-with-level', $logger->getName());
84+
new Configuration($configWithBoth);
9585
}
9686

97-
public function testConfigurationWithMockLogger(): void
87+
public function testConfigurationWithInvalidLoggerThrowsException(): void
9888
{
99-
// Create a mock logger for testing
100-
$mockLogger = $this->createMock(LoggerInterface::class);
101-
102-
// Add mock logger to config
103-
$configWithMockLogger = array_merge($this->baseConfig, [
104-
'logger' => $mockLogger,
105-
]);
106-
107-
$config = new Configuration($configWithMockLogger);
89+
$this->expectException(\InvalidArgumentException::class);
90+
$this->expectExceptionMessage('Logger must implement Psr\Log\LoggerInterface');
10891

109-
$logger = $config->getLogger();
110-
111-
// Assert that the logger is the same instance we passed
112-
$this->assertInstanceOf(LoggerInterface::class, $logger);
113-
$this->assertSame($mockLogger, $logger);
114-
}
115-
116-
public function testConfigurationWithInvalidLoggerIgnored(): void
117-
{
118-
// Try to pass a non-logger object (should fall back to default)
92+
// Try to pass a non-logger object (should throw exception)
11993
$configWithInvalidLogger = array_merge($this->baseConfig, [
12094
'logger' => 'not-a-logger-instance',
12195
]);
12296

123-
$config = new Configuration($configWithInvalidLogger);
124-
125-
$logger = $config->getLogger();
126-
127-
// Should fall back to default logger
128-
$this->assertInstanceOf(LoggerInterface::class, $logger);
129-
$this->assertInstanceOf(Logger::class, $logger);
130-
$this->assertEquals('typesense', $logger->getName());
97+
new Configuration($configWithInvalidLogger);
13198
}
13299

133100
public function testConfigurationThrowsErrorWhenNodesAreMissing(): void

0 commit comments

Comments
 (0)