Skip to content

Commit

Permalink
Allow the user to define a custom mode when fopen-ing a log file
Browse files Browse the repository at this point in the history
  • Loading branch information
phrfpeixoto committed Sep 17, 2024
1 parent 23560e3 commit 935359a
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions src/Monolog/Handler/StreamHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,20 @@ class StreamHandler extends AbstractProcessingHandler
protected $filePermission;
/** @var bool */
protected $useLocking;
/** @var string */
protected $fileOpenMode = 'a';
/** @var true|null */
private $dirCreated = null;

/**
* @param resource|string $stream If a missing path can't be created, an UnexpectedValueException will be thrown on first write
* @param int|null $filePermission Optional file permissions (default (0644) are only for owner read/write)
* @param bool $useLocking Try to lock log file before doing any writes
* @param null $fileOpenMode The fopen() mode used when opening a file, if $stream is a file path
*
* @throws \InvalidArgumentException If stream is not a resource or string
*/
public function __construct($stream, $level = Logger::DEBUG, bool $bubble = true, ?int $filePermission = null, bool $useLocking = false)
public function __construct($stream, $level = Logger::DEBUG, bool $bubble = true, ?int $filePermission = null, bool $useLocking = false, $fileOpenMode = null)
{
parent::__construct($level, $bubble);

Expand All @@ -78,6 +81,11 @@ public function __construct($stream, $level = Logger::DEBUG, bool $bubble = true
throw new \InvalidArgumentException('A stream must either be a resource or a string.');
}

$this->fileOpenMode = $fileOpenMode ?? $this->fileOpenMode;
if(!$this->fileOpenMode){
throw new \InvalidArgumentException("Please define a valid file open mode, as defined by PHP's fopen()");
}

$this->filePermission = $filePermission;
$this->useLocking = $useLocking;
}
Expand Down Expand Up @@ -138,7 +146,7 @@ protected function write(array $record): void
return $this->customErrorHandler(...$args);
});
try {
$stream = fopen($url, 'a');
$stream = fopen($url, $this->fileOpenMode);
if ($this->filePermission !== null) {
@chmod($url, $this->filePermission);
}
Expand Down Expand Up @@ -183,14 +191,14 @@ protected function streamWrite($stream, array $record): void
fwrite($stream, (string) $record['formatted']);
}

private function customErrorHandler(int $code, string $msg): bool
protected final function customErrorHandler(int $code, string $msg): bool
{
$this->errorMessage = preg_replace('{^(fopen|mkdir)\(.*?\): }', '', $msg);

return true;
}

private function getDirFromStream(string $stream): ?string
protected final function getDirFromStream(string $stream): ?string
{
$pos = strpos($stream, '://');
if ($pos === false) {
Expand All @@ -204,7 +212,7 @@ private function getDirFromStream(string $stream): ?string
return null;
}

private function createDir(string $url): void
protected final function createDir(string $url): void
{
// Do not try to create dir if it has already been tried.
if ($this->dirCreated) {
Expand Down

0 comments on commit 935359a

Please sign in to comment.