diff --git a/composer.json b/composer.json index 8f09fca..5091c76 100644 --- a/composer.json +++ b/composer.json @@ -16,10 +16,10 @@ "minimum-stability": "stable", "require": { "php": "^7.4 || ^8.0", - "ext-mbstring": "*", - "jrosset/exceptionhelper": "^1.0", - "jrosset/lasterrorexception": "^1.0", - "monolog/monolog": "^2.9" + "ext-mbstring": "*", + "jrosset/exceptionhelper": "^1.1", + "jrosset/lasterrorexception": "^1.1", + "monolog/monolog": "^2.10" }, "autoload": { "psr-4": { diff --git a/src/ExtendedMonolog/LogDirectoryHandler.php b/src/ExtendedMonolog/LogDirectoryHandler.php index 30edb78..5dfe96c 100644 --- a/src/ExtendedMonolog/LogDirectoryHandler.php +++ b/src/ExtendedMonolog/LogDirectoryHandler.php @@ -20,28 +20,30 @@ class LogDirectoryHandler extends StreamHandler { /** * Initialization * - * @param string $dirPath The log directory path - * @param int|string $level The minimum logging level at which this handler will be triggered - * @param int $historyNumberOfDays The maximum number of history files to keep (in number of days) - * @param bool $bubble Whether the messages that are handled can bubble up the stack or not - * @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 string $dirPath The log directory path + * @param int|string $level The minimum logging level at which this handler will be triggered + * @param int $historyNumberOfDays The maximum number of history files to keep (in number of days) + * @param bool $bubble Whether the messages that are handled can bubble up the stack or not + * @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 string|null $filenamePrefix The filename prefix * * @throws Throwable If an error occurs */ public function __construct ( - string $dirPath, - $level = Logger::DEBUG, - int $historyNumberOfDays = 30, - bool $bubble = true, - ?int $filePermission = null, - bool $useLocking = false + string $dirPath, + $level = Logger::DEBUG, + int $historyNumberOfDays = 30, + bool $bubble = true, + ?int $filePermission = null, + bool $useLocking = false, + ?string $filenamePrefix = null ) { $dirPath = $dirPath . DIRECTORY_SEPARATOR; - $this->normalizeDirectory($dirPath, $historyNumberOfDays); + $this->normalizeDirectory($dirPath, $filenamePrefix, $historyNumberOfDays); parent::__construct( - $dirPath . date('Y-m-d') . '.log', + $dirPath . ($filenamePrefix ?? '') . date('Y-m-d') . '.log', $level, $bubble, $filePermission, @@ -53,14 +55,15 @@ class LogDirectoryHandler extends StreamHandler { /** * Normalize a directory: create if missing and clear old files * - * @param string $dirPath The directory path - * @param int $historyNumberOfDays The maximum number of history files to keep (in number of days) + * @param string $dirPath The directory path + * @param string|null $filenamePrefix The filename prefix + * @param int $historyNumberOfDays The maximum number of history files to keep (in number of days) * * @return void * * @throws Throwable If an error occurs */ - private function normalizeDirectory (string $dirPath, int $historyNumberOfDays = 30): void { + protected final function normalizeDirectory (string $dirPath, ?string $filenamePrefix, int $historyNumberOfDays = 30): void { if (!file_exists($dirPath) || !is_dir($dirPath)) { error_clear_last(); if (!mkdir($dirPath, 0755, true)) { @@ -78,7 +81,13 @@ class LogDirectoryHandler extends StreamHandler { if (!$fileInfo->isFile()) { continue; } - if (preg_match('/^(?\\d{4}-\\d{2}-\\d{2})(?:[_-].*)?\.log$/i', $fileInfo->getFilename(), $match) !== 1) { + if ( + preg_match( + '/^' . preg_quote($filenamePrefix ?? '', '/') . '(?\\d{4}-\\d{2}-\\d{2})(?:[_-].*)?\.log$/i', + $fileInfo->getFilename(), + $match + ) !== 1 + ) { continue; }