[HttpKernel][Monolog] Add PSR-3 support to the LoggerInterface

This commit is contained in:
Jordi Boggiano 2013-01-08 23:26:48 +01:00
parent 8c320b0dfd
commit 91a86f8bec
9 changed files with 107 additions and 51 deletions

View File

@ -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": {

View File

@ -47,10 +47,7 @@ class DebugHandler extends TestHandler implements DebugLoggerInterface
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]);

View File

@ -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\\": "" }

View File

@ -11,6 +11,8 @@
namespace Symfony\Component\HttpKernel\Log;
use Psr\Log\LoggerInterface as PsrLogger;
/**
* LoggerInterface.
*
@ -18,18 +20,13 @@ namespace Symfony\Component\HttpKernel\Log;
*
* @api
*/
interface LoggerInterface
interface LoggerInterface extends PsrLogger
{
/**
* @api
*/
public function emerg($message, array $context = array());
/**
* @api
*/
public function alert($message, array $context = array());
/**
* @api
*/
@ -44,19 +41,4 @@ interface LoggerInterface
* @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());
}

View File

@ -22,6 +22,13 @@ use Symfony\Component\HttpKernel\Log\LoggerInterface;
*/
class NullLogger implements LoggerInterface
{
/**
* @api
*/
public function log($level, $message, array $context = array())
{
}
/**
* @api
*/
@ -29,6 +36,13 @@ class NullLogger implements LoggerInterface
{
}
/**
* @api
*/
public function emergency($message, array $context = array())
{
}
/**
* @api
*/
@ -43,6 +57,13 @@ class NullLogger implements LoggerInterface
{
}
/**
* @api
*/
public function critical($message, array $context = array())
{
}
/**
* @api
*/
@ -50,6 +71,13 @@ class NullLogger implements LoggerInterface
{
}
/**
* @api
*/
public function error($message, array $context = array())
{
}
/**
* @api
*/
@ -57,6 +85,13 @@ class NullLogger implements LoggerInterface
{
}
/**
* @api
*/
public function warning($message, array $context = array())
{
}
/**
* @api
*/

View File

@ -30,7 +30,7 @@ class ControllerResolverTest extends \PHPUnit_Framework_TestCase
$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);

View File

@ -93,7 +93,7 @@ class ExceptionListenerTest extends \PHPUnit_Framework_TestCase
}
$this->assertEquals(3, $logger->countErrors());
$this->assertCount(3, $logger->getLogs('crit'));
$this->assertCount(3, $logger->getLogs('critical'));
}
public function provider()
@ -117,7 +117,7 @@ class TestLogger extends Logger implements DebugLoggerInterface
{
public function countErrors()
{
return count($this->logs['crit']);
return count($this->logs['critical']);
}
}

View File

@ -22,67 +22,107 @@ class Logger implements LoggerInterface
$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);
}
}

View File

@ -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.*",