Skip to content

Commit

Permalink
Allow to change the formatter for the single, daily and syslog logging
Browse files Browse the repository at this point in the history
driver from the config file.
  • Loading branch information
Giulio Troccoli-Allard committed Dec 18, 2018
1 parent 853bdcd commit 3cb2721
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/Illuminate/Log/LogManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ protected function createSingleDriver(array $config)
new StreamHandler(
$config['path'], $this->level($config),
$config['bubble'] ?? true, $config['permission'] ?? null, $config['locking'] ?? false
)
), $config
),
]);
}
Expand All @@ -249,7 +249,7 @@ protected function createDailyDriver(array $config)
$this->prepareHandler(new RotatingFileHandler(
$config['path'], $config['days'] ?? 7, $this->level($config),
$config['bubble'] ?? true, $config['permission'] ?? null, $config['locking'] ?? false
)),
), $config),
]);
}

Expand Down Expand Up @@ -288,7 +288,7 @@ protected function createSyslogDriver(array $config)
return new Monolog($this->parseChannel($config), [
$this->prepareHandler(new SyslogHandler(
$this->app['config']['app.name'], $config['facility'] ?? LOG_USER, $this->level($config)
)),
), $config),
]);
}

Expand Down
126 changes: 126 additions & 0 deletions tests/Log/LogManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Illuminate\Tests\Log;

use Monolog\Formatter\LineFormatter;
use Monolog\Handler\SyslogHandler;
use ReflectionProperty;
use Illuminate\Log\Logger;
use Illuminate\Log\LogManager;
Expand Down Expand Up @@ -116,4 +118,128 @@ public function testLogManagerCreatesMonologHandlerWithConfiguredFormatter()

$this->assertEquals('Y/m/d--test', $dateFormat->getValue($formatter));
}

public function testLogManagerCreateSingleDriverWithConfiguredFormatter()
{
$config = $this->app['config'];
$config->set('logging.channels.defaultsingle', [
'driver' => 'single',
'name' => 'ds',
'path' => storage_path('logs/laravel.log'),
]);

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

// create logger with handler specified from configuration
$logger = $manager->channel('defaultsingle');
$handler = $logger->getLogger()->getHandlers()[0];
$formatter = $handler->getFormatter();

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

$config->set('logging.channels.formattedsingle', [
'driver' => 'single',
'name' => 'fs',
'path' => storage_path('logs/laravel.log'),
'formatter' => HtmlFormatter::class,
'formatter_with' => [
'dateFormat' => 'Y/m/d--test'
]
]);

$logger = $manager->channel('formattedsingle');
$handler = $logger->getLogger()->getHandlers()[0];
$formatter = $handler->getFormatter();

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

$dateFormat = new ReflectionProperty(get_class($formatter), 'dateFormat');
$dateFormat->setAccessible(true);

$this->assertEquals('Y/m/d--test', $dateFormat->getValue($formatter));
}

public function testLogManagerCreateDailyDriverWithConfiguredFormatter()
{
$config = $this->app['config'];
$config->set('logging.channels.defaultdaily', [
'driver' => 'daily',
'name' => 'dd',
'path' => storage_path('logs/laravel.log'),
]);

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

// create logger with handler specified from configuration
$logger = $manager->channel('defaultdaily');
$handler = $logger->getLogger()->getHandlers()[0];
$formatter = $handler->getFormatter();

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

$config->set('logging.channels.formatteddaily', [
'driver' => 'daily',
'name' => 'fd',
'path' => storage_path('logs/laravel.log'),
'formatter' => HtmlFormatter::class,
'formatter_with' => [
'dateFormat' => 'Y/m/d--test'
]
]);

$logger = $manager->channel('formatteddaily');
$handler = $logger->getLogger()->getHandlers()[0];
$formatter = $handler->getFormatter();

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

$dateFormat = new ReflectionProperty(get_class($formatter), 'dateFormat');
$dateFormat->setAccessible(true);

$this->assertEquals('Y/m/d--test', $dateFormat->getValue($formatter));
}

public function testLogManagerCreateSyslogDriverWithConfiguredFormatter()
{
$config = $this->app['config'];
$config->set('logging.channels.defaultsyslog', [
'driver' => 'syslog',
'name' => 'ds',
]);

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

// create logger with handler specified from configuration
$logger = $manager->channel('defaultsyslog');
$handler = $logger->getLogger()->getHandlers()[0];
$formatter = $handler->getFormatter();

$this->assertInstanceOf(SyslogHandler::class, $handler);
$this->assertInstanceOf(LineFormatter::class, $formatter);

$config->set('logging.channels.formattedsyslog', [
'driver' => 'syslog',
'name' => 'fs',
'formatter' => HtmlFormatter::class,
'formatter_with' => [
'dateFormat' => 'Y/m/d--test'
]
]);

$logger = $manager->channel('formattedsyslog');
$handler = $logger->getLogger()->getHandlers()[0];
$formatter = $handler->getFormatter();

$this->assertInstanceOf(SyslogHandler::class, $handler);
$this->assertInstanceOf(HtmlFormatter::class, $formatter);

$dateFormat = new ReflectionProperty(get_class($formatter), 'dateFormat');
$dateFormat->setAccessible(true);

$this->assertEquals('Y/m/d--test', $dateFormat->getValue($formatter));
}
}

0 comments on commit 3cb2721

Please sign in to comment.