Example: Configuring the Logger

edit

Example: Configuring the Logger

edit

By default, logging in the client is disabled for performance reasons. If you wish to enable logging, simply set the logging parameter to true:

$params = array();
$params['logging'] = true;
$client = new Elasticsearch\Client($params);


This will enable logging to a file called elasticsearch.log inside your project directory. The default logging level is WARN. This is a logging level that only describes important events that should probably require action from you.

Often, you’ll want to change the logging location. To change this location, simply do the following:

$params = array();
$params['logging'] = true;
$params['logPath'] = '/var/logs/elasticsearch/elasticsearch.log';
$client = new Elasticsearch\Client($params);


Not all parameters are strings. For example, we can change the logging level of the client:

$params = array();
$params['logging']  = true;
$params['logPath']  = '/var/logs/elasticsearch/elasticsearch.log';
$params['logLevel'] = Psr\Log\LogLevel::INFO;
$client = new Elasticsearch\Client($params);


Easy, right? Let’s get a little more complicated and specify a custom logger. By default, the client uses a file-based logger provided by the Monolog framework. Monolog provides a variety of loggers.

Let’s log to the SysLog instead of a file:

use Monolog\Logger;

// Build a Monolog logger that uses the SyslogHandler
$logger    = new Logger('log');
$handler   = new SyslogHandler('my_user');
$processor = new IntrospectionProcessor();

$logger->pushHandler($handler);
$logger->pushProcessor($processor);

// Over-ride the client's logger object with your own
$params['logging']   = true;
$params['logObject'] = $logger;
$client = new Elasticsearch\Client($params);


The client uses the generic PSR\Log interface, which means that any PSR\Log compatible loggers will work just fine in the client. Replacing the logger with another PSR\Log compatible logger is similar to the previous example of configuring a Monolog logger:

use Monolog\Logger;

$logger = new MySpecialPSRLogger();


$params['logging'] = true;
$params['logObject'] = $logger;
$client = new Elasticsearch\Client($params);