Skip to content

Commit

Permalink
MAGETWO-52767: [Github] Not possible to disable DEBUG logging #4362
Browse files Browse the repository at this point in the history
  • Loading branch information
shiftedreality committed Jun 13, 2016
1 parent e7d6678 commit 73c9f1a
Show file tree
Hide file tree
Showing 4 changed files with 206 additions and 0 deletions.
1 change: 1 addition & 0 deletions app/code/Magento/Backend/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -214,4 +214,5 @@
</argument>
</arguments>
</type>
<preference for="Magento\Framework\Logger\Handler\Debug" type="Magento\Developer\Model\Logger\Handler\Debug"/>
</config>
71 changes: 71 additions & 0 deletions app/code/Magento/Developer/Model/Logger/Handler/Debug.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php
/**
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Developer\Model\Logger\Handler;

use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\State;
use Magento\Framework\Filesystem\DriverInterface;
use Magento\Store\Model\ScopeInterface;
use Magento\Store\Model\StoreManagerInterface;

/**
* Class Debug
*/
class Debug extends \Magento\Framework\Logger\Handler\Debug
{
/**
* @var State
*/
private $state;

/**
* @var ScopeConfigInterface
*/
private $scopeConfig;

/**
* @var StoreManagerInterface
*/
private $storeManager;

/**
* @param DriverInterface $filesystem
* @param State $state
* @param ScopeConfigInterface $scopeConfig
* @param StoreManagerInterface $storeManager
* @param string $filePath
*/
public function __construct(
DriverInterface $filesystem,
State $state,
ScopeConfigInterface $scopeConfig,
StoreManagerInterface $storeManager,
$filePath = null
) {
parent::__construct($filesystem, $filePath);

$this->state = $state;
$this->scopeConfig = $scopeConfig;
$this->storeManager = $storeManager;
}

/**
* {@inheritdoc}
*/
public function write(array $record)
{
$storeCode = $this->storeManager->getStore()->getCode();

if (
$this->state->getMode() === State::MODE_PRODUCTION
|| !$this->scopeConfig->getValue('debug_logging', ScopeInterface::SCOPE_STORE, $storeCode)
) {
return null;
}

parent::write($record);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
<?php
/**
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Developer\Test\Unit\Model\Logger\Handler;

use Magento\Developer\Model\Logger\Handler\Debug;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\State;
use Magento\Framework\Filesystem\DriverInterface;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
use Magento\Store\Api\Data\StoreInterface;
use Magento\Store\Model\ScopeInterface;
use Magento\Store\Model\StoreManagerInterface;

/**
* Class DebugTest
*/
class DebugTest extends \PHPUnit_Framework_TestCase
{
/**
* @var Debug
*/
private $model;

/**
* @var DriverInterface|\PHPUnit_Framework_MockObject_MockObject
*/
private $filesystemMock;

/**
* @var State|\PHPUnit_Framework_MockObject_MockObject
*/
private $stateMock;

/**
* @var ScopeConfigInterface|\PHPUnit_Framework_MockObject_MockObject
*/
private $scopeConfigMock;

/**
* @var StoreManagerInterface|\PHPUnit_Framework_MockObject_MockObject
*/
private $storeManagerMock;

/**
* @var StoreInterface|\PHPUnit_Framework_MockObject_MockObject
*/
private $storeMock;

protected function setUp()
{
$this->filesystemMock = $this->getMockBuilder(DriverInterface::class)
->getMockForAbstractClass();
$this->stateMock = $this->getMockBuilder(State::class)
->disableOriginalConstructor()
->getMock();
$this->scopeConfigMock = $this->getMockBuilder(ScopeConfigInterface::class)
->getMockForAbstractClass();
$this->storeMock = $this->getMockBuilder(StoreInterface::class)
->getMockForAbstractClass();
$this->storeManagerMock = $this->getMockBuilder(StoreManagerInterface::class)
->getMockForAbstractClass();

$this->storeManagerMock->expects($this->any())
->method('getStore')
->willReturn($this->storeMock);

$this->model = (new ObjectManager($this))->getObject(Debug::class, [
'filesystem' => $this->filesystemMock,
'state' => $this->stateMock,
'scopeConfig' => $this->scopeConfigMock,
'storeManager' => $this->storeManagerMock
]);
}

public function testWrite()
{
$this->storeMock->expects($this->once())
->method('getCode')
->willReturn('test_code');
$this->stateMock->expects($this->once())
->method('getMode')
->willReturn(State::MODE_DEVELOPER);
$this->scopeConfigMock->expects($this->once())
->method('getValue')
->with('debug_logging', ScopeInterface::SCOPE_STORE, 'test_code')
->willReturn(true);

$this->model->write(['formatted' => false]);
}

public function testWriteWithDisabledByProduction()
{
$this->storeMock->expects($this->once())
->method('getCode')
->willReturn('test_code');
$this->stateMock->expects($this->once())
->method('getMode')
->willReturn(State::MODE_PRODUCTION);
$this->scopeConfigMock->expects($this->never())
->method('getValue');

$this->assertNull(
$this->model->write(['formatted' => false])
);
}

public function testWriteDisabledByConfig()
{
$this->storeMock->expects($this->once())
->method('getCode')
->willReturn('test_code');
$this->stateMock->expects($this->once())
->method('getMode')
->willReturn(State::MODE_DEVELOPER);
$this->scopeConfigMock->expects($this->once())
->method('getValue')
->with('debug_logging', ScopeInterface::SCOPE_STORE, 'test_code')
->willReturn(false);

$this->assertNull(
$this->model->write(['formatted' => false])
);
}
}
7 changes: 7 additions & 0 deletions app/code/Magento/Developer/etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@
<backend_model>Magento\Developer\Model\Config\Backend\AllowedIps</backend_model>
</field>
</group>
<group id="debug" translate="label" type="text" sortOrder="30" showInDefault="1" showInWebsite="1" showInStore="1">
<field id="debug_logging" translate="label comment" type="select" sortOrder="30" showInDefault="1" showInWebsite="0" showInStore="0">
<label>Log to File</label>
<comment>Not available in production mode.</comment>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
</field>
</group>
</section>
</system>
</config>

0 comments on commit 73c9f1a

Please sign in to comment.