mirror of
https://github.com/Seldaek/monolog.git
synced 2025-08-07 21:56:31 +02:00
Refactor SlackRecord
This commit is contained in:
committed by
Haralan Dobrev
parent
08b577c657
commit
1303dc6d72
@@ -115,59 +115,36 @@ class SlackRecord
|
|||||||
if ($this->useAttachment) {
|
if ($this->useAttachment) {
|
||||||
$attachment = array(
|
$attachment = array(
|
||||||
'fallback' => $message,
|
'fallback' => $message,
|
||||||
|
'text' => $message,
|
||||||
'color' => $this->getAttachmentColor($record['level']),
|
'color' => $this->getAttachmentColor($record['level']),
|
||||||
'fields' => array(),
|
'fields' => array(),
|
||||||
);
|
);
|
||||||
|
|
||||||
if ($this->useShortAttachment) {
|
if ($this->useShortAttachment) {
|
||||||
$attachment['title'] = $record['level_name'];
|
$attachment['title'] = $record['level_name'];
|
||||||
$attachment['text'] = $message;
|
|
||||||
} else {
|
} else {
|
||||||
$attachment['title'] = 'Message';
|
$attachment['title'] = 'Message';
|
||||||
$attachment['text'] = $message;
|
$attachment['fields'][] = $this->generateAttachmentField('Level', $record['level_name'], true);
|
||||||
$attachment['fields'][] = array(
|
|
||||||
'title' => 'Level',
|
|
||||||
'value' => $record['level_name'],
|
|
||||||
'short' => true,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($this->includeContextAndExtra) {
|
if ($this->includeContextAndExtra) {
|
||||||
if (!empty($record['extra'])) {
|
foreach (array('extra', 'context') as $key) {
|
||||||
|
if (empty($record[$key])) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
if ($this->useShortAttachment) {
|
if ($this->useShortAttachment) {
|
||||||
$attachment['fields'][] = array(
|
$attachment['fields'][] = $this->generateAttachmentField(
|
||||||
'title' => "Extra",
|
ucfirst($key),
|
||||||
'value' => $this->stringify($record['extra']),
|
$this->stringify($record[$key]),
|
||||||
'short' => true,
|
true
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
// Add all extra fields as individual fields in attachment
|
// Add all extra fields as individual fields in attachment
|
||||||
foreach ($record['extra'] as $var => $val) {
|
$attachment['fields'] = array_merge(
|
||||||
$attachment['fields'][] = array(
|
$attachment['fields'],
|
||||||
'title' => $var,
|
$this->generateAttachmentFields($record[$key])
|
||||||
'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,
|
|
||||||
);
|
);
|
||||||
} 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;
|
$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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user