|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @author Matthijs van den Bos <[email protected]> |
|
4
|
|
|
* @copyright 2013 Matthijs van den Bos |
|
5
|
|
|
*/ |
|
6
|
|
|
|
|
7
|
|
|
namespace VDB\Spider; |
|
8
|
|
|
|
|
9
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
|
10
|
|
|
use Symfony\Component\EventDispatcher\GenericEvent; |
|
11
|
|
|
use VDB\Uri\UriInterface; |
|
12
|
|
|
|
|
13
|
|
|
use VDB\Spider\Event\SpiderEvents; |
|
14
|
|
|
|
|
15
|
|
|
class LogHandler implements EventSubscriberInterface |
|
16
|
|
|
{ |
|
17
|
|
|
private $debug = false; |
|
18
|
|
|
|
|
19
|
|
|
public function __construct($debug = false) |
|
20
|
|
|
{ |
|
21
|
|
|
$this->debug = $debug; |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
public static function getSubscribedEvents() |
|
25
|
|
|
{ |
|
26
|
|
|
return array( |
|
27
|
|
|
SpiderEvents::SPIDER_CRAWL_FILTER_POSTFETCH => 'logFiltered', |
|
28
|
|
|
SpiderEvents::SPIDER_CRAWL_FILTER_PREFETCH => 'logFiltered', |
|
29
|
|
|
SpiderEvents::SPIDER_CRAWL_POST_ENQUEUE => 'logQueued', |
|
30
|
|
|
SpiderEvents::SPIDER_CRAWL_RESOURCE_PERSISTED => 'logPersisted', |
|
31
|
|
|
SpiderEvents::SPIDER_CRAWL_ERROR_REQUEST => 'logFailed' |
|
32
|
|
|
); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
protected function logEvent($name, GenericEvent $event) |
|
36
|
|
|
{ |
|
37
|
|
|
if ($this->debug === true) { |
|
38
|
|
|
echo "\n[$name]\t:" . $event->getArgument('uri')->toString(); |
|
39
|
|
|
} |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function logQueued(GenericEvent $event) |
|
43
|
|
|
{ |
|
44
|
|
|
$this->logEvent('queued', $event); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public function logPersisted(GenericEvent $event) |
|
48
|
|
|
{ |
|
49
|
|
|
$this->logEvent('persisted', $event); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function logFiltered(GenericEvent $event) |
|
53
|
|
|
{ |
|
54
|
|
|
$this->logEvent('filtered', $event); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function logFailed(GenericEvent $event) |
|
58
|
|
|
{ |
|
59
|
|
|
$this->logEvent('failed', $event); |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|