Commit 4ded03c3 authored by Averor's avatar Averor

LoggingMiddleware: log level added

parent d18f0393
Pipeline #29023720 passed with stage
in 7 minutes and 2 seconds
......@@ -7,6 +7,7 @@ use Averor\MessageBus\QueryBus\Middleware\QueryDispatchingMiddleware;
use Averor\MessageBus\QueryBus\QueryBus;
use Averor\MessageBus\Resolver\CallableMapResolver;
use Averor\SimpleLogger\Logger;
use Psr\Log\LogLevel;
require __DIR__ . '/../vendor/autoload.php';
......@@ -38,7 +39,8 @@ $map = [
$bus = new QueryBus([
new LoggingMiddleware(
new Logger('php://output')
new Logger('php://output'),
LogLevel::INFO
),
new QueryDispatchingMiddleware(
new CallableMapResolver($map)
......
......@@ -5,6 +5,7 @@ namespace Averor\MessageBus\QueryBus\Middleware;
use Averor\MessageBus\QueryBus\Contract\Query;
use Averor\MessageBus\QueryBus\Contract\QueryBusMiddleware;
use Psr\Log\LoggerInterface;
use Psr\Log\LogLevel;
/**
* Class LoggingMiddleware
......@@ -17,24 +18,34 @@ class LoggingMiddleware implements QueryBusMiddleware
/** @var LoggerInterface */
protected $logger;
public function __construct(LoggerInterface $logger)
/** @var string */
protected $level;
public function __construct(LoggerInterface $logger, ?string $level = LogLevel::INFO)
{
$this->logger = $logger;
$this->level = $level;
}
public function execute(Query $query, callable $next)
{
$this->logger->debug(sprintf(
"Dispatching query %s",
get_class($query)
));
$this->logger->log(
$this->level,
sprintf(
"Dispatching query %s",
get_class($query)
)
);
$result = $next($query);
$this->logger->debug(sprintf(
"Query %s dispatched",
get_class($query)
));
$this->logger->log(
$this->level,
sprintf(
"Query %s dispatched",
get_class($query)
)
);
return $result;
}
......
......@@ -7,6 +7,7 @@ use Averor\MessageBus\QueryBus\QueryBus;
use Averor\MessageBus\QueryBus\Tests\Fixtures\SampleQuery;
use Averor\SimpleLogger\Logger;
use PHPUnit\Framework\TestCase;
use Psr\Log\LogLevel;
/**
* Class LoggingMiddlewareTest
......@@ -20,7 +21,8 @@ class LoggingMiddlewareTest extends TestCase
{
$queryBus = new QueryBus([
new LoggingMiddleware(
new Logger('php://output')
new Logger('php://output'),
LogLevel::NOTICE
)
]);
......@@ -34,8 +36,8 @@ class LoggingMiddlewareTest extends TestCase
$this->assertEquals(
implode(PHP_EOL, [
sprintf("debug Dispatching query %s", get_class($message)),
sprintf("debug Query %s dispatched", get_class($message))
sprintf("notice Dispatching query %s", get_class($message)),
sprintf("notice Query %s dispatched", get_class($message))
]),
$result
);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment