Skip to content

Commit 0bfde39

Browse files
committed
Simplify Client logger
1 parent 09c0d99 commit 0bfde39

File tree

2 files changed

+36
-134
lines changed

2 files changed

+36
-134
lines changed

lib/Elastica/Client.php

+36-52
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Elastica\Exception\RuntimeException;
99
use Elastica\Script\AbstractScript;
1010
use Psr\Log\LoggerInterface;
11+
use Psr\Log\NullLogger;
1112

1213
/**
1314
* Client to connect the the elasticsearch server.
@@ -46,7 +47,7 @@ class Client
4647
/**
4748
* @var callback
4849
*/
49-
protected $_callback = null;
50+
protected $_callback;
5051

5152
/**
5253
* @var \Elastica\Request
@@ -61,22 +62,26 @@ class Client
6162
/**
6263
* @var LoggerInterface
6364
*/
64-
protected $_logger = null;
65+
protected $_logger;
66+
6567
/**
6668
* @var Connection\ConnectionPool
6769
*/
68-
protected $_connectionPool = null;
70+
protected $_connectionPool;
6971

7072
/**
7173
* Creates a new Elastica client.
7274
*
73-
* @param array $config OPTIONAL Additional config options
75+
* @param array $config OPTIONAL Additional config options
7476
* @param callback $callback OPTIONAL Callback function which can be used to be notified about errors (for example connection down)
77+
* @param LoggerInterface $logger
7578
*/
76-
public function __construct(array $config = array(), $callback = null)
79+
public function __construct(array $config = array(), $callback = null, LoggerInterface $logger = null)
7780
{
78-
$this->setConfig($config);
7981
$this->_callback = $callback;
82+
$this->_logger = $logger ?: new NullLogger();
83+
84+
$this->setConfig($config);
8085
$this->_initConnections();
8186
}
8287

@@ -614,32 +619,40 @@ public function bulk(array $params)
614619
*
615620
* @throws Exception\ConnectionException|\Exception
616621
*
617-
* @return \Elastica\Response Response object
622+
* @return Response Response object
618623
*/
619624
public function request($path, $method = Request::GET, $data = array(), array $query = array())
620625
{
621626
$connection = $this->getConnection();
622-
try {
623-
$request = new Request($path, $method, $data, $query, $connection);
624-
625-
$this->_log($request);
626-
627-
$response = $request->send();
628-
629-
$this->_lastRequest = $request;
630-
$this->_lastResponse = $response;
627+
$request = $this->_lastRequest = new Request($path, $method, $data, $query, $connection);
628+
$response = null;
631629

632-
return $response;
630+
try {
631+
$response = $this->_lastResponse = $request->send();
633632
} catch (ConnectionException $e) {
634633
$this->_connectionPool->onFail($connection, $e, $this);
635634

635+
$this->_logger->error('Elastica Request Failure', [
636+
'exception' => $e,
637+
'request' => $request->toArray(),
638+
'retry' => $this->hasConnection()
639+
]);
640+
636641
// In case there is no valid connection left, throw exception which caused the disabling of the connection.
637642
if (!$this->hasConnection()) {
638643
throw $e;
639644
}
640645

641646
return $this->request($path, $method, $data, $query);
642647
}
648+
649+
$this->_logger->debug('Elastica Request', [
650+
'request' => $request->toArray(),
651+
'response' => $response->getData(),
652+
'responseStatus' => $response->getStatus()
653+
]);
654+
655+
return $response;
643656
}
644657

645658
/**
@@ -669,57 +682,28 @@ public function refreshAll()
669682
}
670683

671684
/**
672-
* logging.
685+
* Replace the existing logger.
673686
*
674-
* @param string|\Elastica\Request $context
675-
*
676-
* @throws Exception\RuntimeException
687+
* @param LoggerInterface $logger
677688
*/
678-
protected function _log($context)
689+
public function setLogger(LoggerInterface $logger)
679690
{
680-
$log = $this->getConfig('log');
681-
if ($log && !class_exists('Psr\Log\AbstractLogger')) {
682-
throw new RuntimeException('Class Psr\Log\AbstractLogger not found');
683-
} elseif (!$this->_logger && $log) {
684-
$this->setLogger(new Log($this->getConfig('log')));
685-
}
686-
if ($this->_logger) {
687-
if ($context instanceof Request) {
688-
$data = $context->toArray();
689-
} else {
690-
$data = array('message' => $context);
691-
}
692-
$this->_logger->debug('logging Request', $data);
693-
}
691+
$this->_logger = $logger;
694692
}
695693

696694
/**
697-
* @return \Elastica\Request
695+
* @return Request
698696
*/
699697
public function getLastRequest()
700698
{
701699
return $this->_lastRequest;
702700
}
703701

704702
/**
705-
* @return \Elastica\Response
703+
* @return Response
706704
*/
707705
public function getLastResponse()
708706
{
709707
return $this->_lastResponse;
710708
}
711-
712-
/**
713-
* set Logger.
714-
*
715-
* @param LoggerInterface $logger
716-
*
717-
* @return $this
718-
*/
719-
public function setLogger(LoggerInterface $logger)
720-
{
721-
$this->_logger = $logger;
722-
723-
return $this;
724-
}
725709
}

lib/Elastica/Log.php

-82
This file was deleted.

0 commit comments

Comments
 (0)