diff --git a/src/Event/Subscriber/EchoLogger.php b/src/Event/Subscriber/EchoLogger.php index 6daba62..00dc4dd 100644 --- a/src/Event/Subscriber/EchoLogger.php +++ b/src/Event/Subscriber/EchoLogger.php @@ -11,51 +11,16 @@ namespace yswery\DNS\Event\Subscriber; -use Psr\Log\AbstractLogger; -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; +use Psr\Log\LoggerInterface; +use Psr\Log\LoggerTrait; -class EchoLogger extends AbstractLogger implements EventSubscriberInterface +class EchoLogger extends LoggerSubscriber implements LoggerInterface { - 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->log(LogLevel::INFO, 'Server started.'); - $this->log(LogLevel::INFO, sprintf('Listening on %s', $event->getSocket()->getLocalAddress())); - } - - public function onException(ServerExceptionEvent $event): void - { - $this->log(LogLevel::ERROR, $event->getException()->getMessage()); - } - - public function onQueryReceive(QueryReceiveEvent $event): void - { - foreach ($event->getMessage()->getQuestions() as $question) { - $this->log(LogLevel::INFO, 'Query: '.$question); - } - } + use LoggerTrait; - public function onQueryResponse(QueryResponseEvent $event): void + public function __construct() { - foreach ($event->getMessage()->getAnswers() as $answer) { - $this->log(LogLevel::INFO, 'Answer: '.$answer); - } + $this->setLogger($this); } public function log($level, $message, array $context = []) diff --git a/src/Event/Subscriber/LoggerSubscriber.php b/src/Event/Subscriber/LoggerSubscriber.php new file mode 100644 index 0000000..8f2f7ec --- /dev/null +++ b/src/Event/Subscriber/LoggerSubscriber.php @@ -0,0 +1,63 @@ + + * + * 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); + } + } +}