8
8
use Elastica \Exception \RuntimeException ;
9
9
use Elastica \Script \AbstractScript ;
10
10
use Psr \Log \LoggerInterface ;
11
+ use Psr \Log \NullLogger ;
11
12
12
13
/**
13
14
* Client to connect the the elasticsearch server.
@@ -46,7 +47,7 @@ class Client
46
47
/**
47
48
* @var callback
48
49
*/
49
- protected $ _callback = null ;
50
+ protected $ _callback ;
50
51
51
52
/**
52
53
* @var \Elastica\Request
@@ -61,22 +62,26 @@ class Client
61
62
/**
62
63
* @var LoggerInterface
63
64
*/
64
- protected $ _logger = null ;
65
+ protected $ _logger ;
66
+
65
67
/**
66
68
* @var Connection\ConnectionPool
67
69
*/
68
- protected $ _connectionPool = null ;
70
+ protected $ _connectionPool ;
69
71
70
72
/**
71
73
* Creates a new Elastica client.
72
74
*
73
- * @param array $config OPTIONAL Additional config options
75
+ * @param array $config OPTIONAL Additional config options
74
76
* @param callback $callback OPTIONAL Callback function which can be used to be notified about errors (for example connection down)
77
+ * @param LoggerInterface $logger
75
78
*/
76
- public function __construct (array $ config = array (), $ callback = null )
79
+ public function __construct (array $ config = array (), $ callback = null , LoggerInterface $ logger = null )
77
80
{
78
- $ this ->setConfig ($ config );
79
81
$ this ->_callback = $ callback ;
82
+ $ this ->_logger = $ logger ?: new NullLogger ();
83
+
84
+ $ this ->setConfig ($ config );
80
85
$ this ->_initConnections ();
81
86
}
82
87
@@ -614,32 +619,40 @@ public function bulk(array $params)
614
619
*
615
620
* @throws Exception\ConnectionException|\Exception
616
621
*
617
- * @return \Elastica\ Response Response object
622
+ * @return Response Response object
618
623
*/
619
624
public function request ($ path , $ method = Request::GET , $ data = array (), array $ query = array ())
620
625
{
621
626
$ 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 ;
631
629
632
- return $ response ;
630
+ try {
631
+ $ response = $ this ->_lastResponse = $ request ->send ();
633
632
} catch (ConnectionException $ e ) {
634
633
$ this ->_connectionPool ->onFail ($ connection , $ e , $ this );
635
634
635
+ $ this ->_logger ->error ('Elastica Request Failure ' , [
636
+ 'exception ' => $ e ,
637
+ 'request ' => $ request ->toArray (),
638
+ 'retry ' => $ this ->hasConnection ()
639
+ ]);
640
+
636
641
// In case there is no valid connection left, throw exception which caused the disabling of the connection.
637
642
if (!$ this ->hasConnection ()) {
638
643
throw $ e ;
639
644
}
640
645
641
646
return $ this ->request ($ path , $ method , $ data , $ query );
642
647
}
648
+
649
+ $ this ->_logger ->debug ('Elastica Request ' , [
650
+ 'request ' => $ request ->toArray (),
651
+ 'response ' => $ response ->getData (),
652
+ 'responseStatus ' => $ response ->getStatus ()
653
+ ]);
654
+
655
+ return $ response ;
643
656
}
644
657
645
658
/**
@@ -669,57 +682,28 @@ public function refreshAll()
669
682
}
670
683
671
684
/**
672
- * logging .
685
+ * Replace the existing logger .
673
686
*
674
- * @param string|\Elastica\Request $context
675
- *
676
- * @throws Exception\RuntimeException
687
+ * @param LoggerInterface $logger
677
688
*/
678
- protected function _log ( $ context )
689
+ public function setLogger ( LoggerInterface $ logger )
679
690
{
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 ;
694
692
}
695
693
696
694
/**
697
- * @return \Elastica\ Request
695
+ * @return Request
698
696
*/
699
697
public function getLastRequest ()
700
698
{
701
699
return $ this ->_lastRequest ;
702
700
}
703
701
704
702
/**
705
- * @return \Elastica\ Response
703
+ * @return Response
706
704
*/
707
705
public function getLastResponse ()
708
706
{
709
707
return $ this ->_lastResponse ;
710
708
}
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
- }
725
709
}
0 commit comments