|
|
@@ -35,6 +35,7 @@ class StreamHandler extends AbstractProcessingHandler
|
|
|
* @param int|null $filePermission Optional file permissions (default (0644) are only for owner read/write)
|
|
|
* @param Boolean $useLocking Try to lock log file before doing any writes
|
|
|
*
|
|
|
+ * @throws \Exception If a missing directory is not buildable
|
|
|
* @throws \InvalidArgumentException If stream is not a resource or string
|
|
|
*/
|
|
|
public function __construct($stream, $level = Logger::DEBUG, $bubble = true, $filePermission = null, $useLocking = false)
|
|
|
@@ -43,6 +44,16 @@ class StreamHandler extends AbstractProcessingHandler
|
|
|
if (is_resource($stream)) {
|
|
|
$this->stream = $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;
|
|
|
} else {
|
|
|
throw new \InvalidArgumentException('A stream must either be a resource or a string.');
|
|
|
@@ -99,6 +110,25 @@ class StreamHandler extends AbstractProcessingHandler
|
|
|
|
|
|
private function customErrorHandler($code, $msg)
|
|
|
{
|
|
|
- $this->errorMessage = preg_replace('{^fopen\(.*?\): }', '', $msg);
|
|
|
+ $this->errorMessage = preg_replace('{^(fopen|mkdir)\(.*?\): }', '', $msg);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param string $stream
|
|
|
+ *
|
|
|
+ * @return null|string
|
|
|
+ */
|
|
|
+ private function getDirFromStream($stream)
|
|
|
+ {
|
|
|
+ $pos = strpos($stream, '://');
|
|
|
+ if ($pos === false) {
|
|
|
+ return dirname($stream);
|
|
|
+ }
|
|
|
+
|
|
|
+ if ('file://' === substr($stream, 0, 7)) {
|
|
|
+ return dirname(substr($stream, 7));
|
|
|
+ }
|
|
|
+
|
|
|
+ return;
|
|
|
}
|
|
|
}
|