1
0
mirror of https://github.com/Seldaek/monolog.git synced 2025-08-12 08:04:02 +02:00

Canonicalize paths when constructing RotatingFileHandler/StreamHandler, fixes #1326

This commit is contained in:
Jordi Boggiano
2020-05-21 16:54:57 +02:00
parent acd3173c4a
commit c50950d79e
4 changed files with 57 additions and 2 deletions

View File

@@ -12,6 +12,7 @@
namespace Monolog\Handler;
use Monolog\Logger;
use Monolog\Utils;
/**
* Stores logs to files that are rotated every day and a limited number of files are kept.
@@ -45,7 +46,7 @@ class RotatingFileHandler extends StreamHandler
*/
public function __construct($filename, $maxFiles = 0, $level = Logger::DEBUG, $bubble = true, $filePermission = null, $useLocking = false)
{
$this->filename = $filename;
$this->filename = Utils::canonicalizePath($filename);
$this->maxFiles = (int) $maxFiles;
$this->nextRotation = new \DateTime('tomorrow');
$this->filenameFormat = '{filename}-{date}';

View File

@@ -12,6 +12,7 @@
namespace Monolog\Handler;
use Monolog\Logger;
use Monolog\Utils;
/**
* Stores to any stream resource
@@ -45,7 +46,7 @@ class StreamHandler extends AbstractProcessingHandler
if (is_resource($stream)) {
$this->stream = $stream;
} elseif (is_string($stream)) {
$this->url = $stream;
$this->url = Utils::canonicalizePath($stream);
} else {
throw new \InvalidArgumentException('A stream must either be a resource or a string.');
}

View File

@@ -23,6 +23,36 @@ class Utils
return 'c' === $class[0] && 0 === strpos($class, "class@anonymous\0") ? get_parent_class($class).'@anonymous' : $class;
}
/**
* Makes sure if a relative path is passed in it is turned into an absolute path
*
* @param string $streamUrl stream URL or path without protocol
*
* @return string
*/
public static function canonicalizePath($streamUrl)
{
$prefix = '';
if ('file://' === substr($streamUrl, 0, 7)) {
$streamUrl = substr($streamUrl, 7);
$prefix = 'file://';
}
// other type of stream, not supported
if (false !== strpos($streamUrl, '://')) {
return $streamUrl;
}
// already absolute
if (substr($streamUrl, 0, 1) === '/' || substr($streamUrl, 1, 1) === ':' || substr($streamUrl, 0, 2) === '\\\\') {
return $prefix.$streamUrl;
}
$streamUrl = getcwd() . '/' . $streamUrl;
return $prefix.$streamUrl;
}
/**
* Return the JSON representation of a value
*

View File

@@ -13,6 +13,29 @@ namespace Monolog;
class UtilsTest extends \PHPUnit_Framework_TestCase
{
/**
* @param string $expected
* @param string $input
* @dataProvider providePathsToCanonicalize
*/
public function testCanonicalizePath($expected, $input)
{
$this->assertSame($expected, Utils::canonicalizePath($input));
}
public function providePathsToCanonicalize()
{
return array(
array('/foo/bar', '/foo/bar'),
array('file://'.getcwd().'/bla', 'file://bla'),
array(getcwd().'/bla', 'bla'),
array(getcwd().'/./bla', './bla'),
array('file:///foo/bar', 'file:///foo/bar'),
array('any://foo', 'any://foo'),
array('\\\\network\path', '\\\\network\path'),
);
}
/**
* @param int $code
* @param string $msg