Enabling the Logger

edit

Elasticsearch-PHP supports logging, but it is not enabled by default for performance reasons. If you wish to enable logging, you need to select a logging implementation, install it, then enable the logger in the Client. The recommended logger is Monolog, but any logger that implements the PSR/Log interface will work.

You might have noticed that Monolog was suggested during installation. To begin using Monolog, add it to your composer.json:

{
    "require": {
        ...
        "elasticsearch/elasticsearch" : "~5.0",
        "monolog/monolog": "~1.0"
    }
}

And then update your composer installation:

php composer.phar update

Once Monolog (or another logger) is installed, you need to create a log object and inject it into the client. The ClientBuilder object has a helper static function that will generate a common Monolog-based logger for you. All you need to do is provide the path to your desired logging location:

$logger = ClientBuilder::defaultLogger('path/to/your.log');

$client = ClientBuilder::create()       // Instantiate a new ClientBuilder
            ->setLogger($logger)        // Set the logger with a default logger
            ->build();                  // Build the client object

You can also specify the severity of log messages that you wish to log:

// set severity with second parameter
$logger = ClientBuilder::defaultLogger('/path/to/logs/', Logger::INFO);

$client = ClientBuilder::create()       // Instantiate a new ClientBuilder
            ->setLogger($logger)        // Set the logger with a default logger
            ->build();                  // Build the client object

The defaultLogger() method is just a helper, you are not required to use it. You can create your own logger and inject that instead:

use Monolog\Logger;
use Monolog\Handler\StreamHandler;

$logger = new Logger('name');
$logger->pushHandler(new StreamHandler('path/to/your.log', Logger::WARNING));

$client = ClientBuilder::create()       // Instantiate a new ClientBuilder
            ->setLogger($logger)        // Set your custom logger
            ->build();                  // Build the client object