Compare commits

...

2 Commits
master ... 1.x

@ -16,9 +16,10 @@
"minimum-stability": "stable",
"require": {
"php": "^7.4 || ^8.0",
"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": {

@ -1,4 +1,5 @@
<?php
/** @noinspection PhpDocFinalChecksInspection */
namespace jrosset\ExtendedMonolog;
@ -16,7 +17,7 @@ class ExceptionLogger extends Logger implements ExceptionLoggerInterface {
public function exception (int $level, Throwable $exception, array $context = []): void {
$this->addRecord(
$level,
ExceptionHelper::toString($exception, false),
ExceptionHelper::toString($exception, false) . PHP_EOL,
array_merge(
[
'exception' => $exception,

@ -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('/^(?<date>\\d{4}-\\d{2}-\\d{2})(?:[_-].*)?\.log$/i', $fileInfo->getFilename(), $match) !== 1) {
if (
preg_match(
'/^' . preg_quote($filenamePrefix ?? '', '/') . '(?<date>\\d{4}-\\d{2}-\\d{2})(?:[_-].*)?\.log$/i',
$fileInfo->getFilename(),
$match
) !== 1
) {
continue;
}

@ -25,6 +25,30 @@ class LogFileFormatter extends LineFormatter {
* @inheritDoc
*/
public function format (array $record): string {
return preg_replace('/((?<!\\\\)\\\\[rn])+$/', PHP_EOL, parent::format($record));
//region Get then remove newlines at record message end (*before* the formatting)
$messageEndNewlines = '';
$record['message'] = preg_replace_callback(
'/(?:(?<!\\\\)(?:\\\\[rn]|\\r|\\n))+$/',
function (array $match) use (&$messageEndNewlines): string {
$messageEndNewlines = $match[0];
return '';
},
$this->normalize($record['message'])
);
//endregion
//region Format the record
$output = parent::format($record);
//endregion
//region Remove newlines at end (introduced by context, extra or record message itself)
$output = preg_replace('/(?:(?<!\\\\)(?:\\\\[rn]|\\r|\\n))+$/', '', $output);
//endregion
//region Re-add the newlines of record message end
$output .= $messageEndNewlines;
//endregion
//region Then finally, replace manual "\r" or "\n" by theirs equivalent
return preg_replace(
'/(?:(?<!\\\\)\\\\[rn])+/', PHP_EOL, $output
);
//endregion
}
}

@ -4,6 +4,7 @@ require_once __DIR__ . '/../vendor/autoload.php';
use jrosset\ExtendedMonolog\ExceptionLogger;
use jrosset\ExtendedMonolog\LogDirectoryHandler;
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
@ -11,18 +12,25 @@ use Monolog\Logger;
$logger = new ExceptionLogger(
'test',
[
new StreamHandler(STDOUT),
(new StreamHandler(STDOUT))
->setFormatter(
new LineFormatter(
'[%datetime%] %level_name% : %message%',
'Y-m-d H:i:s',
true
)
),
new LogDirectoryHandler(__DIR__ . '/logs/'),
]
);
try {
$logger->info('START');
$logger->info('======= test =======' . PHP_EOL . 'START' . PHP_EOL);
throw new RuntimeException('An unexpected error occurs');
/** @noinspection PhpUnreachableStatementInspection */
$logger->info('END');
$logger->info('END' . PHP_EOL);
}
catch (Throwable $exception) {
$logger->exception(Logger::ERROR, $exception);

Loading…
Cancel
Save