Skip to content

Commit

Permalink
Merge 27fef45 into f093c49
Browse files Browse the repository at this point in the history
  • Loading branch information
rvanlaak committed Feb 11, 2020
2 parents f093c49 + 27fef45 commit 73cfc2c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 16 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"league/uri": "^5.0",
"mf2/mf2": "^0.4",
"ml/json-ld": "^1.1",
"monolog/monolog": "^1.24",
"monolog/monolog": "^1.24 || ^2",
"psr/cache": "^1.0",
"psr/log": "^1.1",
"symfony/cache": "^4.0|^5.0"
Expand Down
23 changes: 18 additions & 5 deletions src/Micrometa/Infrastructure/Logger/ExceptionLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,29 +39,36 @@
use Jkphl\Micrometa\Ports\Exceptions\RuntimeException;
use Monolog\Handler\NullHandler;
use Monolog\Logger;
use Monolog\ResettableInterface;
use Psr\Log\LoggerInterface;
use Psr\Log\LoggerTrait;

/**
* Exception logger
*
* @package Jkphl\Micrometa
* @subpackage Jkphl\Micrometa\Infrastructure
*/
class ExceptionLogger extends Logger
final class ExceptionLogger implements LoggerInterface, ResettableInterface
{
use LoggerTrait;

/**
* Exception threshold
*
* @var int
*/
protected $threshold;

private $decoratedLogger;

/**
* Constructor
*/
public function __construct($threshold = Logger::ERROR)
{
$this->threshold = $threshold;
parent::__construct('exception', [new NullHandler()]);
$this->decoratedLogger = new Logger('exception', [new NullHandler()]);
}

/**
Expand All @@ -71,17 +78,18 @@ public function __construct($threshold = Logger::ERROR)
* @param string $message The log message
* @param array $context The log context
*
* @return Boolean Whether the record has been processed
* @throws \Exception Exception that occured
* @throws \RuntimeException Log message as exception
*/
public function addRecord($level, $message, array $context = [])
public function log($level, $message, array $context = [])
{
$level = Logger::toMonologLevel($level);

if ($this->isTriggered($level)) {
throw $this->getContextException($context) ?: new RuntimeException($message, $level);
}

return parent::addRecord($level, $message, $context);
$this->decoratedLogger->addRecord($level, $message, $context);
}

/**
Expand All @@ -108,4 +116,9 @@ protected function getContextException(array $context)
return (isset($context['exception']) && ($context['exception'] instanceof \Exception)) ?
$context['exception'] : null;
}

public function reset()
{
$this->decoratedLogger->reset();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,8 @@
* @package Jkphl\Micrometa
* @subpackage Jkphl\Micrometa\Tests
*/
class LoggerTest extends AbstractTestBase
class ExceptionLoggerTest extends AbstractTestBase
{
/**
* Test the exception logger
*/
public function testExceptionLogger()
{
$logger = new ExceptionLogger();
$this->assertTrue($logger->debug('DEBUG'));
}

/**
* Test the exception logger
*
Expand Down

0 comments on commit 73cfc2c

Please sign in to comment.