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
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
29 changes: 23 additions & 6 deletions src/Illuminate/Log/LogManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,13 @@ public function getChannels()
* Attempt to get the log from the local cache.
*
* @param string $name
* @param array|null $config
* @return \Psr\Log\LoggerInterface
*/
protected function get($name)
protected function get($name, ?array $config = null)
{
try {
return $this->channels[$name] ?? with($this->resolve($name), function ($logger) use ($name) {
return $this->channels[$name] ?? with($this->resolve($name, $config), function ($logger) use ($name) {
return $this->channels[$name] = $this->tap($name, new Logger($logger, $this->app['events']));
});
} catch (Throwable $e) {
Expand Down Expand Up @@ -180,13 +181,14 @@ protected function createEmergencyLogger()
* Resolve the given log instance by name.
*
* @param string $name
* @param array|null $config
* @return \Psr\Log\LoggerInterface
*
* @throws \InvalidArgumentException
*/
protected function resolve($name)
protected function resolve($name, ?array $config = null)
{
$config = $this->configurationFor($name);
$config = $config ?? $this->configurationFor($name);

if (is_null($config)) {
throw new InvalidArgumentException("Log [{$name}] is not defined.");
Expand Down Expand Up @@ -242,11 +244,15 @@ protected function createStackDriver(array $config)
}

$handlers = collect($config['channels'])->flatMap(function ($channel) {
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.

})->all();

$processors = collect($config['channels'])->flatMap(function ($channel) {
return $this->channel($channel)->getProcessors();
return $channel instanceof LoggerInterface
? $channel->getProcessors()
: $this->channel($channel)->getProcessors();
})->all();

if ($config['ignore_exceptions'] ?? false) {
Expand Down Expand Up @@ -630,6 +636,17 @@ public function log($level, $message, array $context = [])
$this->driver()->log($level, $message, $context);
}

/**
* Build an on-demand channel.
*
* @param array $config
* @return \Psr\Log\LoggerInterface
*/
public function build(array $config)
{
return $this->get('ondemand', $config);
}

/**
* Dynamically call the default driver instance.
*
Expand Down
55 changes: 55 additions & 0 deletions tests/Log/LogManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Monolog\Handler\StreamHandler;
use Monolog\Handler\SyslogHandler;
use Monolog\Logger as Monolog;
use Monolog\Processor\UidProcessor;
use Orchestra\Testbench\TestCase;
use ReflectionProperty;
use RuntimeException;
Expand Down Expand Up @@ -377,4 +378,58 @@ public function testLogManagerPurgeResolvedChannels()

$this->assertEmpty($manager->getChannels());
}

public function testLogManagerCanBuildOnDemandChannel()
{
$manager = new LogManager($this->app);

$logger = $manager->build([
'driver' => 'single',
'path' => storage_path('logs/on-demand.log'),
]);
$handler = $logger->getLogger()->getHandlers()[0];

$this->assertInstanceOf(StreamHandler::class, $handler);

$url = new ReflectionProperty(get_class($handler), 'url');
$url->setAccessible(true);

$this->assertSame(storage_path('logs/on-demand.log'), $url->getValue($handler));
}

public function testLogManagerCanUseOnDemandChannelInOnDemandStack()
{
$manager = new LogManager($this->app);
$this->app['config']->set('logging.channels.test', [
'driver' => 'single',
]);

$factory = new class()
{
public function __invoke()
{
return new Monolog(
'uuid',
[new StreamHandler(storage_path('logs/custom.log'))],
[new UidProcessor()]
);
}
};
$channel = $manager->build([
'driver' => 'custom',
'via' => get_class($factory),
]);
$logger = $manager->stack(['test', $channel]);

$handler = $logger->getLogger()->getHandlers()[1];
$processor = $logger->getLogger()->getProcessors()[0];

$this->assertInstanceOf(StreamHandler::class, $handler);
$this->assertInstanceOf(UidProcessor::class, $processor);

$url = new ReflectionProperty(get_class($handler), 'url');
$url->setAccessible(true);

$this->assertSame(storage_path('logs/custom.log'), $url->getValue($handler));
}
}