Skip to content

Commit 7acb44d

Browse files
committed
Merge branch 'jobyh/8.x' into 8.x
2 parents cdb05c5 + fec8c5d commit 7acb44d

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
@@ -61,6 +61,17 @@ public function __construct($app)
6161
$this->app = $app;
6262
}
6363

64+
/**
65+
* Build an on-demand log channel.
66+
*
67+
* @param array $config
68+
* @return \Psr\Log\LoggerInterface
69+
*/
70+
public function build(array $config)
71+
{
72+
return $this->get('ondemand', $config);
73+
}
74+
6475
/**
6576
* Create a new, on-demand aggregate logger instance.
6677
*
@@ -110,12 +121,13 @@ public function getChannels()
110121
* Attempt to get the log from the local cache.
111122
*
112123
* @param string $name
124+
* @param array|null $config
113125
* @return \Psr\Log\LoggerInterface
114126
*/
115-
protected function get($name)
127+
protected function get($name, ?array $config = null)
116128
{
117129
try {
118-
return $this->channels[$name] ?? with($this->resolve($name), function ($logger) use ($name) {
130+
return $this->channels[$name] ?? with($this->resolve($name, $config), function ($logger) use ($name) {
119131
return $this->channels[$name] = $this->tap($name, new Logger($logger, $this->app['events']));
120132
});
121133
} catch (Throwable $e) {
@@ -180,13 +192,14 @@ protected function createEmergencyLogger()
180192
* Resolve the given log instance by name.
181193
*
182194
* @param string $name
195+
* @param array|null $config
183196
* @return \Psr\Log\LoggerInterface
184197
*
185198
* @throws \InvalidArgumentException
186199
*/
187-
protected function resolve($name)
200+
protected function resolve($name, ?array $config = null)
188201
{
189-
$config = $this->configurationFor($name);
202+
$config = $config ?? $this->configurationFor($name);
190203

191204
if (is_null($config)) {
192205
throw new InvalidArgumentException("Log [{$name}] is not defined.");
@@ -242,11 +255,15 @@ protected function createStackDriver(array $config)
242255
}
243256

244257
$handlers = collect($config['channels'])->flatMap(function ($channel) {
245-
return $this->channel($channel)->getHandlers();
258+
return $channel instanceof LoggerInterface
259+
? $channel->getHandlers()
260+
: $this->channel($channel)->getHandlers();
246261
})->all();
247262

248263
$processors = collect($config['channels'])->flatMap(function ($channel) {
249-
return $this->channel($channel)->getProcessors();
264+
return $channel instanceof LoggerInterface
265+
? $channel->getProcessors()
266+
: $this->channel($channel)->getProcessors();
250267
})->all();
251268

252269
if ($config['ignore_exceptions'] ?? false) {

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)