-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #100 from samuelwilliams/LoggerSubscriber
Add logger subscriber.
- Loading branch information
Showing
2 changed files
with
69 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of PHP DNS Server. | ||
* | ||
* (c) Yif Swery <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace yswery\DNS\Event\Subscriber; | ||
|
||
use Psr\Log\LoggerAwareInterface; | ||
use Psr\Log\LoggerAwareTrait; | ||
use Psr\Log\LogLevel; | ||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
use yswery\DNS\Event\Events; | ||
use yswery\DNS\Event\QueryReceiveEvent; | ||
use yswery\DNS\Event\QueryResponseEvent; | ||
use yswery\DNS\Event\ServerExceptionEvent; | ||
use yswery\DNS\Event\ServerStartEvent; | ||
|
||
class LoggerSubscriber implements EventSubscriberInterface, LoggerAwareInterface | ||
{ | ||
use LoggerAwareTrait; | ||
|
||
public static function getSubscribedEvents(): array | ||
{ | ||
return [ | ||
Events::SERVER_START => 'onServerStart', | ||
Events::SERVER_START_FAIL => 'onException', | ||
Events::SERVER_EXCEPTION => 'onException', | ||
Events::QUERY_RECEIVE => 'onQueryReceive', | ||
Events::QUERY_RESPONSE => 'onQueryResponse', | ||
]; | ||
} | ||
|
||
public function onServerStart(ServerStartEvent $event): void | ||
{ | ||
$this->logger->log(LogLevel::INFO, 'Server started.'); | ||
$this->logger->log(LogLevel::INFO, sprintf('Listening on %s', $event->getSocket()->getLocalAddress())); | ||
} | ||
|
||
public function onException(ServerExceptionEvent $event): void | ||
{ | ||
$this->logger->log(LogLevel::ERROR, $event->getException()->getMessage()); | ||
} | ||
|
||
public function onQueryReceive(QueryReceiveEvent $event): void | ||
{ | ||
foreach ($event->getMessage()->getQuestions() as $question) { | ||
$this->logger->log(LogLevel::INFO, 'Query: '.$question); | ||
} | ||
} | ||
|
||
public function onQueryResponse(QueryResponseEvent $event): void | ||
{ | ||
foreach ($event->getMessage()->getAnswers() as $answer) { | ||
$this->logger->log(LogLevel::INFO, 'Answer: '.$answer); | ||
} | ||
} | ||
} |