diff --git a/README.md b/README.md index 904cea3..ce1828c 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,3 @@ -# XXX +# PhpExtendedMonolog -XXX \ No newline at end of file +Some extensions for [Monolog](https://github.com/Seldaek/monolog) \ No newline at end of file diff --git a/composer.json b/composer.json index 982738d..1c5cde9 100644 --- a/composer.json +++ b/composer.json @@ -1,21 +1,22 @@ { - "name": "jrosset/xxx", - "description": "XXX", + "name": "jrosset/extendedmonolog", + "description": "Some extensions for Monolog", "keywords": [ ], "minimum-stability": "stable", "require": { - "php": "^7.4 || ^8.0" + "php": "^7.4 || ^8.0", + "jrosset/exceptionhelper": "^1.0", + "jrosset/lasterrorexception": "^1.0" }, "autoload": { - "psr-4": { + "psr-4": { "jrosset\\": "src/" - }, - "exclude-from-classmap": [ "tests/" ] + } }, "readme": "README.md", - "homepage": "https://git.jrosset.ovh/jrosset/XXX", + "homepage": "https://git.jrosset.ovh/jrosset/PhpExtendedMonolog", "license": "CC-BY-4.0", "authors": [ { @@ -25,9 +26,9 @@ ], "support": { "email": "jul.rosset@gmail.com", - "issues": "https://git.jrosset.ovh/jrosset/XXX/issues", - "wiki": "https://git.jrosset.ovh/jrosset/XXX/wiki", - "docs": "https://git.jrosset.ovh/jrosset/XXX/wiki", - "source": "https://git.jrosset.ovh/jrosset/XXX" + "issues": "https://git.jrosset.ovh/jrosset/PhpExtendedMonolog/issues", + "wiki": "https://git.jrosset.ovh/jrosset/PhpExtendedMonolog/wiki", + "docs": "https://git.jrosset.ovh/jrosset/PhpExtendedMonolog/wiki", + "source": "https://git.jrosset.ovh/jrosset/PhpExtendedMonolog" } -} \ No newline at end of file +} diff --git a/src/ExtendedMonolog/ExceptionLogger.php b/src/ExtendedMonolog/ExceptionLogger.php new file mode 100644 index 0000000..4bee884 --- /dev/null +++ b/src/ExtendedMonolog/ExceptionLogger.php @@ -0,0 +1,28 @@ +addRecord( + $level, + ExceptionHelper::toString($exception, false), + array_merge( + [ + 'exception' => $exception, + ], + $context + ) + ); + } +} \ No newline at end of file diff --git a/src/ExtendedMonolog/ExceptionLoggerInterface.php b/src/ExtendedMonolog/ExceptionLoggerInterface.php new file mode 100644 index 0000000..5ca7805 --- /dev/null +++ b/src/ExtendedMonolog/ExceptionLoggerInterface.php @@ -0,0 +1,20 @@ +normalizeDirectory($dirPath, $historyNumberOfDays); + + parent::__construct( + $dirPath . date('Y-m-d') . '.log', + Logger::WARNING + ); + $this->setFormatter(new LogFileFormatter()); + } + + /** + * 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) + * + * @return void + * + * @throws Throwable If an error occurs + */ + private function normalizeDirectory (string $dirPath, int $historyNumberOfDays = 30): void { + if (!file_exists($dirPath) || !is_dir($dirPath)) { + error_clear_last(); + if (!mkdir($dirPath, 0755, true)) { + throw new RuntimeException('Failed to create log directory: ' . $dirPath, 0, LastErrorException::createFromLastError()); + } + } + + $dateLimit = (new DateTimeImmutable('now'))->sub(new DateInterval('P' . $historyNumberOfDays . 'D')); + + $iterator = new DirectoryIterator($dirPath); + foreach ($iterator as $fileInfo) { + if ($fileInfo->isDot() || mb_substr($fileInfo->getFilename(), 0, 1) === '.') { + continue; + } + if (!$fileInfo->isFile()) { + continue; + } + if (preg_match('/^(?\\d{4}-\\d{2}-\\d{2})(?:[_-].*)?\.log$/i', $fileInfo->getFilename(), $match) !== 1) { + continue; + } + + $date = DateTimeImmutable::createFromFormat('Y-m-d', $match['date']); + if ($date >= $dateLimit) { + continue; + } + + unlink($fileInfo->getPathname()); + } + } +} \ No newline at end of file diff --git a/src/ExtendedMonolog/LogFileFormatter.php b/src/ExtendedMonolog/LogFileFormatter.php new file mode 100644 index 0000000..23d2a69 --- /dev/null +++ b/src/ExtendedMonolog/LogFileFormatter.php @@ -0,0 +1,30 @@ +