Skip to content

Commit

Permalink
Merge pull request #4 from joaojacome/master
Browse files Browse the repository at this point in the history
Added a middleware processor, remove unnecessary code
  • Loading branch information
rendler-denis authored Sep 21, 2018
2 parents 3269695 + 0574664 commit d892ada
Show file tree
Hide file tree
Showing 20 changed files with 270 additions and 283 deletions.
4 changes: 2 additions & 2 deletions ContextualInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ interface ContextualInterface
/**
* Set the context for the logger
*
* @param array $context
* @param mixed $context
*
* @return mixed
*/
public function setContext(array $context);
public function setContext($context);

/**
* Retrieve the context of this class
Expand Down
6 changes: 6 additions & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ public function getConfigTreeBuilder()
->defaultFalse()
->treatNullLike(false)
->end()
->arrayNode('middlewares')
->info('List of middlewares to use')
->prototype('scalar')
->end()
->treatNullLike([])
->end()
->booleanNode('enable_request_id')
->info('Add a request identifier to all log entries. Allows for easier tracking of logs during a request')
->defaultTrue()
Expand Down
1 change: 1 addition & 0 deletions DependencyInjection/OnelogExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public function load(array $configs, ContainerBuilder $container)

$container->setParameter('onelog.logger_service', $config['logger_service']);
$container->setParameter('onelog.register_global', $config['register_global']);
$container->setParameter('onelog.middlewares', $config['middlewares']);
$container->setParameter('onelog.register_monolog_channels', $config['register_monolog_channels']);
$container->setParameter('onelog.enable_request_id', $config['enable_request_id']);
}
Expand Down
10 changes: 9 additions & 1 deletion Helper/ContextualTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace KoderHut\OnelogBundle\Helper;

use KoderHut\OnelogBundle\ContextualInterface;

/**
* Trait ContextualTrait
*
Expand All @@ -17,8 +19,14 @@ trait ContextualTrait
/**
* @inheritdoc
*/
public function setContext(array $context): self
public function setContext($context): self
{
if (is_object($context) && $context instanceof ContextualInterface) {
$context = $context->getContext();
} else if (!is_array($context)) {
throw new \InvalidArgumentException();
}

$this->contextualData = $context;

return $this;
Expand Down
29 changes: 29 additions & 0 deletions Middleware/ContextProcessor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php declare(strict_types=1);

namespace KoderHut\OnelogBundle\Middleware;

use KoderHut\OnelogBundle\ContextualInterface;

/**
* Class ContextProcessor
*
* @author Joao Jacome <[email protected]>
*/
class ContextProcessor implements MiddlewareInterface
{
/**
* @param string $level
* @param mixed $message
* @param array $context
*
* @return array
*/
public function process($level, $message, $context): array
{
if ($message instanceof ContextualInterface) {
$context = array_merge($context, $message->getContext());
}

return [$message, $context];
}
}
31 changes: 31 additions & 0 deletions Middleware/ExceptionCodeProcessor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php declare(strict_types=1);

namespace KoderHut\OnelogBundle\Middleware;

use KoderHut\OnelogBundle\ContextualInterface;

/**
* Class ExceptionCodeProcessor
*
* @author Joao Jacome <[email protected]>
*/
class ExceptionCodeProcessor implements MiddlewareInterface
{
/**
* @param string $level
* @param mixed $message
* @param array $context
*
* @return array
*/
public function process($level, $message, $context): array
{
if ($message instanceof \Throwable) {
$context = array_merge($context, [
'code' => $message->getCode(),
]);
}

return [$message, $context];
}
}
20 changes: 20 additions & 0 deletions Middleware/MiddlewareInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php declare(strict_types=1);

namespace KoderHut\OnelogBundle\Middleware;

/**
* Interface MiddlewareInterface
*
* @author Joao Jacome <[email protected]>
*/
interface MiddlewareInterface
{
/**
* @param string $level
* @param mixed $message
* @param array $context
*
* @return array
*/
public function process($level, $message, $context): array;
}
29 changes: 29 additions & 0 deletions Middleware/SimpleMessageProcessor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php declare(strict_types=1);

namespace KoderHut\OnelogBundle\Middleware;

use KoderHut\OnelogBundle\ContextualInterface;

/**
* Class SimpleMessageProcessor
*
* @author Joao Jacome <[email protected]>
*/
class SimpleMessageProcessor implements MiddlewareInterface
{
/**
* @param string $level
* @param mixed $message
* @param array $context
*
* @return array
*/
public function process($level, $message, $context): array
{
if ($message instanceof \Throwable) {
$message = $message->getMessage();
}

return [$message, $context];
}
}
32 changes: 32 additions & 0 deletions Middleware/StackTraceProcessor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php declare(strict_types=1);

namespace KoderHut\OnelogBundle\Middleware;

use KoderHut\OnelogBundle\ContextualInterface;
use KoderHut\OnelogBundle\MiddlewareProcessor;

/**
* Class StackTraceProcessor
*
* @author Joao Jacome <[email protected]>
*/
class StackTraceProcessor implements MiddlewareInterface
{
/**
* @param string $level
* @param mixed $message
* @param array $context
*
* @return array
*/
public function process($level, $message, $context): array
{
if ($message instanceof \Throwable) {
$context = array_merge($context, [
'stack_trace' => $message->getTraceAsString(),
]);
}

return [$message, $context];
}
}
42 changes: 42 additions & 0 deletions MiddlewareProcessor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php declare(strict_types=1);

namespace KoderHut\OnelogBundle;

use KoderHut\OnelogBundle\Middleware\MiddlewareInterface;

/**
* Class MiddlewareProcessor
*
* @author Joao Jacome <[email protected]>
*/
class MiddlewareProcessor implements MiddlewareInterface
{
/**
* @var MiddlewareInterface[]
*/
private $middlewares = [];

/**
* @param MiddlewareInterface $middleware
*/
public function registerMiddleware(MiddlewareInterface $middleware)
{
$this->middlewares[] = $middleware;
}

/**
* @param string $level
* @param mixed $message
* @param array $context
*
* @return array
*/
public function process($level, $message, $context): array
{
foreach ($this->middlewares as $middleware) {
[$message, $context] = $middleware->process($level, $message, $context);
}

return [$message, $context];
}
}
33 changes: 31 additions & 2 deletions OneLog.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@
use KoderHut\OnelogBundle\Helper\NullLogger;
use Monolog\Logger;
use Psr\Log\LoggerInterface;
use Psr\Log\LoggerTrait;

/**
* Class OneLog
*
* @author Denis-Florin Rendler <[email protected]>
* @author Joao Jacome <[email protected]>
*/
class OneLog
class OneLog implements LoggerInterface
{
use PSRLoggerTrait;
use LoggerTrait;

public const DEFAULT_LOGGER = 'default';

Expand All @@ -28,6 +30,11 @@ class OneLog
*/
private $loggers;

/**
* @var MiddlewareProcessor
*/
private $middlewareProcessor;

/**
* OneLog constructor.
*
Expand All @@ -45,6 +52,14 @@ public function __construct(LoggerInterface $default = null, LoggerInterface ...
}
}

/**
* @param MiddlewareProcessor $middlewareProcessor
*/
public function setMiddlewareProcessor(MiddlewareProcessor $middlewareProcessor)
{
$this->middlewareProcessor = $middlewareProcessor;
}

/**
* Retrieves a registered logger based on the logger name as a
* public property of the class
Expand Down Expand Up @@ -88,4 +103,18 @@ public function registerLogger(LoggerInterface $logger, $name = null): void

$this->loggers[$loggerName] = $logger;
}

/**
* @param mixed $level
* @param mixed $message
* @param array $context
*/
public function log($level, $message, array $context = [])
{
if (null !== $this->middlewareProcessor) {
[$message, $context] = $this->middlewareProcessor->process($level, $message, $context);
}

$this->defaultLogger->log($level, $message, $context);
}
}
10 changes: 9 additions & 1 deletion OnelogBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
use KoderHut\OnelogBundle\DependencyInjection\Compiler\RegisterMonologChannels;
use KoderHut\OnelogBundle\DependencyInjection\Compiler\RequestIdentifierPass;
use KoderHut\OnelogBundle\Helper\GlobalNamespaceRegister;
use KoderHut\OnelogBundle\Helper\OneLogStatic;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Bundle\Bundle;
use KoderHut\OnelogBundle\Helper\OneLogStatic;

/**
* Class KoderHut\OnelogBundle
Expand All @@ -24,6 +24,14 @@ public function boot()
OneLogStatic::setInstance($onelogService);
GlobalNamespaceRegister::register('\\OneLog', OneLogStatic::class);
}

if (!empty($middlewares = $this->container->getParameter('onelog.middlewares'))) {
$middlewareProcessor = $this->container->get(MiddlewareProcessor::class);
foreach ($middlewares as $middleware) {
$middlewareProcessor->registerMiddleware($this->container->get($middleware));
}
$this->container->get(OneLog::class)->setMiddlewareProcessor($middlewareProcessor);
}
}

public function build(ContainerBuilder $container)
Expand Down
Loading

0 comments on commit d892ada

Please sign in to comment.