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 9399a15 commit 072c7ca
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions src/Snaptcha.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use yii\base\ActionEvent;
use yii\base\Controller as BaseController;
use yii\base\Event;
use yii\log\Dispatcher;
use yii\log\Logger;

/**
Expand Down Expand Up @@ -108,17 +109,19 @@ function(Event $event) {
*/
private function registerLogTarget(): void
{
Craft::getLogger()->dispatcher->targets[] = new MonologTarget([
'name' => 'snaptcha',
'categories' => ['snaptcha'],
'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' => 'snaptcha',
'categories' => ['snaptcha'],
'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 072c7ca

Please sign in to comment.