Skip to content

Commit

Permalink
Merge branch 'logging-monolog-driver' of https://github.com/ralphschi…
Browse files Browse the repository at this point in the history
…ndler/laravel-framework into ralphschindler-logging-monolog-driver
  • Loading branch information
taylorotwell committed Mar 14, 2018
2 parents 701ac58 + 6fce7fd commit 614f4f9
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
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]));
}
}

0 comments on commit 614f4f9

Please sign in to comment.