1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of PHP DNS Server. |
4
|
|
|
* |
5
|
|
|
* (c) Yif Swery <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace yswery\DNS; |
12
|
|
|
|
13
|
|
|
use Psr\Log\AbstractLogger; |
14
|
|
|
use Psr\Log\LogLevel; |
15
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
16
|
|
|
use yswery\DNS\Event\Events; |
17
|
|
|
use yswery\DNS\Event\ServerExceptionEvent; |
18
|
|
|
use yswery\DNS\Event\QueryReceiveEvent; |
19
|
|
|
use yswery\DNS\Event\QueryResponseEvent; |
20
|
|
|
use yswery\DNS\Event\ServerStartEvent; |
21
|
|
|
|
22
|
|
|
class EchoLogger extends AbstractLogger implements EventSubscriberInterface |
23
|
|
|
{ |
24
|
|
|
public static function getSubscribedEvents(): array |
25
|
|
|
{ |
26
|
|
|
return [ |
27
|
|
|
Events::SERVER_START => 'onServerStart', |
28
|
|
|
Events::SERVER_EXCEPTION => 'onException', |
29
|
|
|
Events::QUERY_RECEIVE => 'onQueryReceive', |
30
|
|
|
Events::QUERY_RESPONSE => 'onQueryResponse', |
31
|
|
|
]; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function onServerStart(ServerStartEvent $event): void |
|
|
|
|
35
|
|
|
{ |
36
|
|
|
$this->log(LogLevel::INFO, 'Server started.'); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function onException(ServerExceptionEvent $event): void |
40
|
|
|
{ |
41
|
|
|
$this->log(LogLevel::ERROR, $event->getException()->getMessage()); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function onQueryReceive(QueryReceiveEvent $event): void |
45
|
|
|
{ |
46
|
|
|
foreach ($event->getMessage()->getQuestions() as $question) { |
47
|
|
|
$this->log(LogLevel::INFO, 'Query: '.$question); |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function onQueryResponse(QueryResponseEvent $event): void |
52
|
|
|
{ |
53
|
|
|
foreach ($event->getMessage()->getAnswers() as $answer) { |
54
|
|
|
$this->log(LogLevel::INFO, 'Answer: '.$answer); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function log($level, $message, array $context = []) |
59
|
|
|
{ |
60
|
|
|
echo sprintf('[%s] %s: %s'.PHP_EOL, date('c'), $level, $message); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.