diff --git a/CHANGELOG.md b/CHANGELOG.md index 2d4775872a..dbf08977f7 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ All notable changes to this project will be documented in this file based on the * Elastica\Reindex->run() does not refresh the new Index after completion anymore. Use `$reindex->setParam(Reindex::REFRESH, 'wait_for')` instead. * `Elastica\Search->search()` and `Elastica\Search->count()` use request method `POST` by default. Same for `Elastica\Index`, `Elastica\Type\AbstractType`, `Elastica\Type`. * Elastica\Client `$_config` field is now a `ClientConfiguration` instead of an array +* Removed `\Elastica\Client::_log`, `\Elastica\Log` and the `log` configuration option. Use the `Psr\Log\LoggerInterface $logger` client argument to customize logging. ### Bugfixes * Always set the Guzzle `base_uri` to support connecting to multiple ES hosts. [#1618](https://github.com/ruflin/Elastica/pull/1618) diff --git a/lib/Elastica/Client.php b/lib/Elastica/Client.php index f055ebe503..f47376baf5 100644 --- a/lib/Elastica/Client.php +++ b/lib/Elastica/Client.php @@ -76,10 +76,6 @@ public function __construct($config = [], callable $callback = null, LoggerInter $this->_config = $configuration; $this->_callback = $callback; - - if (!$logger && $configuration->get('log')) { - $logger = new Log($configuration->get('log')); - } $this->_logger = $logger ?: new NullLogger(); $this->_initConnections(); @@ -635,7 +631,11 @@ public function request($path, $method = Request::GET, $data = [], array $query $response = $this->_lastResponse = $request->send(); } catch (ConnectionException $e) { $this->_connectionPool->onFail($connection, $e, $this); - $this->_log($e); + $this->_logger->error('Elastica Request Failure', [ + 'exception' => $e, + 'request' => $e->getRequest()->toArray(), + 'retry' => $this->hasConnection(), + ]); // In case there is no valid connection left, throw exception which caused the disabling of the connection. if (!$this->hasConnection()) { @@ -645,7 +645,11 @@ public function request($path, $method = Request::GET, $data = [], array $query return $this->request($path, $method, $data, $query); } - $this->_log($request); + $this->_logger->debug('Elastica Request', [ + 'request' => $request, + 'response' => $this->_lastResponse ? $this->_lastResponse->getData() : null, + 'responseStatus' => $this->_lastResponse ? $this->_lastResponse->getStatus() : null, + ]); return $response; } @@ -667,40 +671,6 @@ public function requestEndpoint(AbstractEndpoint $endpoint) ); } - /** - * logging. - * - * @deprecated Overwriting Client->_log is deprecated. Handle logging functionality by using a custom LoggerInterface. - * - * @param mixed $context - */ - protected function _log($context) - { - if ($context instanceof ConnectionException) { - $this->_logger->error('Elastica Request Failure', [ - 'exception' => $context, - 'request' => $context->getRequest()->toArray(), - 'retry' => $this->hasConnection(), - ]); - - return; - } - - if ($context instanceof Request) { - $this->_logger->debug('Elastica Request', [ - 'request' => $context->toArray(), - 'response' => $this->_lastResponse ? $this->_lastResponse->getData() : null, - 'responseStatus' => $this->_lastResponse ? $this->_lastResponse->getStatus() : null, - ]); - - return; - } - - $this->_logger->debug('Elastica Request', [ - 'message' => $context, - ]); - } - /** * Force merges all search indices. * diff --git a/lib/Elastica/ClientConfiguration.php b/lib/Elastica/ClientConfiguration.php index a2abb25013..b8650d1243 100644 --- a/lib/Elastica/ClientConfiguration.php +++ b/lib/Elastica/ClientConfiguration.php @@ -14,7 +14,6 @@ class ClientConfiguration /** * Config with defaults. * - * log: Set to true, to enable logging, set a string to log to a specific file * retryOnConflict: Use in \Elastica\Client::updateDocument * bigintConversion: Set to true to enable the JSON bigint to string conversion option (see issue #717) * @@ -31,7 +30,6 @@ class ClientConfiguration 'timeout' => null, 'connections' => [], // host, port, path, timeout, transport, compression, persistent, timeout, username, password, config -> (curl, headers, url) 'roundRobin' => false, - 'log' => false, 'retryOnConflict' => 0, 'bigintConversion' => false, 'username' => null, diff --git a/lib/Elastica/Log.php b/lib/Elastica/Log.php deleted file mode 100644 index d73d0a137e..0000000000 --- a/lib/Elastica/Log.php +++ /dev/null @@ -1,82 +0,0 @@ - - */ -class Log extends AbstractLogger -{ - /** - * Log path or true if enabled. - * - * @var string|bool - */ - protected $_log; - - /** - * Last logged message. - * - * @var string Last logged message - */ - protected $_lastMessage; - - /** - * Inits log object. - * - * @param string|bool String to set a specific file for logging - */ - public function __construct($log = '') - { - $this->setLog($log); - } - - /** - * Log a message. - * - * @param mixed $level - * @param string $message - * @param array $context - * - * @return void|null - */ - public function log($level, $message, array $context = []) - { - $context['error_message'] = $message; - $this->_lastMessage = JSON::stringify($context); - - if (!empty($this->_log) && \is_string($this->_log)) { - \error_log($this->_lastMessage.PHP_EOL, 3, $this->_log); - } else { - \error_log($this->_lastMessage); - } - } - - /** - * Enable/disable log or set log path. - * - * @param bool|string $log Enables log or sets log path - * - * @return $this - */ - public function setLog($log) - { - $this->_log = $log; - - return $this; - } - - /** - * Return last logged message. - * - * @return string Last logged message - */ - public function getLastMessage() - { - return $this->_lastMessage; - } -} diff --git a/test/Elastica/ClientConfigurationTest.php b/test/Elastica/ClientConfigurationTest.php index f1b17af356..4fc9754f3b 100644 --- a/test/Elastica/ClientConfigurationTest.php +++ b/test/Elastica/ClientConfigurationTest.php @@ -24,7 +24,7 @@ public function testFromSimpleDsn() $expected = [ 'host' => '192.168.1.1', - 'port' => '9201', + 'port' => 9201, 'path' => null, 'url' => null, 'proxy' => null, @@ -33,7 +33,6 @@ public function testFromSimpleDsn() 'timeout' => null, 'connections' => [], 'roundRobin' => false, - 'log' => false, 'retryOnConflict' => 0, 'bigintConversion' => false, 'username' => null, @@ -45,7 +44,7 @@ public function testFromSimpleDsn() public function testFromDsnWithParameters() { - $configuration = ClientConfiguration::fromDsn('https://user:p4ss@foo.com:9201/my-path?proxy=https://proxy.com&persistent=false&timeout=45&roundRobin=true&log=true&retryOnConflict=2&bigintConversion=true&extra=abc'); + $configuration = ClientConfiguration::fromDsn('https://user:p4ss@foo.com:9201/my-path?proxy=https://proxy.com&persistent=false&timeout=45&roundRobin=true&retryOnConflict=2&bigintConversion=true&extra=abc'); $expected = [ 'host' => 'foo.com', 'port' => '9201', @@ -57,7 +56,6 @@ public function testFromDsnWithParameters() 'timeout' => 45, 'connections' => [], 'roundRobin' => true, - 'log' => true, 'retryOnConflict' => 2, 'bigintConversion' => true, 'username' => 'user', @@ -83,7 +81,6 @@ public function testFromEmptyArray() 'timeout' => null, 'connections' => [], // host, port, path, timeout, transport, compression, persistent, timeout, username, password, config -> (curl, headers, url) 'roundRobin' => false, - 'log' => false, 'retryOnConflict' => 0, 'bigintConversion' => false, 'username' => null, @@ -111,7 +108,6 @@ public function testFromArray() 'timeout' => null, 'connections' => [], // host, port, path, timeout, transport, compression, persistent, timeout, username, password, config -> (curl, headers, url) 'roundRobin' => false, - 'log' => false, 'retryOnConflict' => 0, 'bigintConversion' => false, 'username' => 'Jdoe', @@ -145,7 +141,6 @@ public function testGet() 'timeout' => null, 'connections' => [], 'roundRobin' => false, - 'log' => false, 'retryOnConflict' => 0, 'bigintConversion' => false, 'username' => null, diff --git a/test/Elastica/ClientTest.php b/test/Elastica/ClientTest.php index 42c58d6e1d..1ecd5447d0 100644 --- a/test/Elastica/ClientTest.php +++ b/test/Elastica/ClientTest.php @@ -41,7 +41,7 @@ public function testConstructWithDsn() $expected = [ 'host' => 'foo.com', - 'port' => '9200', + 'port' => 9200, 'path' => null, 'url' => null, 'proxy' => null, @@ -50,7 +50,6 @@ public function testConstructWithDsn() 'timeout' => null, 'connections' => [], 'roundRobin' => false, - 'log' => false, 'retryOnConflict' => 2, 'bigintConversion' => false, 'username' => 'user', diff --git a/test/Elastica/LogTest.php b/test/Elastica/LogTest.php deleted file mode 100644 index bc854f695b..0000000000 --- a/test/Elastica/LogTest.php +++ /dev/null @@ -1,197 +0,0 @@ -assertInstanceOf('Psr\Log\LoggerInterface', $log); - } - - /** - * @group unit - */ - public function testSetLogConfigPath() - { - $logPath = '/tmp/php.log'; - $client = $this->_getClient(['log' => $logPath]); - $this->assertEquals($logPath, $client->getConfig('log')); - } - - /** - * @group unit - */ - public function testSetLogConfigEnable() - { - $client = $this->_getClient(['log' => true]); - $this->assertTrue($client->getConfig('log')); - } - - /** - * @group unit - */ - public function testSetLogConfigEnable1() - { - $client = $this->_getClient(); - $client->setLogger(new Log()); - $this->assertFalse($client->getConfig('log')); - } - - /** - * @group unit - */ - public function testEmptyLogConfig() - { - $client = $this->_getClient(); - $this->assertEmpty($client->getConfig('log')); - } - - /** - * @group unit - */ - public function testGetLastMessage() - { - $log = new Log('/tmp/php.log'); - - $log->log(LogLevel::DEBUG, $this->_message, $this->_context); - - $this->_context['error_message'] = $this->_message; - $message = \json_encode($this->_context); - - $this->assertEquals($message, $log->getLastMessage()); - } - - /** - * @group unit - */ - public function testGetLastMessage2() - { - $client = $this->_getClient(['log' => true]); - $log = new Log($client); - - // Set log path temp path as otherwise test fails with output - $errorLog = \ini_get('error_log'); - \ini_set('error_log', \sys_get_temp_dir().DIRECTORY_SEPARATOR.'php.log'); - - $this->_context['error_message'] = $this->_message; - $message = \json_encode($this->_context); - - $log->log(LogLevel::DEBUG, $this->_message, $this->_context); - \ini_set('error_log', $errorLog); - - $this->assertEquals($message, $log->getLastMessage()); - } - - /** - * @group unit - */ - public function testGetLastMessageInfo() - { - $log = $this->initLog(); - $log->info($this->_message, $this->_context); - $this->assertEquals($this->getMessage(), $log->getLastMessage()); - } - - /** - * @group unit - */ - public function testGetLastMessageCritical() - { - $log = $this->initLog(); - $log->critical($this->_message, $this->_context); - $this->assertEquals($this->getMessage(), $log->getLastMessage()); - } - - /** - * @group unit - */ - public function testGetLastMessageAlert() - { - $log = $this->initLog(); - $log->alert($this->_message, $this->_context); - $this->assertEquals($this->getMessage(), $log->getLastMessage()); - } - - /** - * @group unit - */ - public function testGetLastMessageDebug() - { - $log = $this->initLog(); - $log->debug($this->_message, $this->_context); - $this->assertEquals($this->getMessage(), $log->getLastMessage()); - } - - /** - * @group unit - */ - public function testGetLastMessageEmergency() - { - $log = $this->initLog(); - $log->emergency($this->_message, $this->_context); - $this->assertEquals($this->getMessage(), $log->getLastMessage()); - } - - /** - * @group unit - */ - public function testGetLastMessageError() - { - $log = $this->initLog(); - $log->error($this->_message, $this->_context); - $this->assertEquals($this->getMessage(), $log->getLastMessage()); - } - - /** - * @group unit - */ - public function testGetLastMessageNotice() - { - $log = $this->initLog(); - $log->notice($this->_message, $this->_context); - $this->assertEquals($this->getMessage(), $log->getLastMessage()); - } - - /** - * @group unit - */ - public function testGetLastMessageWarning() - { - $log = $this->initLog(); - $log->warning($this->_message, $this->_context); - $this->assertEquals($this->getMessage(), $log->getLastMessage()); - } - - private function initLog() - { - $log = new Log('/tmp/php.log'); - - return $log; - } - - private function getMessage() - { - $this->_context['error_message'] = $this->_message; - - return \json_encode($this->_context); - } -}