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

[8.x] Add method for on-demand log creation #39273

Merged
merged 1 commit into from
Oct 20, 2021
Merged

Conversation

jobyh
Copy link
Contributor

@jobyh jobyh commented Oct 20, 2021

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.

use Illuminate\Support\Facades\Log;

$logger = Log::build([
    'driver' => 'single',
    'path' => storage_path('logs/custom.log'),
]);

$logger->info('Ahoy custom log!');

// Use in on-demand stack.
$stack = Log::stack(['syslog', $logger]);
$stack->warning('Watch out, stack!');

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.

return $this->channel($channel)->getHandlers();
return $channel instanceof LoggerInterface
? $channel->getHandlers()
: $this->channel($channel)->getHandlers();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain why this change was necessary?

Copy link
Contributor Author

@jobyh jobyh Oct 20, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The on-demand loggers are not resolvable by name using ->channel() as they do not have a name which is stored in config. This modification was to support using them in Log::stack() by including on-demand logger instance in the array alongside name strings of loggers from config/logging.php.

@taylorotwell taylorotwell merged commit 69a95de into laravel:8.x Oct 20, 2021
@jobyh
Copy link
Contributor Author

jobyh commented Oct 20, 2021

I'll put together a PR adding this to the docs tomorrow

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants