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

Refactor SlackRecord

This commit is contained in:
Anton Nizhegorodov
2016-11-14 03:24:07 +02:00
committed by Haralan Dobrev
parent 08b577c657
commit 1303dc6d72

View File

@@ -115,59 +115,36 @@ class SlackRecord
if ($this->useAttachment) {
$attachment = array(
'fallback' => $message,
'text' => $message,
'color' => $this->getAttachmentColor($record['level']),
'fields' => array(),
);
if ($this->useShortAttachment) {
$attachment['title'] = $record['level_name'];
$attachment['text'] = $message;
} else {
$attachment['title'] = 'Message';
$attachment['text'] = $message;
$attachment['fields'][] = array(
'title' => 'Level',
'value' => $record['level_name'],
'short' => true,
);
$attachment['fields'][] = $this->generateAttachmentField('Level', $record['level_name'], true);
}
if ($this->includeContextAndExtra) {
if (!empty($record['extra'])) {
foreach (array('extra', 'context') as $key) {
if (empty($record[$key])) {
continue;
}
if ($this->useShortAttachment) {
$attachment['fields'][] = array(
'title' => "Extra",
'value' => $this->stringify($record['extra']),
'short' => true,
$attachment['fields'][] = $this->generateAttachmentField(
ucfirst($key),
$this->stringify($record[$key]),
true
);
} else {
// Add all extra fields as individual fields in attachment
foreach ($record['extra'] as $var => $val) {
$attachment['fields'][] = array(
'title' => $var,
'value' => is_array($val) ? $this->lineFormatter->stringify($val) : $val,
'short' => false,
);
}
}
}
if (!empty($record['context'])) {
if ($this->useShortAttachment) {
$attachment['fields'][] = array(
'title' => "Context",
'value' => $this->stringify($record['context']),
'short' => true,
$attachment['fields'] = array_merge(
$attachment['fields'],
$this->generateAttachmentFields($record[$key])
);
} else {
// Add all context fields as individual fields in attachment
foreach ($record['context'] as $var => $val) {
$attachment['fields'][] = array(
'title' => $var,
'value' => is_array($val) ? $this->lineFormatter->stringify($val) : $val,
'short' => false,
);
}
}
}
}
@@ -236,4 +213,37 @@ class SlackRecord
{
$this->formatter = $formatter;
}
/**
* Generates attachment field
*
* @param $title
* @param $value
* @param $short
* @return array
*/
private function generateAttachmentField($title, $value, $short)
{
return array(
'title' => $title,
'value' => is_array($value) ? $this->lineFormatter->stringify($value) : $value,
'short' => $short
);
}
/**
* Generates a collection of attachment fields from array
*
* @param array $data
* @return array
*/
private function generateAttachmentFields(array $data)
{
$fields = array();
foreach ($data as $key => $value) {
$fields[] = $this->generateAttachmentField($key, $value, false);
}
return $fields;
}
}