Skip to content

Commit

Permalink
Merge pull request #389 from andrey1s/master
Browse files Browse the repository at this point in the history
add support psr-3 for logging request
  • Loading branch information
ruflin committed May 29, 2013
2 parents 4b38a77 + 425b472 commit c8d5331
Show file tree
Hide file tree
Showing 7 changed files with 176 additions and 27 deletions.
10 changes: 9 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,24 @@ language: php
php:
- 5.3
- 5.4
- 5.5

matrix:
allow_failures:
- php: 5.5

env:
global:
- ES_VER=0.90.0
- ES_MAPPER_ATTACHMENTS_VER=1.6.0
- ES_TRANSPORT_THRIFT_VER=1.4.0
matrix:
- ES_REQUIRE=dev
- ES_REQUIRE=no-dev

before_script:
- composer self-update
- composer --dev --prefer-source install
- composer --${ES_REQUIRE} --prefer-source install
- ./test/bin/install_php_memcache.sh
- ./test/bin/run_elasticsearch.sh

Expand Down
8 changes: 8 additions & 0 deletions changes.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
CHANGES

2013-05-23
- add support PSR-3(https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md)
- Elastica\Log implement LoggerInterface(extends Psr\Log\AbstractLogger)
if you want use logging need install https://github.com/php-fig/log for example (composer require psr/log:dev-master)
if use Elastica\Log inside Elastica\Client nothing more is needed
if use Elastica\Log outside we need use as(https://github.com/php-fig/log) for example Elastica\Log::info($message) or Elastica\Log::log(LogLevel::INFO,$message)
- Elastica\Client add setLogger for setting custom Logger for example Monolog(https://github.com/Seldaek/monolog)

2013-05-18
- Elastica\Index::exists fixed for 0.90.0. HEAD request method introduced
- Elastica\Filter\AbstractMulti::getFilters() added
Expand Down
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@
"php": ">=5.3.3"
},
"require-dev": {
"phpunit/phpunit": "3.7.*",
"psr/log": "~1.0",
"munkie/elasticsearch-thrift-php": "1.4.*"
},
"suggest": {
"munkie/elasticsearch-thrift-php": "Allow using thrift transport"
"munkie/elasticsearch-thrift-php": "Allow using thrift transport",
"psr/log": "for logging",
"monolog/monolog": "Logging request"
},
"autoload": {
"psr-0": {
Expand Down
47 changes: 40 additions & 7 deletions lib/Elastica/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use Elastica\Exception\ClientException;
use Elastica\Exception\ConnectionException;
use Elastica\Exception\InvalidException;
use Elastica\Exception\RuntimeException;
use Psr\Log\LoggerInterface;

/**
* Client to connect the the elasticsearch server
Expand Down Expand Up @@ -60,6 +62,11 @@ class Client
*/
protected $_lastResponse;

/**
* @var LoggerInterface
*/
protected $_logger = null;

/**
* Creates a new Elastica client
*
Expand Down Expand Up @@ -411,14 +418,14 @@ public function addConnection(Connection $connection)
public function getConnection()
{
$enabledConnection = null;

foreach ($this->_connections as $connection) {
if ($connection->isEnabled()) {
$enabledConnection = $connection;
break;
}
}

if (empty($enabledConnection)) {
throw new ClientException('No enabled connection');
}
Expand Down Expand Up @@ -557,13 +564,26 @@ public function optimizeAll($args = array())
}

/**
* @param string|\Elastica\Request $message
* logging
*
* @param string|\Elastica\Request $context
* @throws Exception\RuntimeException
*/
protected function _log($message)
protected function _log($context)
{
if ($this->getConfig('log')) {
$log = new Log($this->getConfig('log'));
$log->log($message);
$log = $this->getConfig('log');
if ($log && !class_exists('Psr\Log\AbstractLogger')) {
throw new RuntimeException('Class Psr\Log\AbstractLogger not found');
} elseif (!$this->_logger && $log) {
$this->setLogger(new Log($this->getConfig('log')));
}
if ($this->_logger) {
if ($context instanceof Request) {
$data = $context->toArray();
} else {
$data = array('message' => $context);
}
$this->_logger->info('logging Request', $data);
}
}

Expand All @@ -582,4 +602,17 @@ public function getLastResponse()
{
return $this->_lastResponse;
}

/**
* set Logger
*
* @param LoggerInterface $logger
* @return $this
*/
public function setLogger(LoggerInterface $logger)
{
$this->_logger = $logger;

return $this;
}
}
24 changes: 13 additions & 11 deletions lib/Elastica/Log.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@

namespace Elastica;

use Psr\Log\AbstractLogger;

/**
* Elastica log object
*
* @category Xodoa
* @package Elastica
* @author Nicolas Ruflin <spam@ruflin.com>
*/
class Log
class Log extends AbstractLogger
{
/**
* Log path or true if enabled
Expand Down Expand Up @@ -38,28 +40,28 @@ public function __construct($log = '')
/**
* Log a message
*
* @param string|\Elastica\Request $message
* @param mixed $level
* @param string $message
* @param array $context
* @return null|void
*/
public function log($message)
public function log($level, $message, array $context = array())
{
if ($message instanceof Request) {
$message = $message->toString();
}

$this->_lastMessage = $message;
$context['error_message'] = $message;
$this->_lastMessage = json_encode($context);

if (!empty($this->_log) && is_string($this->_log)) {
error_log($message . PHP_EOL, 3, $this->_log);
error_log($this->_lastMessage . PHP_EOL, 3, $this->_log);
} else {
error_log($message);
error_log($this->_lastMessage);
}

}

/**
* Enable/disable log or set log path
*
* @param bool|string $log Enables log or sets log path
* @param bool|string $log Enables log or sets log path
* @return \Elastica\Log
*/
public function setLog($log)
Expand Down
2 changes: 1 addition & 1 deletion test/lib/Elastica/Test/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -623,7 +623,7 @@ public function testDeleteDocuments()

public function testLastRequestResponse()
{
$client = new Client(array('log' => '/tmp/php.log'));
$client = new Client();
$response = $client->request('_status');

$this->assertInstanceOf('Elastica\Response', $response);
Expand Down
106 changes: 101 additions & 5 deletions test/lib/Elastica/Test/LogTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,26 @@
use Elastica\Client;
use Elastica\Log;
use Elastica\Test\Base as BaseTest;
use Psr\Log\LogLevel;

class LogTest extends BaseTest
{
private $_context = array();
private $_message = 'hello world';

protected function setUp()
{
if (!class_exists('Psr\Log\AbstractLogger')) {
$this->markTestSkipped('The Psr extension is not available.');
}
}

public function testLogInterface()
{
$log = new Log();
$this->assertInstanceOf('Psr\Log\LoggerInterface', $log);
}

public function testSetLogConfigPath()
{
$logPath = '/tmp/php.log';
Expand All @@ -21,6 +38,13 @@ public function testSetLogConfigEnable()
$this->assertTrue($client->getConfig('log'));
}

public function testSetLogConfigEnable1()
{
$client = new Client();
$client->setLogger(new Log());
$this->assertFalse($client->getConfig('log'));
}

public function testEmptyLogConfig()
{
$client = $this->_getClient();
Expand All @@ -30,9 +54,11 @@ public function testEmptyLogConfig()
public function testGetLastMessage()
{
$log = new Log('/tmp/php.log');
$message = 'hello world';

$log->log($message);
$log->log(LogLevel::DEBUG, $this->_message, $this->_context);

$this->_context['error_message'] = $this->_message;
$message = json_encode($this->_context);

$this->assertEquals($message, $log->getLastMessage());
}
Expand All @@ -46,12 +72,82 @@ public function testGetLastMessage2()
$errorLog = ini_get('error_log');
ini_set('error_log', sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'php.log');

$message = 'hello world';

$log->log($message);
$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());
}

public function testGetLastMessageInfo()
{
$log = $this->initLog();
$log->info($this->_message, $this->_context);
$this->assertEquals($this->getMessage(), $log->getLastMessage());
}

public function testGetLastMessageCritical()
{
$log = $this->initLog();
$log->critical($this->_message, $this->_context);
$this->assertEquals($this->getMessage(), $log->getLastMessage());
}

public function testGetLastMessageAlert()
{
$log = $this->initLog();
$log->alert($this->_message, $this->_context);
$this->assertEquals($this->getMessage(), $log->getLastMessage());
}

public function testGetLastMessageDebug()
{
$log = $this->initLog();
$log->debug($this->_message, $this->_context);
$this->assertEquals($this->getMessage(), $log->getLastMessage());
}

public function testGetLastMessageEmergency()
{
$log = $this->initLog();
$log->emergency($this->_message, $this->_context);
$this->assertEquals($this->getMessage(), $log->getLastMessage());
}

public function testGetLastMessageError()
{
$log = $this->initLog();
$log->error($this->_message, $this->_context);
$this->assertEquals($this->getMessage(), $log->getLastMessage());
}

public function testGetLastMessageNotice()
{
$log = $this->initLog();
$log->notice($this->_message, $this->_context);
$this->assertEquals($this->getMessage(), $log->getLastMessage());
}

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);
}
}

0 comments on commit c8d5331

Please sign in to comment.