Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add getDefaultTarget to Dispatcher #14283

Merged
merged 4 commits into from
Feb 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
});
}
}