[8.x] Add method for on-demand log creation #39273
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This patch proposes a new method
LogManager::build
to create on-demand logger instances.The implementation and method name are based on the recently added
FilesystemManager::build
method.Why is this useful?
Occasionally it would be useful to configure a logger at runtime. For example setting the log path based on a command option or writing entries to their own location for a niche use-case or debugging.
This could also be useful to divert to a one-off log from
artisan tinker
.I needed this functionality recently for a command to ingest several hundred models from an external XML data export. The XML was provided by a 3rd party and expected to contain a small number of errors which needed recording and communicating back for correction. To this end the command allows dry runs which do not modify the database but log results to a user-defined local path.
In order to write to a dedicated log file based on an optional parameter I ended up duplicating and adapting initialization code from within the core LogManager inside the command which felt a bit of a kludge. As use will be very infrequent adding an entry to the main
config/logging.php
seemed overkill.Is it backwards compatible?
The new functionality extends the existing public API so should not be a breaking change.