mirror of
https://github.com/Seldaek/monolog.git
synced 2025-08-03 11:47:38 +02:00
LineFormatter: context and extra optionally not shown when empty
This commit is contained in:
@@ -27,16 +27,19 @@ class LineFormatter extends NormalizerFormatter
|
|||||||
|
|
||||||
protected $format;
|
protected $format;
|
||||||
protected $allowInlineLineBreaks;
|
protected $allowInlineLineBreaks;
|
||||||
|
protected $ignoreEmptyContextAndExtra;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $format The format of the message
|
* @param string $format The format of the message
|
||||||
* @param string $dateFormat The format of the timestamp: one supported by DateTime::format
|
* @param string $dateFormat The format of the timestamp: one supported by DateTime::format
|
||||||
* @param bool $allowInlineLineBreaks Whether to allow inline line breaks in log entries
|
* @param bool $allowInlineLineBreaks Whether to allow inline line breaks in log entries
|
||||||
|
* @param bool $ignoreEmptyContextAndExtra
|
||||||
*/
|
*/
|
||||||
public function __construct($format = null, $dateFormat = null, $allowInlineLineBreaks = false)
|
public function __construct($format = null, $dateFormat = null, $allowInlineLineBreaks = false, $ignoreEmptyContextAndExtra = false)
|
||||||
{
|
{
|
||||||
$this->format = $format ?: static::SIMPLE_FORMAT;
|
$this->format = $format ?: static::SIMPLE_FORMAT;
|
||||||
$this->allowInlineLineBreaks = $allowInlineLineBreaks;
|
$this->allowInlineLineBreaks = $allowInlineLineBreaks;
|
||||||
|
$this->ignoreEmptyContextAndExtra = $ignoreEmptyContextAndExtra;
|
||||||
parent::__construct($dateFormat);
|
parent::__construct($dateFormat);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -48,12 +51,26 @@ class LineFormatter extends NormalizerFormatter
|
|||||||
$vars = parent::format($record);
|
$vars = parent::format($record);
|
||||||
|
|
||||||
$output = $this->format;
|
$output = $this->format;
|
||||||
|
|
||||||
foreach ($vars['extra'] as $var => $val) {
|
foreach ($vars['extra'] as $var => $val) {
|
||||||
if (false !== strpos($output, '%extra.'.$var.'%')) {
|
if (false !== strpos($output, '%extra.'.$var.'%')) {
|
||||||
$output = str_replace('%extra.'.$var.'%', $this->replaceNewlines($this->convertToString($val)), $output);
|
$output = str_replace('%extra.'.$var.'%', $this->replaceNewlines($this->convertToString($val)), $output);
|
||||||
unset($vars['extra'][$var]);
|
unset($vars['extra'][$var]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ($this->ignoreEmptyContextAndExtra) {
|
||||||
|
if (empty($vars['context'])) {
|
||||||
|
unset($vars['context']);
|
||||||
|
$output = str_replace('%context%', '', $output);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (empty($vars['extra'])) {
|
||||||
|
unset($vars['extra']);
|
||||||
|
$output = str_replace('%extra%', '', $output);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
foreach ($vars as $var => $val) {
|
foreach ($vars as $var => $val) {
|
||||||
if (false !== strpos($output, '%'.$var.'%')) {
|
if (false !== strpos($output, '%'.$var.'%')) {
|
||||||
$output = str_replace('%'.$var.'%', $this->replaceNewlines($this->convertToString($val)), $output);
|
$output = str_replace('%'.$var.'%', $this->replaceNewlines($this->convertToString($val)), $output);
|
||||||
|
@@ -77,6 +77,20 @@ class LineFormatterTest extends \PHPUnit_Framework_TestCase
|
|||||||
$this->assertEquals('['.date('Y-m-d').'] meh.ERROR: log [] test {"ip":"127.0.0.1"}'."\n", $message);
|
$this->assertEquals('['.date('Y-m-d').'] meh.ERROR: log [] test {"ip":"127.0.0.1"}'."\n", $message);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testContextAndExtraOptionallyNotShownIfEmpty()
|
||||||
|
{
|
||||||
|
$formatter = new LineFormatter(null, 'Y-m-d', false, true);
|
||||||
|
$message = $formatter->format(array(
|
||||||
|
'level_name' => 'ERROR',
|
||||||
|
'channel' => 'meh',
|
||||||
|
'context' => array(),
|
||||||
|
'datetime' => new \DateTime,
|
||||||
|
'extra' => array(),
|
||||||
|
'message' => 'log',
|
||||||
|
));
|
||||||
|
$this->assertEquals('['.date('Y-m-d').'] meh.ERROR: log '."\n", $message);
|
||||||
|
}
|
||||||
|
|
||||||
public function testDefFormatWithObject()
|
public function testDefFormatWithObject()
|
||||||
{
|
{
|
||||||
$formatter = new LineFormatter(null, 'Y-m-d');
|
$formatter = new LineFormatter(null, 'Y-m-d');
|
||||||
|
Reference in New Issue
Block a user