diff --git a/composer.json b/composer.json index 8e7ae2b..20e46d4 100644 --- a/composer.json +++ b/composer.json @@ -23,7 +23,7 @@ ], "require": { "php": ">=7.4 <8.1", - "nette/di": "^3.0.5", + "nette/di": "^3.0.7", "nette/utils": "^3.0", "monolog/monolog": "^2.2", "nette/schema": "^1.2" diff --git a/src/DI/MonologExtension.php b/src/DI/MonologExtension.php index 3e2b469..4b5f9b0 100644 --- a/src/DI/MonologExtension.php +++ b/src/DI/MonologExtension.php @@ -64,7 +64,12 @@ public function getConfigSchema(): Schema 'tracyHook' => Expect::bool(true), 'tracyBaseUrl' => Expect::string(), 'usePriorityProcessor' => Expect::bool(true), - 'registerFallback' => Expect::bool(false), + 'fallback' => Expect::structure([ + 'register' => Expect::bool(false), + 'defaultFormat' => Expect::string('[%datetime%] %message% %context% %extra%'), + 'priorityFormat' => Expect::string('[%datetime%] %level_name%: %message% %context% %extra%'), + ]), + 'registerFallback' => Expect::bool(false)->deprecated(), 'accessPriority' => Expect::string(ILogger::INFO), ]); } @@ -86,6 +91,11 @@ public function loadConfiguration(): void # Load other stuff $this->loadHandlers(); $this->loadProcessors(); + + # registerFallback transformation + if ($this->config->registerFallback) { + $this->config->fallback->register = true; + } } protected function loadHandlers(): void @@ -132,23 +142,23 @@ protected function getDefinition($definition, string $name) { $builder = $this->getContainerBuilder(); - // String definition + # String definition if (is_string($definition)) { - // @alias + # @alias if (Strings::startsWith($definition, '@')) { $defName = substr($definition, 1); return $builder->hasDefinition($defName) ? $builder->getDefinition($defName) : $definition; } - // Inline definition + # Inline definition return $builder ->addDefinition($name, (new ServiceDefinition)->setType($definition)) ->setAutowired(false); } - // Add service and set autowired + # Add service and set autowired if (is_array($definition)) { $definition['autowired'] ??= false; } @@ -156,7 +166,7 @@ protected function getDefinition($definition, string $name) $name => $definition, ]); - return $name; + return '@' . $name; } public function beforeCompile(): void @@ -168,20 +178,22 @@ public function beforeCompile(): void $logger->setArgument('handlers', $this->getDefinitionsByPriority($this->handlers)); $logger->setArgument('processors', $this->getDefinitionsByPriority($this->processors)); - // Register fallback, if no handlers is set or registerFallback is true - if (!$this->handlers || $this->config->registerFallback) { + # Register fallback, if no handlers is set or fallback->register is true + if (!$this->handlers || $this->config->fallback->register) { $builder ->addDefinition($this->prefix('fallback')) ->setFactory(FallbackNetteHandler::class, [ 'appName' => $this->config->name, 'logDir' => $this->config->logDir, + 'defaultFormat' => $this->config->fallback->defaultFormat, + 'priorityFormat' => $this->config->fallback->priorityFormat, ]) ->setAutowired(false); $logger->addSetup('pushHandler', ['@' . $this->prefix('fallback')]); } - // Decorator for LoggerAwareInterface + # Decorator for LoggerAwareInterface foreach ($builder->findByType(LoggerAwareInterface::class) as $service) { /** @var ServiceDefinition $service */ $service->addSetup('setLogger', ['@' . $this->prefix('logger')]); diff --git a/src/Handler/FallbackNetteHandler.php b/src/Handler/FallbackNetteHandler.php index 91ff9dd..c6ae1c6 100644 --- a/src/Handler/FallbackNetteHandler.php +++ b/src/Handler/FallbackNetteHandler.php @@ -19,21 +19,25 @@ class FallbackNetteHandler extends \Monolog\Handler\ErrorLogHandler { - use \Nette\SmartObject; - private string $appName; private string $logDir; private LineFormatter $defaultFormatter; private LineFormatter $priorityFormatter; - public function __construct(string $appName, string $logDir, bool $expandNewlines = FALSE, int $level = MonologLogger::DEBUG) - { + public function __construct( + string $appName, + string $logDir, + string $defaultFormat, + string $priorityFormat, + bool $expandNewlines = FALSE, + int $level = MonologLogger::DEBUG + ) { parent::__construct(self::SAPI, $level, TRUE, $expandNewlines); $this->appName = $appName; $this->logDir = $logDir; - $this->defaultFormatter = new LineFormatter('[%datetime%] %message% %context% %extra%'); - $this->priorityFormatter = new LineFormatter('[%datetime%] %level_name%: %message% %context% %extra%'); + $this->defaultFormatter = new LineFormatter($defaultFormat); + $this->priorityFormatter = new LineFormatter($priorityFormat); } /** diff --git a/tests/Monolog/ExtensionTest.phpt b/tests/Monolog/ExtensionTest.phpt index d42130f..30aea67 100644 --- a/tests/Monolog/ExtensionTest.phpt +++ b/tests/Monolog/ExtensionTest.phpt @@ -10,6 +10,7 @@ namespace Tests\Monolog; use Mallgroup\Monolog\CustomChannel; use Mallgroup\Monolog\DI\MonologExtension; +use Mallgroup\Monolog\Handler\FallbackNetteHandler; use Mallgroup\Monolog\Logger as MonologLogger; use Mallgroup\Monolog\Processor\PriorityProcessor; use Mallgroup\Monolog\Processor\TracyExceptionProcessor; @@ -247,6 +248,15 @@ class ExtensionTest extends \Tester\TestCase Assert::type(GitProcessor::class, array_shift($processors)); } + public function testDeprecated(): void { + Assert::error(function(){ + $dic = $this->createContainer('deprecated'); + $logger = $dic->getByType(MonologLogger::class); + $handlers = $logger->getHandlers(); + + Assert::type(FallbackNetteHandler::class, array_shift($handlers)); + }, E_USER_DEPRECATED); + } } (new ExtensionTest())->run(); diff --git a/tests/Monolog/FallbackNetteHandlerTest.phpt b/tests/Monolog/FallbackNetteHandlerTest.phpt index 0f5c496..3fc5439 100644 --- a/tests/Monolog/FallbackNetteHandlerTest.phpt +++ b/tests/Monolog/FallbackNetteHandlerTest.phpt @@ -30,7 +30,12 @@ class FallbackNetteHandlerTest extends \Tester\TestCase unlink($logFile); } - $this->handler = new FallbackNetteHandler('mall', $this->logDir); + $this->handler = new FallbackNetteHandler( + 'mall', + $this->logDir, + '[%datetime%] %message% %context% %extra%', + '[%datetime%] %level_name%: %message% %context% %extra%' + ); $this->now = new DateTime(); } diff --git a/tests/Monolog/config/default.neon b/tests/Monolog/config/default.neon index 2173e8d..9ccc00c 100644 --- a/tests/Monolog/config/default.neon +++ b/tests/Monolog/config/default.neon @@ -1,3 +1,4 @@ monolog: - registerFallback: true + fallback: + register: true logDir: %tempDir%/log \ No newline at end of file diff --git a/tests/Monolog/config/deprecated.neon b/tests/Monolog/config/deprecated.neon new file mode 100644 index 0000000..2173e8d --- /dev/null +++ b/tests/Monolog/config/deprecated.neon @@ -0,0 +1,3 @@ +monolog: + registerFallback: true + logDir: %tempDir%/log \ No newline at end of file diff --git a/tests/Monolog/config/handlers.neon b/tests/Monolog/config/handlers.neon index ff68465..c5f6504 100644 --- a/tests/Monolog/config/handlers.neon +++ b/tests/Monolog/config/handlers.neon @@ -1,6 +1,10 @@ +services: + browserConsoleHandler: Monolog\Handler\BrowserConsoleHandler + monolog: logDir: %tempDir%/log handlers: - - Monolog\Handler\BrowserConsoleHandler - - Monolog\Handler\ChromePHPHandler + - @browserConsoleHandler + - + factory: Monolog\Handler\ChromePHPHandler - Monolog\Handler\NewRelicHandler