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

Allow the user to define a custom mode when fopen-ing a log file (#1913)

* Allow the user to define a custom mode when `fopen`-ing a log file

* Undo unnecessary changes

---------

Co-authored-by: Jordi Boggiano <j.boggiano@seld.be>
This commit is contained in:
Pedro Peixoto
2024-11-11 06:26:15 -03:00
committed by GitHub
parent 23560e32b1
commit 8ae546b2b9

View File

@@ -41,6 +41,8 @@ class StreamHandler extends AbstractProcessingHandler
protected $filePermission; protected $filePermission;
/** @var bool */ /** @var bool */
protected $useLocking; protected $useLocking;
/** @var string */
protected $fileOpenMode;
/** @var true|null */ /** @var true|null */
private $dirCreated = null; private $dirCreated = null;
@@ -48,10 +50,11 @@ class StreamHandler extends AbstractProcessingHandler
* @param resource|string $stream If a missing path can't be created, an UnexpectedValueException will be thrown on first write * @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 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 bool $useLocking Try to lock log file before doing any writes
* @param string $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 * @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 = 'a')
{ {
parent::__construct($level, $bubble); parent::__construct($level, $bubble);
@@ -78,6 +81,7 @@ class StreamHandler extends AbstractProcessingHandler
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.');
} }
$this->fileOpenMode = $fileOpenMode;
$this->filePermission = $filePermission; $this->filePermission = $filePermission;
$this->useLocking = $useLocking; $this->useLocking = $useLocking;
} }
@@ -138,7 +142,7 @@ class StreamHandler extends AbstractProcessingHandler
return $this->customErrorHandler(...$args); return $this->customErrorHandler(...$args);
}); });
try { try {
$stream = fopen($url, 'a'); $stream = fopen($url, $this->fileOpenMode);
if ($this->filePermission !== null) { if ($this->filePermission !== null) {
@chmod($url, $this->filePermission); @chmod($url, $this->filePermission);
} }