-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
CrawlLogger.php
69 lines (63 loc) · 2.08 KB
/
CrawlLogger.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?php
namespace Spekulatius\SpatieCrawlerToolkit\Observers;
use Psr\Http\Message\UriInterface;
use Psr\Http\Message\ResponseInterface;
use Illuminate\Support\Facades\Log;
use GuzzleHttp\Exception\RequestException;
use Spatie\Crawler\CrawlObservers\CrawlObserver as SpatieCrawlObserver;
class CrawlLogger extends SpatieCrawlObserver
{
/**
* @param \Psr\Http\Message\UriInterface $url
*/
public function willCrawl(UriInterface $url): void
{
if (in_array(__FUNCTION__, config('crawler-toolkit.log'))) {
Log::debug('Crawler: ' . $url . ' will be crawled.');
}
}
/**
* Called when the crawler has crawled the given url successfully.
*
* @param \Psr\Http\Message\UriInterface $url
* @param \Psr\Http\Message\ResponseInterface $response
* @param \Psr\Http\Message\UriInterface|null $foundOnUrl
*/
public function crawled(
UriInterface $url,
ResponseInterface $response,
?UriInterface $foundOnUrl = null
): void {
if (in_array(__FUNCTION__, config('crawler-toolkit.log'))) {
Log::debug(
'Crawler: ' . $url . ' crawled' .
(($foundOnUrl === null) ? '.' : ', found on ' . $foundOnUrl)
);
}
}
/**
* Called when the crawler had a problem crawling the given url.
*
* @param \Psr\Http\Message\UriInterface $url
* @param \GuzzleHttp\Exception\RequestException $requestException
* @param \Psr\Http\Message\UriInterface|null $foundOnUrl
*/
public function crawlFailed(
UriInterface $url,
RequestException $requestException,
?UriInterface $foundOnUrl = null
): void {
if (in_array(__FUNCTION__, config('crawler-toolkit.log'))) {
Log::debug('Crawler: ' . $url . ' failed: ' . $requestException->getMessage());
}
}
/**
* Called when the crawl has ended.
*/
public function finishedCrawling(): void
{
if (in_array(__FUNCTION__, config('crawler-toolkit.log'))) {
Log::debug('Crawler: Finished');
}
}
}