Skip to content

Commit

Permalink
Merge pull request #14283 from craftcms/feature/getDefaultTarget
Browse files Browse the repository at this point in the history
Add getDefaultTarget to Dispatcher
  • Loading branch information
brandonkelly authored Feb 3, 2024
2 parents 37b19d7 + 29b7a7f commit 0682daa
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG-WIP.md
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@
- Added `craft\helpers\ProjectConfig::ensureAllEntryTypesProcessed()`.
- Added `craft\i18n\Locale::$aliasOf`.
- Added `craft\i18n\Locale::setDisplayName()`.
- Added `craft\log\Dispatcher::getDefaultTarget()`. ([#14283](https://github.com/craftcms/cms/pull/14283))
- Added `craft\migrations\BaseContentRefactorMigration`.
- Added `craft\models\EntryType::$color`.
- Added `craft\models\FieldLayout::getCardBodyFields()`.
Expand Down Expand Up @@ -380,6 +381,7 @@
- Renamed `craft\fields\Matrix::$maxBlocks` to `$maxEntries`.
- Renamed `craft\fields\Matrix::$minBlocks` to `$minEntries`.
- Renamed `craft\helpers\MailerHelper\EVENT_REGISTER_MAILER_TRANSPORT_TYPES` to `EVENT_REGISTER_MAILER_TRANSPORTS`.
- Renamed `craft\log\Dispatcher::getTargets()` to `getDefaultTargets()`. ([#14283](https://github.com/craftcms/cms/pull/14283))
- Renamed `craft\services\Addresses::getLayout()` to `getFieldLayout()`.
- Renamed `craft\services\Addresses::saveLayout()` to `saveFieldLayout()`.
- Renamed `craft\services\Utilities::EVENT_REGISTER_UTILITY_TYPES` to `EVENT_REGISTER_UTILITIES`.
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
- Component chips within component select inputs which have an “Edit” action can now be double-clicked on.
- The `queue/run` command now supports a `--job-id` option.
- Removed the `--force` option from the `up` command. `--isolated=0` should be used instead. ([#14270](https://github.com/craftcms/cms/pull/14270))
- Added `craft\log\Dispatcher::getDefaultTarget()`. ([#14283](https://github.com/craftcms/cms/pull/14283))
- Renamed `craft\log\Dispatcher::getTargets()` to `getDefaultTargets()`. ([#14283](https://github.com/craftcms/cms/pull/14283))
- Fixed a bug where newly-created inline Matrix entries could have validation errors.
- Fixed a bug where selecting a field type via keyboard was unreliable if a field type’s icon contained a `<style>` tag.
- Fixed an error that occurred if a component select was used for components with non-numeric IDs.
Expand Down
26 changes: 21 additions & 5 deletions src/log/Dispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,29 @@ public function init(): void
{
parent::init();

$this->targets = array_merge($this->getTargets(), $this->targets);
$this->targets = array_merge($this->getDefaultTargets()->all(), $this->targets);
}

/**
* @return array<MonologTarget>
* Gets the active default target, or one specified by key.
*
* @param string|null $key The target key to use (`web`, `console`, or `queue`).
* @return MonologTarget|null
* @since 5.0.0
*/
public function getTargets(): array
public function getDefaultTarget(?string $key = null): ?MonologTarget
{
$defaultTargets = $this->getDefaultTargets();

return $key === null
? $defaultTargets->first(fn(MonologTarget $target) => $target->enabled)
: $defaultTargets->get($key);
}

/**
* @return Collection<MonologTarget>
*/
public function getDefaultTargets(): Collection
{
// Warning - Don't do anything that could cause something to get logged from here!
// If the dispatcher is configured with flushInterval => 1, it could cause a PHP error if any log
Expand All @@ -58,7 +74,7 @@ public function getTargets(): array

// Only log console requests and web requests that aren't getAuthTimeout requests
if (!$isConsoleRequest && !Craft::$app->getUser()->enableSession) {
return [];
return Collection::make();
}

return Collection::make([
Expand All @@ -80,6 +96,6 @@ public function getTargets(): array
];

return [$name => Craft::createObject($config)];
})->all();
});
}
}

0 comments on commit 0682daa

Please sign in to comment.