Skip to content
This repository has been archived by the owner on Dec 5, 2023. It is now read-only.

Commit

Permalink
feat: Add ability to enable monolog error handler (#7)
Browse files Browse the repository at this point in the history
* feat(added default monolog error handler)

Co-authored-by: Radovan Kepák <[email protected]>
  • Loading branch information
MiMatus and Radovan Kepák authored Apr 14, 2022
1 parent 79a5dc8 commit 3a2a700
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 3 deletions.
41 changes: 41 additions & 0 deletions src/DI/MonologExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Mallgroup\Monolog\Processor\TracyUrlProcessor;
use Mallgroup\Monolog\Tracy\BlueScreenRenderer;
use Mallgroup\Monolog\Tracy\MonologAdapter;
use Monolog\ErrorHandler;
use Nette\Configurator;
use Nette\DI\Compiler;
use Nette\DI\CompilerExtension;
Expand All @@ -38,6 +39,11 @@
*/
class MonologExtension extends CompilerExtension
{
private const
EXCEPTION_HANDLER = 'exception',
ERROR_HANDLER = 'error',
FATAL_HANDLER = 'fatal';

/** @var PriorityDefinition[] */
private array $handlers = [];

Expand Down Expand Up @@ -71,6 +77,13 @@ public function getConfigSchema(): Schema
]),
'registerFallback' => Expect::bool(false)->deprecated(),
'accessPriority' => Expect::string(ILogger::INFO),
'errorHandler' => Expect::structure([
'handlers' => Expect::arrayOf(
Expect::anyOf(self::ERROR_HANDLER, self::EXCEPTION_HANDLER, self::FATAL_HANDLER)
),
'reportedOnly' => Expect::bool(true),
'errorTypes' => Expect::int(-1),
])->required(false),
]);
}

Expand Down Expand Up @@ -198,6 +211,29 @@ public function beforeCompile(): void
/** @var ServiceDefinition $service */
$service->addSetup('setLogger', ['@' . $this->prefix('logger')]);
}

if ($this->config->errorHandler) {
$errorHandlerDefinition = $builder
->addDefinition(($this->prefix('errorHandler')))
->setFactory(ErrorHandler::class, ['logger' => '@' . $this->prefix('logger')])
->setAutowired(false);

if(in_array(self::ERROR_HANDLER, $this->config->errorHandler->handlers, true)){
$errorHandlerDefinition->addSetup(
'registerErrorHandler',
[
'errorTypes' => $this->config->errorHandler->errorTypes,
'handleOnlyReportedErrors' => $this->config->errorHandler->reportedOnly
]
);
}
if(in_array(self::EXCEPTION_HANDLER, $this->config->errorHandler->handlers, true)){
$errorHandlerDefinition->addSetup('registerExceptionHandler');
}
if(in_array(self::FATAL_HANDLER, $this->config->errorHandler->handlers, true)){
$errorHandlerDefinition->addSetup('registerFatalHandler');
}
}
}

public function afterCompile(ClassType $class): void
Expand All @@ -218,6 +254,11 @@ public function afterCompile(ClassType $class): void
$initialize->addBody('?::$logDirectory = ?;', [new PhpLiteral(Debugger::class), $this->config->logDir]);
}


if(!empty($this->config->errorHandler->handlers)){
$initialize->addBody('$this->getService(?);', [$this->prefix('errorHandler')]);
}

$initialize->addBody("// monolog\n($closure)();");
}

Expand Down
52 changes: 49 additions & 3 deletions tests/Monolog/ExtensionTest.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use Monolog\Processor\WebProcessor;
use Nette\Configurator;
use Tester\Assert;
use Tracy\Debugger;
use Tracy\ILogger;

require_once __DIR__ . '/../bootstrap.php';

Expand Down Expand Up @@ -56,9 +57,7 @@ class ExtensionTest extends \Tester\TestCase

public function testFunctional(): void
{
foreach (array_merge(glob(TEMP_DIR . '/*.log'), glob(TEMP_DIR . '/*.html')) as $logFile) {
unlink($logFile);
}
$this->cleanUpTemp();

Debugger::$logDirectory = TEMP_DIR;

Expand Down Expand Up @@ -257,6 +256,53 @@ class ExtensionTest extends \Tester\TestCase
Assert::type(FallbackNetteHandler::class, array_shift($handlers));
}, E_USER_DEPRECATED);
}

public function testErrorHandler(): void
{
Assert::error(function() {
$this->cleanUpTemp();
$this->createContainer('errorHandler');

Debugger::log('tracy message 1');
trigger_error('Custom user Notice', E_USER_NOTICE);
@trigger_error('Custom user Notice suppressed', E_USER_NOTICE);
trigger_error('Custom user Deprecated', E_USER_DEPRECATED);
@trigger_error('Custom user Deprecated suppressed', E_USER_DEPRECATED);

trigger_error('Custom user Warning', E_USER_WARNING);
@trigger_error('Custom user Warning suppressed', E_USER_WARNING);

Debugger::log('tracy warning message', ILogger::WARNING);

Assert::match(
'[%a%] tracy message 1 {"at":"%a%"} []' . "\n" ,
file_get_contents(TEMP_DIR . '/log/info.log')
);

Assert::match(
'[%a%] E_USER_NOTICE: Custom user Notice {%a%} []'. "\n".
'[%a%] E_USER_NOTICE: Custom user Notice suppressed {%a%} []'. "\n".
'[%a%] E_USER_DEPRECATED: Custom user Deprecated {%a%} []'. "\n".
'[%a%] E_USER_DEPRECATED: Custom user Deprecated suppressed {%a%} []'. "\n",
file_get_contents(TEMP_DIR . '/log/notice.log')
);

Assert::match(
'[%a%] E_USER_WARNING: Custom user Warning {%a%} []'. "\n".
'[%a%] E_USER_WARNING: Custom user Warning suppressed {%a%} []'. "\n".
'[%a%] tracy warning message {%a%} []'. "\n",
file_get_contents(TEMP_DIR . '/log/warning.log')
);
}, [E_USER_NOTICE, E_USER_DEPRECATED, E_USER_WARNING]);

}

private function cleanUpTemp(): void
{
foreach (array_merge(glob(TEMP_DIR . '/*.log'), glob(TEMP_DIR . '/*.html')) as $logFile) {
unlink($logFile);
}
}
}

(new ExtensionTest())->run();
6 changes: 6 additions & 0 deletions tests/Monolog/config/errorHandler.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
monolog:
logDir: %tempDir%/log
errorHandler:
handlers: ['exception', 'error', 'fatal']
reportedOnly: false
errorTypes: -1

0 comments on commit 3a2a700

Please sign in to comment.