|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace Firesphere\SolrSearch\Helpers; |
|
5
|
|
|
|
|
6
|
|
|
use Firesphere\SolrSearch\Models\SolrLog; |
|
7
|
|
|
use Firesphere\SolrSearch\Services\SolrCoreService; |
|
8
|
|
|
use GuzzleHttp\Client; |
|
9
|
|
|
use GuzzleHttp\Exception\GuzzleException; |
|
10
|
|
|
use SilverStripe\Dev\Debug; |
|
11
|
|
|
use SilverStripe\ORM\ValidationException; |
|
12
|
|
|
|
|
13
|
|
|
class SolrLogger |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var Client |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $client; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* SolrLogger constructor. |
|
22
|
|
|
* @param null $handler |
|
23
|
|
|
*/ |
|
24
|
30 |
|
public function __construct($handler = null) |
|
25
|
|
|
{ |
|
26
|
30 |
|
$config = SolrCoreService::config()->get('config'); |
|
27
|
30 |
|
$endpoint = $config['endpoint']; |
|
28
|
30 |
|
$hostConfig = array_values($endpoint); |
|
29
|
|
|
$guzzleConfig = [ |
|
30
|
30 |
|
'base_uri' => $hostConfig[0]['host'] . ':' . $hostConfig[0]['port'] |
|
31
|
|
|
]; |
|
32
|
30 |
|
if ($handler) { |
|
33
|
1 |
|
$guzzleConfig['handler'] = $handler; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
30 |
|
$this->client = new Client($guzzleConfig); |
|
37
|
30 |
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param string $type |
|
41
|
|
|
* @param string $message |
|
42
|
|
|
* @param string $index |
|
43
|
|
|
* @throws GuzzleException |
|
44
|
|
|
* @throws ValidationException |
|
45
|
|
|
*/ |
|
46
|
1 |
|
public static function logMessage($type, $message, $index) |
|
47
|
|
|
{ |
|
48
|
1 |
|
$solrLogger = new self(); |
|
49
|
1 |
|
$solrLogger->saveSolrLog($type); |
|
50
|
|
|
/** @var SolrLog $lastError */ |
|
51
|
1 |
|
$lastError = SolrLog::get() |
|
52
|
1 |
|
->filter([ |
|
53
|
1 |
|
'Index' => 'x:' . $index, |
|
54
|
1 |
|
'Level' => $type |
|
55
|
|
|
]) |
|
56
|
1 |
|
->sort('Created ASC') |
|
57
|
1 |
|
->first(); |
|
58
|
|
|
|
|
59
|
1 |
|
$err = ($lastError === null) ? 'Unknown' : $lastError->getLastErrorLine(); |
|
60
|
1 |
|
Debug::dump($message . PHP_EOL . $err); |
|
61
|
1 |
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @param string $type |
|
65
|
|
|
* @throws GuzzleException |
|
66
|
|
|
* @throws ValidationException |
|
67
|
|
|
*/ |
|
68
|
28 |
|
public function saveSolrLog($type = 'Query'): void |
|
69
|
|
|
{ |
|
70
|
28 |
|
$response = $this->client->request('GET', 'solr/admin/info/logging', [ |
|
71
|
28 |
|
'query' => [ |
|
72
|
|
|
'since' => 0, |
|
73
|
|
|
'wt' => 'json' |
|
74
|
|
|
] |
|
75
|
|
|
]); |
|
76
|
|
|
|
|
77
|
28 |
|
$arrayResponse = json_decode($response->getBody(), true); |
|
78
|
|
|
|
|
79
|
28 |
|
foreach ($arrayResponse['history']['docs'] as $error) { |
|
80
|
|
|
$filter = [ |
|
81
|
28 |
|
'Timestamp' => $error['time'], |
|
82
|
28 |
|
'Index' => $error['core'], |
|
83
|
28 |
|
'Level' => $error['level'], |
|
84
|
|
|
]; |
|
85
|
28 |
|
if (!SolrLog::get()->filter($filter)->exists()) { |
|
86
|
|
|
$logData = [ |
|
87
|
28 |
|
'Message' => $error['message'], |
|
88
|
28 |
|
'Type' => $type, |
|
89
|
|
|
]; |
|
90
|
28 |
|
$log = array_merge($filter, $logData); |
|
91
|
28 |
|
SolrLog::create($log)->write(); |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
28 |
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @return Client |
|
98
|
|
|
*/ |
|
99
|
2 |
|
public function getClient(): Client |
|
100
|
|
|
{ |
|
101
|
2 |
|
return $this->client; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* @param Client $client |
|
106
|
|
|
* @return SolrLogger |
|
107
|
|
|
*/ |
|
108
|
1 |
|
public function setClient(Client $client): self |
|
109
|
|
|
{ |
|
110
|
1 |
|
$this->client = $client; |
|
111
|
|
|
|
|
112
|
1 |
|
return $this; |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|