1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace Firesphere\SolrSearch\Helpers; |
5
|
|
|
|
6
|
|
|
use Countable; |
7
|
|
|
use Firesphere\SolrSearch\Models\SolrLog; |
8
|
|
|
use Firesphere\SolrSearch\Services\SolrCoreService; |
9
|
|
|
use GuzzleHttp\Client; |
10
|
|
|
use GuzzleHttp\Exception\GuzzleException; |
11
|
|
|
use SilverStripe\Dev\Debug; |
12
|
|
|
use SilverStripe\ORM\DB; |
13
|
|
|
use SilverStripe\ORM\ValidationException; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class SolrLogger |
17
|
|
|
* |
18
|
|
|
* Log information from Solr to the CMS for reference |
19
|
|
|
* |
20
|
|
|
* @todo implement {@link LoggerInterface} |
21
|
|
|
* @package Firesphere\SolrSearch\Helpers |
22
|
|
|
*/ |
23
|
|
|
class SolrLogger |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* Guzzle base client to communicate with Solr |
27
|
|
|
* |
28
|
|
|
* @var Client |
29
|
|
|
*/ |
30
|
|
|
protected $client; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* SolrLogger constructor. |
34
|
|
|
* |
35
|
|
|
* @param null|Countable $handler |
36
|
|
|
*/ |
37
|
38 |
|
public function __construct($handler = null) |
38
|
|
|
{ |
39
|
38 |
|
$config = SolrCoreService::config()->get('config'); |
40
|
38 |
|
$hostConfig = array_shift($config['endpoint']); |
41
|
|
|
$guzzleConfig = [ |
42
|
38 |
|
'base_uri' => $hostConfig['host'] . ':' . $hostConfig['port'], |
43
|
|
|
]; |
44
|
38 |
|
if ($handler) { |
45
|
1 |
|
$guzzleConfig['handler'] = $handler; |
46
|
|
|
} |
47
|
|
|
|
48
|
38 |
|
$this->client = new Client($guzzleConfig); |
49
|
38 |
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Log the given message and dump it out. |
53
|
|
|
* Also boot the Log to get the latest errors from Solr |
54
|
|
|
* |
55
|
|
|
* @param string $type |
56
|
|
|
* @param string $message |
57
|
|
|
* @param string $index |
58
|
|
|
* @throws GuzzleException |
59
|
|
|
* @throws ValidationException |
60
|
|
|
*/ |
61
|
1 |
|
public static function logMessage($type, $message, $index): void |
62
|
|
|
{ |
63
|
1 |
|
$solrLogger = new self(); |
64
|
1 |
|
$solrLogger->saveSolrLog($type); |
65
|
|
|
/** @var SolrLog $lastError */ |
66
|
1 |
|
$lastError = SolrLog::get() |
67
|
1 |
|
->filter([ |
68
|
1 |
|
'Index' => 'x:' . $index, |
69
|
1 |
|
'Level' => $type, |
70
|
|
|
]) |
71
|
1 |
|
->sort('Timestamp DESC') |
72
|
1 |
|
->first(); |
73
|
|
|
|
74
|
1 |
|
$err = ($lastError === null) ? 'Unknown' : $lastError->getLastErrorLine(); |
75
|
1 |
|
$message .= 'Last known error:' . PHP_EOL . $err; |
76
|
1 |
|
Debug::dump($message); |
77
|
1 |
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Save the latest Solr errors to the log |
81
|
|
|
* |
82
|
|
|
* @param string $type |
83
|
|
|
* @throws GuzzleException |
84
|
|
|
* @throws ValidationException |
85
|
|
|
*/ |
86
|
36 |
|
public function saveSolrLog($type = 'Query'): void |
87
|
|
|
{ |
88
|
36 |
|
$response = $this->client->request('GET', 'solr/admin/info/logging', [ |
89
|
36 |
|
'query' => [ |
90
|
|
|
'since' => 0, |
91
|
|
|
'wt' => 'json', |
92
|
|
|
], |
93
|
|
|
]); |
94
|
|
|
|
95
|
36 |
|
$arrayResponse = json_decode($response->getBody(), true); |
96
|
|
|
|
97
|
36 |
|
foreach ($arrayResponse['history']['docs'] as $error) { |
98
|
|
|
$filter = [ |
99
|
36 |
|
'Timestamp' => $error['time'], |
100
|
36 |
|
'Index' => $error['core'] ?? 'x:Unknown', |
101
|
36 |
|
'Level' => $error['level'], |
102
|
|
|
]; |
103
|
36 |
|
$this->findOrCreateLog($type, $filter, $error); |
104
|
|
|
} |
105
|
36 |
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Return the Guzzle Client |
109
|
|
|
* |
110
|
|
|
* @return Client |
111
|
|
|
*/ |
112
|
2 |
|
public function getClient(): Client |
113
|
|
|
{ |
114
|
2 |
|
return $this->client; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Set the Guzzle client |
119
|
|
|
* |
120
|
|
|
* @param Client $client |
121
|
|
|
* @return SolrLogger |
122
|
|
|
*/ |
123
|
1 |
|
public function setClient(Client $client): self |
124
|
|
|
{ |
125
|
1 |
|
$this->client = $client; |
126
|
|
|
|
127
|
1 |
|
return $this; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @param $type |
132
|
|
|
* @param array $filter |
133
|
|
|
* @param $error |
134
|
|
|
*/ |
135
|
36 |
|
private function findOrCreateLog($type, array $filter, $error): void |
136
|
|
|
{ |
137
|
36 |
|
if (!SolrLog::get()->filter($filter)->exists()) { |
138
|
|
|
$logData = [ |
139
|
36 |
|
'Message' => $error['message'], |
140
|
36 |
|
'Type' => $type, |
141
|
|
|
]; |
142
|
36 |
|
$log = array_merge($filter, $logData); |
143
|
36 |
|
$conn = DB::get_conn(); |
144
|
36 |
|
if ($conn) { |
|
|
|
|
145
|
36 |
|
SolrLog::create($log)->write(); |
146
|
|
|
} |
147
|
|
|
} |
148
|
36 |
|
} |
149
|
|
|
} |
150
|
|
|
|