5
5
use Elastica \Bulk \Action ;
6
6
use Elastica \Exception \ConnectionException ;
7
7
use Elastica \Exception \InvalidException ;
8
- use Elastica \Exception \RuntimeException ;
9
8
use Elastica \Script \AbstractScript ;
10
9
use Psr \Log \LoggerInterface ;
10
+ use Psr \Log \NullLogger ;
11
11
12
12
/**
13
13
* Client to connect the the elasticsearch server.
@@ -46,7 +46,7 @@ class Client
46
46
/**
47
47
* @var callback
48
48
*/
49
- protected $ _callback = null ;
49
+ protected $ _callback ;
50
50
51
51
/**
52
52
* @var \Elastica\Request
@@ -61,22 +61,30 @@ class Client
61
61
/**
62
62
* @var LoggerInterface
63
63
*/
64
- protected $ _logger = null ;
64
+ protected $ _logger ;
65
+
65
66
/**
66
67
* @var Connection\ConnectionPool
67
68
*/
68
- protected $ _connectionPool = null ;
69
+ protected $ _connectionPool ;
69
70
70
71
/**
71
72
* Creates a new Elastica client.
72
73
*
73
- * @param array $config OPTIONAL Additional config options
74
+ * @param array $config OPTIONAL Additional config options
74
75
* @param callback $callback OPTIONAL Callback function which can be used to be notified about errors (for example connection down)
76
+ * @param LoggerInterface $logger
75
77
*/
76
- public function __construct (array $ config = array (), $ callback = null )
78
+ public function __construct (array $ config = array (), $ callback = null , LoggerInterface $ logger = null )
77
79
{
78
- $ this ->setConfig ($ config );
79
80
$ this ->_callback = $ callback ;
81
+
82
+ if (!$ logger && isset ($ config ['log ' ]) && $ config ['log ' ]) {
83
+ $ logger = new Log ($ config ['log ' ]);
84
+ }
85
+ $ this ->_logger = $ logger ?: new NullLogger ();
86
+
87
+ $ this ->setConfig ($ config );
80
88
$ this ->_initConnections ();
81
89
}
82
90
@@ -622,32 +630,66 @@ public function bulk(array $params)
622
630
*
623
631
* @throws Exception\ConnectionException|\Exception
624
632
*
625
- * @return \Elastica\ Response Response object
633
+ * @return Response Response object
626
634
*/
627
635
public function request ($ path , $ method = Request::GET , $ data = array (), array $ query = array ())
628
636
{
629
637
$ connection = $ this ->getConnection ();
630
- try {
631
- $ request = new Request ($ path , $ method , $ data , $ query , $ connection );
632
-
633
- $ this ->_log ($ request );
634
-
635
- $ response = $ request ->send ();
636
-
637
- $ this ->_lastRequest = $ request ;
638
- $ this ->_lastResponse = $ response ;
638
+ $ request = $ this ->_lastRequest = new Request ($ path , $ method , $ data , $ query , $ connection );
639
+ $ this ->_lastResponse = null ;
639
640
640
- return $ response ;
641
+ try {
642
+ $ response = $ this ->_lastResponse = $ request ->send ();
641
643
} catch (ConnectionException $ e ) {
642
644
$ this ->_connectionPool ->onFail ($ connection , $ e , $ this );
643
645
646
+ $ this ->_log ($ e );
647
+
644
648
// In case there is no valid connection left, throw exception which caused the disabling of the connection.
645
649
if (!$ this ->hasConnection ()) {
646
650
throw $ e ;
647
651
}
648
652
649
653
return $ this ->request ($ path , $ method , $ data , $ query );
650
654
}
655
+
656
+ $ this ->_log ($ request , $ response );
657
+
658
+ return $ response ;
659
+ }
660
+
661
+ /**
662
+ * logging.
663
+ *
664
+ * @deprecated Overwriting Client->_log is deprecated. Handle logging functionality by using a custom LoggerInterface.
665
+ *
666
+ * @param mixed $context
667
+ */
668
+ protected function _log ($ context )
669
+ {
670
+ if ($ context instanceof ConnectionException) {
671
+ $ this ->_logger ->error ('Elastica Request Failure ' , [
672
+ 'exception ' => $ context ,
673
+ 'request ' => $ context ->getRequest ()->toArray (),
674
+ 'retry ' => $ this ->hasConnection ()
675
+ ]);
676
+
677
+ return ;
678
+ }
679
+
680
+ if ($ context instanceof Request) {
681
+ $ this ->_logger ->debug ('Elastica Request ' , [
682
+ 'request ' => $ context ->toArray (),
683
+ 'response ' => $ this ->_lastResponse ? $ this ->_lastResponse ->getData () : null ,
684
+ 'responseStatus ' => $ this ->_lastResponse ? $ this ->_lastResponse ->getStatus () : null
685
+ ]);
686
+
687
+ return ;
688
+ }
689
+
690
+ $ this ->_logger ->debug ('Elastica Request ' , [
691
+ 'message ' => $ context
692
+ ]);
651
693
}
652
694
653
695
/**
@@ -677,48 +719,23 @@ public function refreshAll()
677
719
}
678
720
679
721
/**
680
- * logging.
681
- *
682
- * @param string|\Elastica\Request $context
683
- *
684
- * @throws Exception\RuntimeException
685
- */
686
- protected function _log ($ context )
687
- {
688
- $ log = $ this ->getConfig ('log ' );
689
- if ($ log && !class_exists ('Psr\Log\AbstractLogger ' )) {
690
- throw new RuntimeException ('Class Psr\Log\AbstractLogger not found ' );
691
- } elseif (!$ this ->_logger && $ log ) {
692
- $ this ->setLogger (new Log ($ this ->getConfig ('log ' )));
693
- }
694
- if ($ this ->_logger ) {
695
- if ($ context instanceof Request) {
696
- $ data = $ context ->toArray ();
697
- } else {
698
- $ data = array ('message ' => $ context );
699
- }
700
- $ this ->_logger ->debug ('logging Request ' , $ data );
701
- }
702
- }
703
-
704
- /**
705
- * @return \Elastica\Request
722
+ * @return Request
706
723
*/
707
724
public function getLastRequest ()
708
725
{
709
726
return $ this ->_lastRequest ;
710
727
}
711
728
712
729
/**
713
- * @return \Elastica\ Response
730
+ * @return Response
714
731
*/
715
732
public function getLastResponse ()
716
733
{
717
734
return $ this ->_lastResponse ;
718
735
}
719
736
720
737
/**
721
- * set Logger .
738
+ * Replace the existing logger .
722
739
*
723
740
* @param LoggerInterface $logger
724
741
*
0 commit comments