Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added possibility to set max length for level name in LineFormatter #1850

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/Monolog/Formatter/LineFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,23 @@ class LineFormatter extends NormalizerFormatter
protected bool $allowInlineLineBreaks;
protected bool $ignoreEmptyContextAndExtra;
protected bool $includeStacktraces;
protected ?int $maxLevelNameLength = null;
protected Closure|null $stacktracesParser = null;

/**
* @param string|null $format The format of the message
* @param string|null $dateFormat The format of the timestamp: one supported by DateTime::format
* @param bool $allowInlineLineBreaks Whether to allow inline line breaks in log entries
* @param int|null $maxLevelNameLength Maximum characters for the level name. Set null for infinite length (default)
*
* @throws \RuntimeException If the function json_encode does not exist
*/
public function __construct(?string $format = null, ?string $dateFormat = null, bool $allowInlineLineBreaks = false, bool $ignoreEmptyContextAndExtra = false, bool $includeStacktraces = false)
public function __construct(?string $format = null, ?string $dateFormat = null, bool $allowInlineLineBreaks = false, bool $ignoreEmptyContextAndExtra = false, bool $includeStacktraces = false, ?int $maxLevelNameLength = null)
{
$this->format = $format === null ? static::SIMPLE_FORMAT : $format;
$this->allowInlineLineBreaks = $allowInlineLineBreaks;
$this->ignoreEmptyContextAndExtra = $ignoreEmptyContextAndExtra;
$this->maxLevelNameLength = $maxLevelNameLength;
$this->includeStacktraces($includeStacktraces);
parent::__construct($dateFormat);
}
Expand Down Expand Up @@ -119,6 +122,11 @@ public function format(LogRecord $record): string

foreach ($vars as $var => $val) {
if (false !== strpos($output, '%'.$var.'%')) {

if ($var === 'level_name' && $this->maxLevelNameLength !== null) {
$val = substr($val, 0, $this->maxLevelNameLength);
}

$output = str_replace('%'.$var.'%', $this->stringify($val), $output);
}
}
Expand Down
34 changes: 34 additions & 0 deletions tests/Monolog/Formatter/LineFormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,40 @@ public function testFormatShouldNotStripInlineLineBreaksWhenFlagIsSet()

$this->assertMatchesRegularExpression('/foo\nbar/', $message);
}

/**
* @dataProvider providerMaxLevelNameLength
*/
public function testMaxLevelNameLength(?int $maxLength, Level $logLevel, string $expectedLevelName): void
{
$formatter = new LineFormatter(maxLevelNameLength: $maxLength);
$message = $formatter->format($this->getRecord(message: "foo\nbar", level: $logLevel));

$this->assertStringContainsString("test.$expectedLevelName:", $message);
}

public static function providerMaxLevelNameLength(): array
{
return [
'info_no_max_length' => [
'max_length' => null,
'level' => Level::Info,
'expected_level_name' => 'INFO',
],

'error_max_length_3' => [
'max_length' => 3,
'level' => Level::Error,
'expected_level_name' => 'ERR',
],

'debug_max_length_2' => [
'max_length' => 2,
'level' => Level::Debug,
'expected_level_name' => 'DE',
],
];
}
}

class TestFoo
Expand Down