1
0
mirror of https://github.com/Seldaek/monolog.git synced 2025-08-08 14:16:42 +02:00

#493 Ensure extra and context fields are only added when they are present and not empty. Ensure both follow have consistent formatting.

This commit is contained in:
Jamie Learmonth
2015-02-11 14:11:24 +00:00
parent 01e593cdeb
commit 6f0e7f666e

View File

@@ -157,26 +157,47 @@ class SlackHandler extends SocketHandler
}
if ($this->includeExtra) {
$extra = '';
foreach ($record['extra'] as $var => $val) {
$extra .= $var.': '.$this->lineFormatter->stringify($val)." | ";
if (!empty($record['extra'])) {
if ($this->useShortAttachment) {
$attachment['fields'][] = array(
'title' => "Extra",
'value' => $this->stringify($record['extra']),
'short' => $this->useShortAttachment
);
} else {
// Add all extra fields as individual fields in attachment
foreach ($record['extra'] as $var => $val) {
$attachment['fields'][] = array(
'title' => $var,
'value' => $val,
'short' => $this->useShortAttachment
);
}
}
}
$extra = rtrim($extra, " |");
if (!empty($record['context'])) {
$attachment['fields'][] = array(
'title' => "Extra",
'value' => $extra,
'short' => false
);
if ($this->useShortAttachment) {
// Add all context items as individual fields
foreach ($record['context'] as $var => $val) {
$attachment['fields'][] = array(
'title' => $var,
'value' => $val,
'short' => true
);
$attachment['fields'][] = array(
'title' => "Context",
'value' => $this->stringify($record['context']),
'short' => $this->useShortAttachment
);
} else {
// Add all context fields as individual fields in attachment
foreach ($record['context'] as $var => $val) {
$attachment['fields'][] = array(
'title' => $var,
'value' => $val,
'short' => $this->useShortAttachment
);
}
}
}
}
@@ -240,4 +261,23 @@ class SlackHandler extends SocketHandler
return '#e3e4e6';
}
}
/**
* Stringifys an array of key/value pairs to be used in attachment fields
*
* @param array $fields
* @access protected
* @return string
*/
protected function stringify($fields)
{
$string = '';
foreach ($fields as $var => $val) {
$string .= $var.': '.$this->lineFormatter->stringify($val)." | ";
}
$string = rtrim($string, " |");
return $string;
}
}