1
0
mirror of https://github.com/Seldaek/monolog.git synced 2025-08-07 13:46:38 +02:00

Merge pull request #507 from jamiel/master

#493 Include context in slack extra info.
This commit is contained in:
Jordi Boggiano
2015-02-11 14:20:33 +00:00

View File

@@ -157,18 +157,48 @@ 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) {
$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
);
}
}
}
}
$dataArray['attachments'] = json_encode(array($attachment));
@@ -231,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;
}
}