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

[5.6] LogManager driver capable of producing logger with any Monolog handler #23527

Merged
merged 5 commits into from
Mar 14, 2018
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
27 changes: 27 additions & 0 deletions src/Illuminate/Log/LogManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,33 @@ protected function createErrorlogDriver(array $config)
]);
}

/**
* Create an instance of any handler available in Monolog from configuration.
*
* @param array $config
* @return \Monolog\Logger
* @throws \Illuminate\Contracts\Container\BindingResolutionException
*/
protected function createMonologDriver(array $config)
{
if (isset($config['handler_type'])) {
$handlerClass = 'Monolog\Handler\\'.ucfirst($config['handler_type']).'Handler';
} elseif (isset($config['handler_class'])) {
$handlerClass = $config['handler_class'];
} else {
throw new InvalidArgumentException('"handler_type" or "handler_class" is required for the monolog driver');
}

if (! is_a($handlerClass, HandlerInterface::class, true)) {
throw new InvalidArgumentException($handlerClass.' must be an instance of '.HandlerInterface::class);
}

return new Monolog(
$this->parseChannel($config),
[$this->prepareHandler($this->app->make($handlerClass, $config['handler_params'] ?? []))]
);
}

/**
* Prepare the handlers for usage by Monolog.
*
Expand Down
55 changes: 55 additions & 0 deletions tests/Log/LogManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@

namespace Illuminate\Tests\Log;

use ReflectionProperty;
use Illuminate\Log\Logger;
use Illuminate\Log\LogManager;
use Monolog\Logger as Monolog;
use Orchestra\Testbench\TestCase;
use Monolog\Handler\StreamHandler;
use Monolog\Handler\LogEntriesHandler;

class LogManagerTest extends TestCase
{
Expand All @@ -16,4 +21,54 @@ public function testLogManagerCachesLoggerInstances()

$this->assertSame($logger1, $logger2);
}

public function testLogManagerCreatesConfiguredMonologHandler()
{
$config = $this->app['config'];
$config->set('logging.channels.nonbubblingstream', [
'driver' => 'monolog',
'name' => 'foobar',
'handler_type' => 'stream',
'handler_params' => [
'stream' => 'php://stderr',
'level' => Monolog::NOTICE,
'bubble' => false,
],
]);

$manager = new LogManager($this->app);

// create logger with handler specified from configuration
$logger = $manager->channel('nonbubblingstream');
$handlers = $logger->getLogger()->getHandlers();

$this->assertInstanceOf(Logger::class, $logger);
$this->assertEquals('foobar', $logger->getName());
$this->assertCount(1, $handlers);
$this->assertInstanceOf(StreamHandler::class, $handlers[0]);
$this->assertEquals(Monolog::NOTICE, $handlers[0]->getLevel());
$this->assertFalse($handlers[0]->getBubble());

$url = new ReflectionProperty(get_class($handlers[0]), 'url');
$url->setAccessible(true);
$this->assertEquals('php://stderr', $url->getValue($handlers[0]));

$config->set('logging.channels.logentries', [
'driver' => 'monolog',
'name' => 'le',
'handler_type' => 'LogEntries',
'handler_params' => [
'token' => '123456789',
],
]);

$logger = $manager->channel('logentries');
$handlers = $logger->getLogger()->getHandlers();

$logToken = new ReflectionProperty(get_class($handlers[0]), 'logToken');
$logToken->setAccessible(true);

$this->assertInstanceOf(LogEntriesHandler::class, $handlers[0]);
$this->assertEquals('123456789', $logToken->getValue($handlers[0]));
}
}