Skip to content

Commit

Permalink
Added DevNull Logger (#68)
Browse files Browse the repository at this point in the history
  • Loading branch information
surikman authored and Florian Preusner committed Nov 2, 2016
1 parent 146855f commit 9df9034
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 7 deletions.
2 changes: 1 addition & 1 deletion DataCollector/HttpDataCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace EightPoints\Bundle\GuzzleBundle\DataCollector;

use EightPoints\Bundle\GuzzleBundle\Log\LogGroup;
use Psr\Log\LoggerInterface;
use EightPoints\Bundle\GuzzleBundle\Log\LoggerInterface;
use Symfony\Component\HttpKernel\DataCollector\DataCollector;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
Expand Down
11 changes: 8 additions & 3 deletions DependencyInjection/GuzzleExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function load(array $configs, ContainerBuilder $container)
$configuration = new Configuration($this->getAlias(), $container->getParameter('kernel.debug'));
$config = $processor->processConfiguration($configuration, $configs);

$this->createLogger($container);
$this->createLogger($config, $container);

foreach ($config['clients'] as $name => $options) {

Expand Down Expand Up @@ -149,16 +149,21 @@ protected function createHandler(ContainerBuilder $container, $name, array $conf
*
* @since 2015-07
*
* @param array $config
* @param ContainerBuilder $container
*
* @return Definition
*
* @throws \Symfony\Component\DependencyInjection\Exception\BadMethodCallException
*/
protected function createLogger(ContainerBuilder $container)
protected function createLogger(array $config, ContainerBuilder $container)
{

$logger = new Definition('%guzzle_bundle.logger.class%');
if ($config['logging'] === true) {
$logger = new Definition('%guzzle_bundle.logger.class%');
} else {
$logger = new Definition('EightPoints\Bundle\GuzzleBundle\Log\DevNullLogger');
}

$container->setDefinition('guzzle_bundle.logger', $logger);

Expand Down
56 changes: 56 additions & 0 deletions Log/DevNullLogger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace EightPoints\Bundle\GuzzleBundle\Log;

use Psr\Log\LoggerTrait;

/**
* Class DevNullLogger
*
* @author SuRiKmAn <[email protected]>
* @version 5.0
* @since 2016-11
* @package EightPoints\Bundle\GuzzleBundle\Log
*/
class DevNullLogger implements LoggerInterface
{
use LoggerTrait;

/**
* @inheritDoc
*/
public function log($level, $message, array $context = [])
{
// do nothing!!
}

/**
* Clear messages list
*
* @return void
*/
public function clear()
{
// do nothing!!
}

/**
* Return if messages exist or not
*
* @return boolean
*/
public function hasMessages()
{
return false;
}

/**
* Return log messages
*
* @return array
*/
public function getMessages()
{
return [];
}
}
1 change: 0 additions & 1 deletion Log/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace EightPoints\Bundle\GuzzleBundle\Log;

use Psr\Log\LoggerTrait;
use Psr\Log\LoggerInterface;

/**
* @version 2.1
Expand Down
36 changes: 36 additions & 0 deletions Log/LoggerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace EightPoints\Bundle\GuzzleBundle\Log;

use Psr\Log;

/**
* Interface LoggerInterface
*
* @author SuRiKmAn <[email protected]>
* @package EightPoints\Bundle\GuzzleBundle\Log
*/
interface LoggerInterface extends Log\LoggerInterface
{

/**
* Clear messages list
*
* @return void
*/
public function clear();

/**
* Return if messages exist or not
*
* @return boolean
*/
public function hasMessages();

/**
* Return log messages
*
* @return array
*/
public function getMessages();
}
2 changes: 1 addition & 1 deletion Middleware/LogMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\MessageFormatter;
use Psr\Log\LoggerInterface;
use EightPoints\Bundle\GuzzleBundle\Log\LoggerInterface;

/**
* @version 3.0
Expand Down
2 changes: 1 addition & 1 deletion Tests/Log/LoggerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class LoggerTest extends \PHPUnit_Framework_TestCase
*/
public function testConstruct()
{
$this->assertInstanceOf('Psr\Log\LoggerInterface', new Logger());
$this->assertInstanceOf('EightPoints\Bundle\GuzzleBundle\Log\LoggerInterface', new Logger());
}

/**
Expand Down

0 comments on commit 9df9034

Please sign in to comment.