1
0
mirror of https://github.com/Seldaek/monolog.git synced 2025-10-22 17:16:18 +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,28 +157,49 @@ class SlackHandler extends SocketHandler
} }
if ($this->includeExtra) { if ($this->includeExtra) {
$extra = '';
foreach ($record['extra'] as $var => $val) {
$extra .= $var.': '.$this->lineFormatter->stringify($val)." | ";
}
$extra = rtrim($extra, " |"); if (!empty($record['extra'])) {
if ($this->useShortAttachment) {
$attachment['fields'][] = array( $attachment['fields'][] = array(
'title' => "Extra", 'title' => "Extra",
'value' => $extra, 'value' => $this->stringify($record['extra']),
'short' => false '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
);
}
}
}
// Add all context items as individual fields if (!empty($record['context'])) {
if ($this->useShortAttachment) {
$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) { foreach ($record['context'] as $var => $val) {
$attachment['fields'][] = array( $attachment['fields'][] = array(
'title' => $var, 'title' => $var,
'value' => $val, 'value' => $val,
'short' => true 'short' => $this->useShortAttachment
); );
} }
} }
}
}
$dataArray['attachments'] = json_encode(array($attachment)); $dataArray['attachments'] = json_encode(array($attachment));
} else { } else {
@@ -240,4 +261,23 @@ class SlackHandler extends SocketHandler
return '#e3e4e6'; 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;
}
} }