From 94d89c2fc6e5bed987c61ba63ddd1a9e3a57a335 Mon Sep 17 00:00:00 2001 From: bencroker Date: Tue, 5 Mar 2024 08:30:15 -0600 Subject: [PATCH] Ensure dispatcher exists --- CHANGELOG.md | 6 +++++- src/Campaign.php | 25 ++++++++++++++----------- 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 66f6bec4..db645cc3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,10 @@ # Release Notes for Campaign -## 2.12.2 - Unreleased +## 2.12.2 - 2024-03-05 + +### Changed + +- A custom log target is now only registered if a dispatcher exists. ### Security diff --git a/src/Campaign.php b/src/Campaign.php index 23db104c..2c4c5e5f 100644 --- a/src/Campaign.php +++ b/src/Campaign.php @@ -84,6 +84,7 @@ use yii\base\Controller; use yii\base\Event; use yii\di\Instance; +use yii\log\Dispatcher; use yii\log\Logger; use yii\queue\Queue; use yii\web\ForbiddenHttpException; @@ -513,17 +514,19 @@ function(Event $event) { */ private function _registerLogTarget(): void { - Craft::getLogger()->dispatcher->targets[] = new MonologTarget([ - 'name' => 'campaign', - 'categories' => ['campaign'], - 'level' => LogLevel::INFO, - 'logContext' => false, - 'allowLineBreaks' => false, - 'formatter' => new LineFormatter( - format: "[%datetime%] %message%\n", - dateFormat: 'Y-m-d H:i:s', - ), - ]); + if (Craft::getLogger()->dispatcher instanceof Dispatcher) { + Craft::getLogger()->dispatcher->targets[] = new MonologTarget([ + 'name' => 'campaign', + 'categories' => ['campaign'], + 'level' => LogLevel::INFO, + 'logContext' => false, + 'allowLineBreaks' => false, + 'formatter' => new LineFormatter( + format: "[%datetime%] %message%\n", + dateFormat: 'Y-m-d H:i:s', + ), + ]); + } } /**