-
Notifications
You must be signed in to change notification settings - Fork 1
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 #4 from joaojacome/master
Added a middleware processor, remove unnecessary code
- Loading branch information
Showing
20 changed files
with
270 additions
and
283 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
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
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,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]; | ||
} | ||
} |
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,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]; | ||
} | ||
} |
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,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; | ||
} |
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,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]; | ||
} | ||
} |
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,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]; | ||
} | ||
} |
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,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]; | ||
} | ||
} |
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 |
---|---|---|
|
@@ -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'; | ||
|
||
|
@@ -28,6 +30,11 @@ class OneLog | |
*/ | ||
private $loggers; | ||
|
||
/** | ||
* @var MiddlewareProcessor | ||
*/ | ||
private $middlewareProcessor; | ||
|
||
/** | ||
* OneLog constructor. | ||
* | ||
|
@@ -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 | ||
|
@@ -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); | ||
} | ||
} |
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
Oops, something went wrong.