Skip to content

Commit 69a95de

Browse files
committed
[8.x] Add method for on-demand log creation
1 parent a161262 commit 69a95de

File tree

2 files changed

+78
-6
lines changed

2 files changed

+78
-6
lines changed

src/Illuminate/Log/LogManager.php

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -110,12 +110,13 @@ public function getChannels()
110110
* Attempt to get the log from the local cache.
111111
*
112112
* @param string $name
113+
* @param array|null $config
113114
* @return \Psr\Log\LoggerInterface
114115
*/
115-
protected function get($name)
116+
protected function get($name, ?array $config = null)
116117
{
117118
try {
118-
return $this->channels[$name] ?? with($this->resolve($name), function ($logger) use ($name) {
119+
return $this->channels[$name] ?? with($this->resolve($name, $config), function ($logger) use ($name) {
119120
return $this->channels[$name] = $this->tap($name, new Logger($logger, $this->app['events']));
120121
});
121122
} catch (Throwable $e) {
@@ -180,13 +181,14 @@ protected function createEmergencyLogger()
180181
* Resolve the given log instance by name.
181182
*
182183
* @param string $name
184+
* @param array|null $config
183185
* @return \Psr\Log\LoggerInterface
184186
*
185187
* @throws \InvalidArgumentException
186188
*/
187-
protected function resolve($name)
189+
protected function resolve($name, ?array $config = null)
188190
{
189-
$config = $this->configurationFor($name);
191+
$config = $config ?? $this->configurationFor($name);
190192

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

244246
$handlers = collect($config['channels'])->flatMap(function ($channel) {
245-
return $this->channel($channel)->getHandlers();
247+
return $channel instanceof LoggerInterface
248+
? $channel->getHandlers()
249+
: $this->channel($channel)->getHandlers();
246250
})->all();
247251

248252
$processors = collect($config['channels'])->flatMap(function ($channel) {
249-
return $this->channel($channel)->getProcessors();
253+
return $channel instanceof LoggerInterface
254+
? $channel->getProcessors()
255+
: $this->channel($channel)->getProcessors();
250256
})->all();
251257

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

639+
/**
640+
* Build an on-demand channel.
641+
*
642+
* @param array $config
643+
* @return \Psr\Log\LoggerInterface
644+
*/
645+
public function build(array $config)
646+
{
647+
return $this->get('ondemand', $config);
648+
}
649+
633650
/**
634651
* Dynamically call the default driver instance.
635652
*

tests/Log/LogManagerTest.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Monolog\Handler\StreamHandler;
1414
use Monolog\Handler\SyslogHandler;
1515
use Monolog\Logger as Monolog;
16+
use Monolog\Processor\UidProcessor;
1617
use Orchestra\Testbench\TestCase;
1718
use ReflectionProperty;
1819
use RuntimeException;
@@ -377,4 +378,58 @@ public function testLogManagerPurgeResolvedChannels()
377378

378379
$this->assertEmpty($manager->getChannels());
379380
}
381+
382+
public function testLogManagerCanBuildOnDemandChannel()
383+
{
384+
$manager = new LogManager($this->app);
385+
386+
$logger = $manager->build([
387+
'driver' => 'single',
388+
'path' => storage_path('logs/on-demand.log'),
389+
]);
390+
$handler = $logger->getLogger()->getHandlers()[0];
391+
392+
$this->assertInstanceOf(StreamHandler::class, $handler);
393+
394+
$url = new ReflectionProperty(get_class($handler), 'url');
395+
$url->setAccessible(true);
396+
397+
$this->assertSame(storage_path('logs/on-demand.log'), $url->getValue($handler));
398+
}
399+
400+
public function testLogManagerCanUseOnDemandChannelInOnDemandStack()
401+
{
402+
$manager = new LogManager($this->app);
403+
$this->app['config']->set('logging.channels.test', [
404+
'driver' => 'single',
405+
]);
406+
407+
$factory = new class()
408+
{
409+
public function __invoke()
410+
{
411+
return new Monolog(
412+
'uuid',
413+
[new StreamHandler(storage_path('logs/custom.log'))],
414+
[new UidProcessor()]
415+
);
416+
}
417+
};
418+
$channel = $manager->build([
419+
'driver' => 'custom',
420+
'via' => get_class($factory),
421+
]);
422+
$logger = $manager->stack(['test', $channel]);
423+
424+
$handler = $logger->getLogger()->getHandlers()[1];
425+
$processor = $logger->getLogger()->getProcessors()[0];
426+
427+
$this->assertInstanceOf(StreamHandler::class, $handler);
428+
$this->assertInstanceOf(UidProcessor::class, $processor);
429+
430+
$url = new ReflectionProperty(get_class($handler), 'url');
431+
$url->setAccessible(true);
432+
433+
$this->assertSame(storage_path('logs/custom.log'), $url->getValue($handler));
434+
}
380435
}

0 commit comments

Comments
 (0)