mirror of
https://github.com/guzzle/guzzle.git
synced 2025-01-18 05:48:15 +01: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:
parent
4c2405ffed
commit
782b323893
3
.gitmodules
vendored
3
.gitmodules
vendored
@ -4,3 +4,6 @@
|
||||
[submodule "vendor/Doctrine"]
|
||||
path = vendor/Doctrine
|
||||
url = https://github.com/doctrine/common
|
||||
[submodule "vendor/Monolog"]
|
||||
path = vendor/Monolog
|
||||
url = https://github.com/Seldaek/monolog.git
|
||||
|
45
src/Guzzle/Common/Log/MonologLogAdapter.php
Normal file
45
src/Guzzle/Common/Log/MonologLogAdapter.php
Normal 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;
|
||||
}
|
||||
}
|
@ -33,7 +33,7 @@ class ZendLogAdapter extends AbstractLogAdapter
|
||||
/**
|
||||
* {@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);
|
||||
|
||||
|
47
tests/Guzzle/Tests/Common/Log/MonologLogAdapterTest.php
Normal file
47
tests/Guzzle/Tests/Common/Log/MonologLogAdapterTest.php
Normal 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());
|
||||
}
|
||||
}
|
@ -37,27 +37,12 @@ class ZendLogAdapterTest extends \Guzzle\Tests\GuzzleTestCase
|
||||
$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
|
||||
* @expectedException InvalidArgumentException
|
||||
*/
|
||||
public function testConstruct()
|
||||
public function testEnforcesType()
|
||||
{
|
||||
$this->zfSkip();
|
||||
|
||||
// A successful construction
|
||||
$this->adapter = new ZendLogAdapter($this->log, new Collection());
|
||||
|
||||
@ -71,8 +56,6 @@ class ZendLogAdapterTest extends \Guzzle\Tests\GuzzleTestCase
|
||||
*/
|
||||
public function testLogsMessagesToAdaptedObject()
|
||||
{
|
||||
$this->zfSkip();
|
||||
|
||||
// Test without a priority
|
||||
$this->adapter->log('test', \LOG_NOTICE, 'guzzle.common.log.adapter.zend_log_adapter', 'localhost');
|
||||
$this->assertEquals(1, substr_count(ob_get_contents(), 'test'));
|
||||
@ -87,8 +70,6 @@ class ZendLogAdapterTest extends \Guzzle\Tests\GuzzleTestCase
|
||||
*/
|
||||
public function testExposesAdaptedLogObject()
|
||||
{
|
||||
$this->zfSkip();
|
||||
|
||||
$this->assertEquals($this->log, $this->adapter->getLogObject());
|
||||
}
|
||||
}
|
@ -17,7 +17,8 @@ $classLoader = new \Symfony\Component\ClassLoader\UniversalClassLoader();
|
||||
$classLoader->registerNamespaces(array(
|
||||
'Guzzle\Tests' => __DIR__,
|
||||
'Guzzle' => __DIR__ . '/../src',
|
||||
'Doctrine' => __DIR__ . '/../vendor/Doctrine/lib'
|
||||
'Doctrine' => __DIR__ . '/../vendor/Doctrine/lib',
|
||||
'Monolog' => __DIR__ . '/../vendor/Monolog/src'
|
||||
));
|
||||
|
||||
$classLoader->registerPrefix('Zend_', __DIR__ . '/../vendor');
|
||||
|
1
vendor/Monolog
vendored
Submodule
1
vendor/Monolog
vendored
Submodule
@ -0,0 +1 @@
|
||||
Subproject commit ea466fa84863ef15e5dd178d14a804a1efbfd07a
|
Loading…
x
Reference in New Issue
Block a user