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

Merge pull request #607 from APMG/master

Create directory on initial log write rather than class instantiation.
This commit is contained in:
Jordi Boggiano
2015-07-20 19:17:36 +01:00

View File

@@ -27,6 +27,7 @@ class StreamHandler extends AbstractProcessingHandler
private $errorMessage; private $errorMessage;
protected $filePermission; protected $filePermission;
protected $useLocking; protected $useLocking;
private $dirCreated;
/** /**
* @param resource|string $stream * @param resource|string $stream
@@ -44,16 +45,6 @@ class StreamHandler extends AbstractProcessingHandler
if (is_resource($stream)) { if (is_resource($stream)) {
$this->stream = $stream; $this->stream = $stream;
} elseif (is_string($stream)) { } elseif (is_string($stream)) {
$dir = $this->getDirFromStream($stream);
if (null !== $dir && !is_dir($dir)) {
$this->errorMessage = null;
set_error_handler(array($this, 'customErrorHandler'));
$status = mkdir($dir, 0777, true);
restore_error_handler();
if (false === $status) {
throw new \UnexpectedValueException(sprintf('There is no existing directory at "%s" and its not buildable: '.$this->errorMessage, $dir));
}
}
$this->url = $stream; $this->url = $stream;
} else { } else {
throw new \InvalidArgumentException('A stream must either be a resource or a string.'); throw new \InvalidArgumentException('A stream must either be a resource or a string.');
@@ -83,6 +74,7 @@ class StreamHandler extends AbstractProcessingHandler
if (!$this->url) { if (!$this->url) {
throw new \LogicException('Missing stream url, the stream can not be opened. This may be caused by a premature call to close().'); throw new \LogicException('Missing stream url, the stream can not be opened. This may be caused by a premature call to close().');
} }
$this->createDir();
$this->errorMessage = null; $this->errorMessage = null;
set_error_handler(array($this, 'customErrorHandler')); set_error_handler(array($this, 'customErrorHandler'));
$this->stream = fopen($this->url, 'a'); $this->stream = fopen($this->url, 'a');
@@ -131,4 +123,22 @@ class StreamHandler extends AbstractProcessingHandler
return; return;
} }
private function createDir()
{
// Do not try to create dir if it has already been tried.
if ($this->dirCreated) return;
$dir = $this->getDirFromStream($this->url);
if (null !== $dir && !is_dir($dir)) {
$this->errorMessage = null;
set_error_handler(array($this, 'customErrorHandler'));
$status = mkdir($dir, 0777, true);
restore_error_handler();
if (false === $status) {
throw new \UnexpectedValueException(sprintf('There is no existing directory at "%s" and its not buildable: '.$this->errorMessage, $dir));
}
}
$this->dirCreated = true;
}
} }