Skip to content

Conversation

@jkobus
Copy link

@jkobus jkobus commented Jan 28, 2026

Change Summary

The Typesense PHP client now supports injecting your own PSR-3 compatible logger instance instead of using the default Monolog logger.

Usage

With Custom Logger

You can pass any PSR-3 compatible logger instance via the logger configuration option:

use Monolog\Logger;
use Monolog\Handler\StreamHandler;
use Typesense\Client;

// Create your custom logger
$customLogger = new Logger('my-app');
$customLogger->pushHandler(new StreamHandler('/var/log/typesense.log', Logger::DEBUG));

// Pass it to the client
$client = new Client([
    'api_key' => 'xyz',
    'nodes' => [
        [
            'host' => 'localhost',
            'port' => '8108',
            'protocol' => 'http',
        ],
    ],
    'logger' => $customLogger,  // Your custom logger
]);

Compatible Loggers

Any PSR-3 compliant logger will work:

  • Monolog
  • Symfony Logger
  • Laravel Logger
  • Any other PSR-3 compliant implementation

Default Behavior

If you don't provide a custom logger, the client will create a default Monolog logger that writes to stdout:

$client = new Client([
    'api_key' => 'xyz',
    'nodes' => [
        [
            'host' => 'localhost',
            'port' => '8108',
            'protocol' => 'http',
        ],
    ],
    'log_level' => Logger::INFO,  // Optional: customize the default logger level
]);

Implementation Details

The change was made in src/Lib/Configuration.php:

// Allow custom logger injection
if (isset($config['logger']) && $config['logger'] instanceof LoggerInterface) {
    $this->logger = $config['logger'];
} else {
    $this->logLevel = $config['log_level'] ?? Logger::WARNING;
    $this->logger   = new Logger('typesense');
    $this->logger->pushHandler(new StreamHandler('php://stdout', $this->logLevel));
}

This ensures backward compatibility - existing code will continue to work without any changes.

Example

See examples/custom_logger.php for a complete working example.

PR Checklist

Copilot AI review requested due to automatic review settings January 28, 2026 18:31
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR implements custom logger injection support for the Typesense PHP client, allowing users to provide their own PSR-3 compatible logger instead of relying on the default Monolog logger. This enhancement improves flexibility for users who want to integrate Typesense logging with their existing logging infrastructure.

Changes:

  • Modified Configuration class to accept and validate custom PSR-3 logger instances via the logger configuration option
  • Added comprehensive test coverage for logger injection scenarios including custom loggers, log levels, and edge cases
  • Provided a working example demonstrating both custom logger usage and default logger configuration

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
src/Lib/Configuration.php Implements custom logger injection with PSR-3 interface validation, falling back to default Monolog logger when no custom logger is provided
tests/Feature/ConfigurationTest.php Comprehensive test suite covering default logger, custom logger, custom log levels, mock loggers, invalid logger handling, and configuration validation
examples/custom_logger.php Example demonstrating custom logger injection and default logger configuration patterns

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

$this->assertNotNull($config->getNearestNode());
}
}

Copy link

Copilot AI Jan 28, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is an extra blank line at the end of the file. PSR-12 requires exactly one blank line at the end of files. Remove the extra blank line on line 195.

Suggested change

Copilot uses AI. Check for mistakes.
Comment on lines +1 to +3
<?php

include '../vendor/autoload.php';
Copy link

Copilot AI Jan 28, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For consistency with other example files (alias_operations.php, cluster_operations.php, collection_operations.php, info_operations.php), consider adding the PHPStorm suppression annotation at the top of the file after the opening PHP tag: /** @noinspection ForgottenDebugOutputInspection */. This suppresses IDE warnings about debug output functions like print_r and echo in example files.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant