Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Monolog 2 #53

Merged
merged 4 commits into from
Feb 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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