Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/yswery/PHP-DNS-SERVER
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelwilliams committed Mar 7, 2020
2 parents 99093c9 + f440b42 commit 4dbd72e
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 41 deletions.
47 changes: 6 additions & 41 deletions src/Event/Subscriber/EchoLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [])
Expand Down
63 changes: 63 additions & 0 deletions src/Event/Subscriber/LoggerSubscriber.php
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);
}
}
}

0 comments on commit 4dbd72e

Please sign in to comment.