Skip to content

Commit

Permalink
[HttpKernel][Monolog] Add PSR-3 support to the LoggerInterface
Browse files Browse the repository at this point in the history
  • Loading branch information
Seldaek committed Jan 8, 2013
1 parent 8c320b0 commit 91a86f8
Show file tree
Hide file tree
Showing 9 changed files with 107 additions and 51 deletions.
3 changes: 2 additions & 1 deletion composer.json
Expand Up @@ -59,7 +59,8 @@
"doctrine/data-fixtures": "1.0.*",
"doctrine/dbal": ">=2.2,<2.4-dev",
"doctrine/orm": ">=2.2.3,<2.4-dev",
"monolog/monolog": ">=1.0,<1.3-dev",
"monolog/monolog": "~1.3",
"psr/log": "~1.0",
"propel/propel1": "dev-master"
},
"autoload": {
Expand Down
5 changes: 1 addition & 4 deletions src/Symfony/Bridge/Monolog/Handler/DebugHandler.php
Expand Up @@ -47,10 +47,7 @@ public function getLogs()
public function countErrors()
{
$cnt = 0;
$levels = array(Logger::ERROR, Logger::CRITICAL, Logger::ALERT);
if (defined('Monolog\Logger::EMERGENCY')) {
$levels[] = Logger::EMERGENCY;
}
$levels = array(Logger::ERROR, Logger::CRITICAL, Logger::ALERT, Logger::EMERGENCY);
foreach ($levels as $level) {
if (isset($this->recordsByLevel[$level])) {
$cnt += count($this->recordsByLevel[$level]);
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Bridge/Monolog/composer.json
Expand Up @@ -18,7 +18,7 @@
"require": {
"php": ">=5.3.3",
"symfony/http-kernel": "2.2.*",
"monolog/monolog": ">=1.0,<1.3-dev"
"monolog/monolog": "~1.3"
},
"autoload": {
"psr-0": { "Symfony\\Bridge\\Monolog\\": "" }
Expand Down
24 changes: 3 additions & 21 deletions src/Symfony/Component/HttpKernel/Log/LoggerInterface.php
Expand Up @@ -11,25 +11,22 @@

namespace Symfony\Component\HttpKernel\Log;

use Psr\Log\LoggerInterface as PsrLogger;

/**
* LoggerInterface.
*
* @author Fabien Potencier <fabien@symfony.com>
*
* @api
*/
interface LoggerInterface
interface LoggerInterface extends PsrLogger
{
/**
* @api
*/
public function emerg($message, array $context = array());

/**
* @api
*/
public function alert($message, array $context = array());

/**
* @api
*/
Expand All @@ -44,19 +41,4 @@ public function err($message, array $context = array());
* @api
*/
public function warn($message, array $context = array());

/**
* @api
*/
public function notice($message, array $context = array());

/**
* @api
*/
public function info($message, array $context = array());

/**
* @api
*/
public function debug($message, array $context = array());
}
35 changes: 35 additions & 0 deletions src/Symfony/Component/HttpKernel/Log/NullLogger.php
Expand Up @@ -22,13 +22,27 @@
*/
class NullLogger implements LoggerInterface
{
/**
* @api
*/
public function log($level, $message, array $context = array())
{
}

/**
* @api
*/
public function emerg($message, array $context = array())
{
}

/**
* @api
*/
public function emergency($message, array $context = array())
{
}

/**
* @api
*/
Expand All @@ -43,20 +57,41 @@ public function crit($message, array $context = array())
{
}

/**
* @api
*/
public function critical($message, array $context = array())
{
}

/**
* @api
*/
public function err($message, array $context = array())
{
}

/**
* @api
*/
public function error($message, array $context = array())
{
}

/**
* @api
*/
public function warn($message, array $context = array())
{
}

/**
* @api
*/
public function warning($message, array $context = array())
{
}

/**
* @api
*/
Expand Down
Expand Up @@ -30,7 +30,7 @@ public function testGetController()

$request = Request::create('/');
$this->assertFalse($resolver->getController($request), '->getController() returns false when the request has no _controller attribute');
$this->assertEquals(array('Unable to look for the controller as the "_controller" parameter is missing'), $logger->getLogs('warn'));
$this->assertEquals(array('Unable to look for the controller as the "_controller" parameter is missing'), $logger->getLogs('warning'));

$request->attributes->set('_controller', 'Symfony\Component\HttpKernel\Tests\ControllerResolverTest::testGetController');
$controller = $resolver->getController($request);
Expand Down
Expand Up @@ -93,7 +93,7 @@ public function testHandleWithLogger($event, $event2)
}

$this->assertEquals(3, $logger->countErrors());
$this->assertCount(3, $logger->getLogs('crit'));
$this->assertCount(3, $logger->getLogs('critical'));
}

public function provider()
Expand All @@ -117,7 +117,7 @@ class TestLogger extends Logger implements DebugLoggerInterface
{
public function countErrors()
{
return count($this->logs['crit']);
return count($this->logs['critical']);
}
}

Expand Down
80 changes: 60 additions & 20 deletions src/Symfony/Component/HttpKernel/Tests/Logger.php
Expand Up @@ -22,67 +22,107 @@ public function __construct()
$this->clear();
}

public function getLogs($priority = false)
public function getLogs($level = false)
{
return false === $priority ? $this->logs : $this->logs[$priority];
return false === $level ? $this->logs : $this->logs[$level];
}

public function clear()
{
$this->logs = array(
'emerg' => array(),
'emergency' => array(),
'alert' => array(),
'crit' => array(),
'err' => array(),
'warn' => array(),
'critical' => array(),
'error' => array(),
'warning' => array(),
'notice' => array(),
'info' => array(),
'debug' => array(),
);
}

public function log($message, $priority)
public function log($level, $message, array $context = array())
{
$this->logs[$priority][] = $message;
$this->logs[$level][] = $message;
}

public function emerg($message, array $context = array())
public function emergency($message, array $context = array())
{
$this->log($message, 'emerg');
$this->log('emergency', $message, $context);
}

public function alert($message, array $context = array())
{
$this->log($message, 'alert');
$this->log('alert', $message, $context);
}

public function crit($message, array $context = array())
public function critical($message, array $context = array())
{
$this->log($message, 'crit');
$this->log('critical', $message, $context);
}

public function err($message, array $context = array())
public function error($message, array $context = array())
{
$this->log($message, 'err');
$this->log('error', $message, $context);
}

public function warn($message, array $context = array())
public function warning($message, array $context = array())
{
$this->log($message, 'warn');
$this->log('warning', $message, $context);
}

public function notice($message, array $context = array())
{
$this->log($message, 'notice');
$this->log('notice', $message, $context);
}

public function info($message, array $context = array())
{
$this->log($message, 'info');
$this->log('info', $message, $context);
}

public function debug($message, array $context = array())
{
$this->log($message, 'debug');
$this->log('debug', $message, $context);
}

/**
* @deprecated
*/
public function emerg($message, array $context = array())
{
trigger_error('Use emergency() which is PSR-3 compatible', E_USER_DEPRECATED);

$this->log('emergency', $message, $context);
}

/**
* @deprecated
*/
public function crit($message, array $context = array())
{
trigger_error('Use crit() which is PSR-3 compatible', E_USER_DEPRECATED);

$this->log('critical', $message, $context);
}

/**
* @deprecated
*/
public function err($message, array $context = array())
{
trigger_error('Use err() which is PSR-3 compatible', E_USER_DEPRECATED);

$this->log('error', $message, $context);
}

/**
* @deprecated
*/
public function warn($message, array $context = array())
{
trigger_error('Use warn() which is PSR-3 compatible', E_USER_DEPRECATED);

$this->log('warning', $message, $context);
}
}
3 changes: 2 additions & 1 deletion src/Symfony/Component/HttpKernel/composer.json
Expand Up @@ -18,7 +18,8 @@
"require": {
"php": ">=5.3.3",
"symfony/event-dispatcher": "2.2.*",
"symfony/http-foundation": "2.2.*"
"symfony/http-foundation": "2.2.*",
"psr/log": "~1.0"
},
"require-dev": {
"symfony/browser-kit": "2.2.*",
Expand Down

0 comments on commit 91a86f8

Please sign in to comment.