-
Notifications
You must be signed in to change notification settings - Fork 49
feat(logger): Implement custom logger injection for Typesense client #113
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
Open
jkobus
wants to merge
4
commits into
typesense:master
Choose a base branch
from
jkobus:logger-dependency-injection
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
05adecd
feat(logger): Implement custom logger injection for Typesense client
jkobus 23d674b
feat(logger): Implement custom logger injection for Typesense client
jkobus 73007ed
feat(logger): Implement custom logger injection for Typesense client
jkobus 510c07d
Update src/Lib/Configuration.php
jkobus File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| <?php | ||
|
|
||
| include '../vendor/autoload.php'; | ||
|
|
||
| use Monolog\Logger; | ||
| use Monolog\Handler\StreamHandler; | ||
| use Symfony\Component\HttpClient\HttplugClient; | ||
| use Typesense\Client; | ||
|
|
||
| /** | ||
| * Example: Using a custom logger with Typesense | ||
| * | ||
| * This example demonstrates how to inject your own logger instance | ||
| * instead of using the default Monolog logger. | ||
| */ | ||
|
|
||
| try { | ||
| echo '<pre>'; | ||
|
|
||
| // Create your custom logger instance | ||
| $customLogger = new Logger('my-custom-logger'); | ||
| $customLogger->pushHandler(new StreamHandler('/tmp/typesense-custom.log', Logger::DEBUG)); | ||
|
|
||
| // You can also use any other PSR-3 compatible logger | ||
| // For example: Symfony's Logger, Laravel's Logger, etc. | ||
|
|
||
| echo "--------Example 1: Client with Custom Logger-------\n"; | ||
| // Initialize Typesense client with custom logger | ||
| $client = new Client( | ||
| [ | ||
| 'api_key' => 'xyz', | ||
| 'nodes' => [ | ||
| [ | ||
| 'host' => 'localhost', | ||
| 'port' => '8108', | ||
| 'protocol' => 'http', | ||
| ], | ||
| ], | ||
| 'client' => new HttplugClient(), | ||
| 'logger' => $customLogger, // Inject your custom logger here | ||
| ] | ||
| ); | ||
|
|
||
| // Use the client - all logs will now use your custom logger | ||
| $health = $client->health->retrieve(); | ||
| print_r($health); | ||
| echo "✓ Using custom logger - logs written to /tmp/typesense-custom.log\n"; | ||
|
|
||
| echo "\n--------Example 2: Client with Default Logger-------\n"; | ||
| // Example without custom logger (uses default): | ||
| $clientWithDefaultLogger = new Client( | ||
| [ | ||
| 'api_key' => 'xyz', | ||
| 'nodes' => [ | ||
| [ | ||
| 'host' => 'localhost', | ||
| 'port' => '8108', | ||
| 'protocol' => 'http', | ||
| ], | ||
| ], | ||
| 'client' => new HttplugClient(), | ||
| 'log_level' => Logger::INFO, // You can customize the log level | ||
| ] | ||
| ); | ||
|
|
||
| $health2 = $clientWithDefaultLogger->health->retrieve(); | ||
| print_r($health2); | ||
| echo "✓ Using default logger - logs written to stdout\n"; | ||
|
|
||
| } catch (Exception $e) { | ||
| echo $e->getMessage(); | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,161 @@ | ||
| <?php | ||
|
|
||
| namespace Feature; | ||
|
|
||
| use Monolog\Handler\StreamHandler; | ||
| use Monolog\Logger; | ||
| use PHPUnit\Framework\TestCase; | ||
| use Typesense\Exceptions\ConfigError; | ||
| use Typesense\Lib\Configuration; | ||
|
|
||
| class ConfigurationTest extends TestCase | ||
| { | ||
| private array $baseConfig; | ||
|
|
||
| protected function setUp(): void | ||
| { | ||
| $this->baseConfig = [ | ||
| 'api_key' => 'test_api_key', | ||
| 'nodes' => [ | ||
| [ | ||
| 'host' => 'localhost', | ||
| 'port' => '8108', | ||
| 'protocol' => 'http', | ||
| ], | ||
| ], | ||
| ]; | ||
| } | ||
|
|
||
| public function testConfigurationWithDefaultLogger(): void | ||
| { | ||
| $config = new Configuration($this->baseConfig); | ||
|
|
||
| $logger = $config->getLogger(); | ||
|
|
||
| $this->assertInstanceOf(Logger::class, $logger); | ||
| } | ||
|
|
||
| public function testConfigurationWithCustomLogger(): void | ||
| { | ||
| // Create a custom logger | ||
| $customLogger = new Logger('custom-test-logger'); | ||
| $customLogger->pushHandler(new StreamHandler('php://stdout', Logger::DEBUG)); | ||
|
|
||
| // Add custom logger to config | ||
| $configWithCustomLogger = array_merge($this->baseConfig, [ | ||
| 'logger' => $customLogger, | ||
| ]); | ||
|
|
||
| $config = new Configuration($configWithCustomLogger); | ||
|
|
||
| $logger = $config->getLogger(); | ||
|
|
||
| // Assert that the logger is the same instance we passed | ||
| $this->assertSame($customLogger, $logger); | ||
| } | ||
|
|
||
| public function testConfigurationWithCustomLogLevel(): void | ||
| { | ||
| // Add custom log level to config | ||
| $configWithLogLevel = array_merge($this->baseConfig, [ | ||
| 'log_level' => Logger::DEBUG, | ||
| ]); | ||
|
|
||
| $config = new Configuration($configWithLogLevel); | ||
|
|
||
| $logger = $config->getLogger(); | ||
| $this->assertInstanceOf(Logger::class, $logger); | ||
| } | ||
|
|
||
| public function testConfigurationWithCustomLoggerThrowsExceptionWhenLogLevelIsAlsoProvided(): void | ||
| { | ||
| $this->expectException(\InvalidArgumentException::class); | ||
| $this->expectExceptionMessage('Setting log_level is not allowed when a custom logger is provided.'); | ||
|
|
||
| $customLogger = new Logger('custom-logger-with-level'); | ||
| $customLogger->pushHandler(new StreamHandler('php://stdout', Logger::ERROR)); | ||
|
|
||
| $configWithBoth = array_merge($this->baseConfig, [ | ||
| 'logger' => $customLogger, | ||
| 'log_level' => Logger::DEBUG, | ||
| ]); | ||
|
|
||
| new Configuration($configWithBoth); | ||
| } | ||
|
|
||
| public function testConfigurationWithInvalidLoggerThrowsException(): void | ||
| { | ||
| $this->expectException(\InvalidArgumentException::class); | ||
| $this->expectExceptionMessage('Logger must implement Psr\Log\LoggerInterface'); | ||
|
|
||
| // Try to pass a non-logger object (should throw exception) | ||
| $configWithInvalidLogger = array_merge($this->baseConfig, [ | ||
| 'logger' => 'not-a-logger-instance', | ||
| ]); | ||
|
|
||
| new Configuration($configWithInvalidLogger); | ||
| } | ||
|
|
||
| public function testConfigurationThrowsErrorWhenNodesAreMissing(): void | ||
| { | ||
| $this->expectException(ConfigError::class); | ||
| $this->expectExceptionMessage('`nodes` is not defined.'); | ||
|
|
||
| new Configuration([ | ||
| 'api_key' => 'test_api_key', | ||
| ]); | ||
| } | ||
|
|
||
| public function testConfigurationThrowsErrorWhenApiKeyIsMissing(): void | ||
| { | ||
| $this->expectException(ConfigError::class); | ||
| $this->expectExceptionMessage('`api_key` is not defined.'); | ||
|
|
||
| new Configuration([ | ||
| 'nodes' => [ | ||
| [ | ||
| 'host' => 'localhost', | ||
| 'port' => '8108', | ||
| 'protocol' => 'http', | ||
| ], | ||
| ], | ||
| ]); | ||
| } | ||
|
|
||
| public function testConfigurationWithAllOptions(): void | ||
| { | ||
| $customLogger = new Logger('full-config-logger'); | ||
| $customLogger->pushHandler(new StreamHandler('php://stdout', Logger::INFO)); | ||
|
|
||
| $fullConfig = [ | ||
| 'api_key' => 'test_api_key', | ||
| 'nodes' => [ | ||
| [ | ||
| 'host' => 'localhost', | ||
| 'port' => '8108', | ||
| 'protocol' => 'http', | ||
| 'path' => '/api', | ||
| ], | ||
| ], | ||
| 'nearest_node' => [ | ||
| 'host' => 'nearest.example.com', | ||
| 'port' => '443', | ||
| 'protocol' => 'https', | ||
| ], | ||
| 'logger' => $customLogger, | ||
| 'num_retries' => 5, | ||
| 'retry_interval_seconds' => 2.0, | ||
| 'healthcheck_interval_seconds' => 30, | ||
| 'randomize_nodes' => false, | ||
| ]; | ||
|
|
||
| $config = new Configuration($fullConfig); | ||
|
|
||
| $this->assertSame($customLogger, $config->getLogger()); | ||
| $this->assertEquals(5, $config->getNumRetries()); | ||
| $this->assertEquals(2.0, $config->getRetryIntervalSeconds()); | ||
| $this->assertEquals(30, $config->getHealthCheckIntervalSeconds()); | ||
| $this->assertNotNull($config->getNearestNode()); | ||
| } | ||
| } | ||
|
|
||
jkobus marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.