Skip to content
This repository has been archived by the owner on Jul 4, 2018. It is now read-only.

Commit

Permalink
Allow strings for levels in the monolog service provider
Browse files Browse the repository at this point in the history
The monolog service provider requires a constant on Monolog\Logger to be
referenced, which has an integer value. This is not very practical when
working with external configuration files.

This patch accepts strings like debug, info, error, etc. that are
translated to the corresponding logger constant values.

This is an alternative to #882.
  • Loading branch information
igorw committed Dec 27, 2013
1 parent 282628d commit 121f3f2
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 3 deletions.
2 changes: 2 additions & 0 deletions doc/providers/monolog.rst
Expand Up @@ -19,6 +19,8 @@ Parameters
everything, ``INFO`` will log everything except ``DEBUG``,
etc.

May also be a string of ``DEBUG``, ``INFO``, ``WARNING``, ``ERROR``.

* **monolog.name** (optional): Name of the monolog channel,
defaults to ``myapp``.

Expand Down
23 changes: 21 additions & 2 deletions src/Silex/Provider/MonologServiceProvider.php
Expand Up @@ -36,7 +36,8 @@ public function register(Application $app)

if ($bridge = class_exists('Symfony\Bridge\Monolog\Logger')) {
$app['monolog.handler.debug'] = function () use ($app) {
return new DebugHandler($app['monolog.level']);
$level = static::translateLevel($app['monolog.level']);
return new DebugHandler($level);
};
}

Expand All @@ -55,7 +56,8 @@ public function register(Application $app)
});

$app['monolog.handler'] = function () use ($app) {
return new StreamHandler($app['monolog.logfile'], $app['monolog.level']);
$level = static::translateLevel($app['monolog.level']);
return new StreamHandler($app['monolog.logfile'], $level);
};

$app['monolog.level'] = function () {
Expand Down Expand Up @@ -92,4 +94,21 @@ public function boot(Application $app)
}
});
}

public static function translateLevel($name)
{
// level is already translated to logger constant, return as-is
if (is_int($name)) {
return $name;
}

$levels = Logger::getLevels();
$upper = strtoupper($name);

if (!isset($levels[$upper])) {
throw new \InvalidArgumentException("Provided logging level '$name' does not exist. Must be a valid monolog logging level.");
}

return $levels[$upper];
}
}
23 changes: 22 additions & 1 deletion tests/Silex/Tests/Provider/MonologServiceProviderTest.php
Expand Up @@ -136,6 +136,26 @@ public function testErrorLoggingGivesWayToSecurityExceptionHandling()
$this->assertEmpty($app['monolog.handler']->getRecords(), "Expected no logging to occur");
}

public function testStringErrorLevel()
{
$app = $this->getApplication();
$app['monolog.level'] = 'info';

$this->assertSame(Logger::INFO, $app['monolog.handler']->getLevel());
}

/**
* @expectedException InvalidArgumentException
* @expectedExceptionMessage Provided logging level 'foo' does not exist. Must be a valid monolog logging level.
*/
public function testNonExistentStringErrorLevel()
{
$app = $this->getApplication();
$app['monolog.level'] = 'foo';

$app['monolog.handler']->getLevel();
}

protected function assertMatchingRecord($pattern, $level, $handler)
{
$found = false;
Expand All @@ -156,7 +176,8 @@ protected function getApplication()
$app->register(new MonologServiceProvider());

$app['monolog.handler'] = $app->share(function () use ($app) {
return new TestHandler($app['monolog.level']);
$level = MonologServiceProvider::translateLevel($app['monolog.level']);
return new TestHandler($level);
});

return $app;
Expand Down

0 comments on commit 121f3f2

Please sign in to comment.