Skip to content

Commit

Permalink
Ensure dispatcher exists
Browse files Browse the repository at this point in the history
  • Loading branch information
bencroker committed Mar 5, 2024
1 parent cb5af3f commit 94d89c2
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
25 changes: 14 additions & 11 deletions src/Campaign.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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',
),
]);
}
}

/**
Expand Down

0 comments on commit 94d89c2

Please sign in to comment.