1
0
mirror of https://github.com/guzzle/guzzle.git synced 2025-05-02 04:38:14 +02:00

[Common] Adding a Monolog log adapter. Fixing the ZendLogAdapter and updating logging unit tests. Adding Monolog as a submodule and adding to to the test bootstrap.

This commit is contained in:
Michael Dowling 2011-04-12 22:57:12 -05:00
parent 4c2405ffed
commit 782b323893
7 changed files with 100 additions and 22 deletions

3
.gitmodules vendored
View File

@ -4,3 +4,6 @@
[submodule "vendor/Doctrine"] [submodule "vendor/Doctrine"]
path = vendor/Doctrine path = vendor/Doctrine
url = https://github.com/doctrine/common url = https://github.com/doctrine/common
[submodule "vendor/Monolog"]
path = vendor/Monolog
url = https://github.com/Seldaek/monolog.git

View File

@ -0,0 +1,45 @@
<?php
/**
* @package Guzzle PHP <http://www.guzzlephp.org>
* @license See the LICENSE file that was distributed with this source code.
*/
namespace Guzzle\Common\Log;
use Monolog\Logger;
/**
* Monolog log adapter
*
* @author Michael Dowling <michael@guzzlephp.org>
* @link https://github.com/Seldaek/monolog
*/
class MonologLogAdapter extends AbstractLogAdapter
{
/**
* Adapt a Monolog Logger object
*
* @param Logger $logObject Log object to adapt
* @throws InvalidArgumentException
*/
public function __construct($logObject)
{
if (!($logObject instanceof Logger)) {
throw new \InvalidArgumentException(
'Object must be an instance of Monolog\Logger'
);
}
$this->log = $logObject;
}
/**
* {@inheritdoc}
*/
public function log($message, $priority = LOG_INFO, $extras = null)
{
$this->log->addRecord($priority, $message);
return $this;
}
}

View File

@ -33,7 +33,7 @@ class ZendLogAdapter extends AbstractLogAdapter
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function log($message, $priority = self::INFO, $extras = null) public function log($message, $priority = LOG_INFO, $extras = null)
{ {
$this->log->log($message, $priority, $extras); $this->log->log($message, $priority, $extras);

View File

@ -0,0 +1,47 @@
<?php
/**
* @package Guzzle PHP <http://www.guzzlephp.org>
* @license See the LICENSE file that was distributed with this source code.
*/
namespace Guzzle\Tests\Common\Log;
use Guzzle\Common\Log\MonologLogAdapter;
use Monolog\Logger;
use Monolog\Handler\TestHandler;
/**
* @author Michael Dowling <michael@guzzlephp.org>
*/
class MonologLogAdapterTest extends \Guzzle\Tests\GuzzleTestCase
{
/**
* @covers Guzzle\Common\Log\MonologLogAdapter::__construct
* @expectedException InvalidArgumentException
*/
public function testEnforcesType()
{
// A successful construction
$log = new Logger('test');
$log->pushHandler(new TestHandler());
$adapter = new MonologLogAdapter($log);
// Throws an exception
$this->adapter = new MonologLogAdapter(new \stdClass());
}
/**
* @covers Guzzle\Common\Log\MonologLogAdapter::log
*/
public function testLogsMessagesToAdaptedObject()
{
$log = new Logger('test');
$handler = new TestHandler();
$log->pushHandler($handler);
$adapter = new MonologLogAdapter($log);
$adapter->log('test!', Logger::INFO);
$this->assertTrue($handler->hasInfoRecords());
}
}

View File

@ -37,27 +37,12 @@ class ZendLogAdapterTest extends \Guzzle\Tests\GuzzleTestCase
$this->adapter = new ZendLogAdapter($this->log); $this->adapter = new ZendLogAdapter($this->log);
} }
/**
* Check for the existence of the Zend_Framework in your path
*/
protected function zfSkip()
{
if (!class_exists('\Zend_Log')) {
$this->markTestSkipped(
'The Zend Framework is not present in your path'
);
return;
}
}
/** /**
* @covers Guzzle\Common\Log\AbstractLogAdapter::__construct * @covers Guzzle\Common\Log\AbstractLogAdapter::__construct
* @expectedException InvalidArgumentException * @expectedException InvalidArgumentException
*/ */
public function testConstruct() public function testEnforcesType()
{ {
$this->zfSkip();
// A successful construction // A successful construction
$this->adapter = new ZendLogAdapter($this->log, new Collection()); $this->adapter = new ZendLogAdapter($this->log, new Collection());
@ -71,8 +56,6 @@ class ZendLogAdapterTest extends \Guzzle\Tests\GuzzleTestCase
*/ */
public function testLogsMessagesToAdaptedObject() public function testLogsMessagesToAdaptedObject()
{ {
$this->zfSkip();
// Test without a priority // Test without a priority
$this->adapter->log('test', \LOG_NOTICE, 'guzzle.common.log.adapter.zend_log_adapter', 'localhost'); $this->adapter->log('test', \LOG_NOTICE, 'guzzle.common.log.adapter.zend_log_adapter', 'localhost');
$this->assertEquals(1, substr_count(ob_get_contents(), 'test')); $this->assertEquals(1, substr_count(ob_get_contents(), 'test'));
@ -87,8 +70,6 @@ class ZendLogAdapterTest extends \Guzzle\Tests\GuzzleTestCase
*/ */
public function testExposesAdaptedLogObject() public function testExposesAdaptedLogObject()
{ {
$this->zfSkip();
$this->assertEquals($this->log, $this->adapter->getLogObject()); $this->assertEquals($this->log, $this->adapter->getLogObject());
} }
} }

View File

@ -17,7 +17,8 @@ $classLoader = new \Symfony\Component\ClassLoader\UniversalClassLoader();
$classLoader->registerNamespaces(array( $classLoader->registerNamespaces(array(
'Guzzle\Tests' => __DIR__, 'Guzzle\Tests' => __DIR__,
'Guzzle' => __DIR__ . '/../src', 'Guzzle' => __DIR__ . '/../src',
'Doctrine' => __DIR__ . '/../vendor/Doctrine/lib' 'Doctrine' => __DIR__ . '/../vendor/Doctrine/lib',
'Monolog' => __DIR__ . '/../vendor/Monolog/src'
)); ));
$classLoader->registerPrefix('Zend_', __DIR__ . '/../vendor'); $classLoader->registerPrefix('Zend_', __DIR__ . '/../vendor');

1
vendor/Monolog vendored Submodule

@ -0,0 +1 @@
Subproject commit ea466fa84863ef15e5dd178d14a804a1efbfd07a