Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add filename paramenter log #4147

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 29 additions & 2 deletions lib/internal/Magento/Framework/Logger/Handler/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,50 @@ class Base extends StreamHandler
/**
* @param DriverInterface $filesystem
* @param string $filePath
* @param string $fileName
*/
public function __construct(
DriverInterface $filesystem,
$filePath = null
$filePath = null,
$fileName = null
) {
$this->filesystem = $filesystem;
if (!empty($fileName)) {
$this->fileName = $this->sanitizeFileName($fileName);
}
parent::__construct(
$filePath ? $filePath . $this->fileName : BP . $this->fileName,
$filePath ? $filePath . $this->fileName : BP . DIRECTORY_SEPARATOR . $this->fileName,
$this->loggerType
);

$this->setFormatter(new LineFormatter(null, null, true));
}

/**
* @param string $fileName
*
* @return string
* @throws \InvalidArgumentException
*/
protected function sanitizeFileName($fileName)
{
if (!is_string($fileName)) {
throw new \InvalidArgumentException('Filename expected to be a string');
}

$parts = explode('/', $fileName);
$parts = array_filter($parts, function ($value) {
return !in_array($value, ['', '.', '..']);
});

return implode('/', $parts);
}

/**
* @{inheritDoc}
*
* @param $record array
*
* @return void
*/
public function write(array $record)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php
/**
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Framework\Logger\Test\Unit\Handler;

class BaseTest extends \PHPUnit_Framework_TestCase
{
/** @var \Magento\Framework\Logger\Handler\Base */
protected $_model;

protected $_sanitizeMethod;

public function setUp()
{
$this->_model = new \Magento\Framework\Logger\Handler\Base(
$this->getMock('Magento\Framework\Filesystem\DriverInterface')
);

$class = new \ReflectionClass($this->_model);
$this->_sanitizeMethod = $class->getMethod('sanitizeFileName');
$this->_sanitizeMethod->setAccessible(true);
}

public function testSanitizeEmpty()
{
$this->assertEquals('', $this->_sanitizeMethod->invokeArgs($this->_model, ['']));
}

public function testSanitizeSimpleFilename()
{
$this->assertEquals('custom.log', $this->_sanitizeMethod->invokeArgs($this->_model, ['custom.log']));
}

public function testSanitizeLeadingSlashFilename()
{
$this->assertEquals(
'customfolder/custom.log',
$this->_sanitizeMethod->invokeArgs($this->_model, ['/customfolder/custom.log'])
);
}

public function testSanitizeParentLevelFolder()
{
$this->assertEquals(
'var/hack/custom.log',
$this->_sanitizeMethod->invokeArgs($this->_model, ['../../../var/hack/custom.log'])
);
}

/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage Filename expected to be a string
*/
public function testSanitizeFileException()
{
$this->_sanitizeMethod->invokeArgs($this->_model, [['filename' => 'notValid']]);
}
}