summaryrefslogtreecommitdiff
path: root/vendor/monolog/monolog/src/Monolog/Handler/SlackHandler.php
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/monolog/monolog/src/Monolog/Handler/SlackHandler.php')
-rw-r--r--vendor/monolog/monolog/src/Monolog/Handler/SlackHandler.php120
1 files changed, 89 insertions, 31 deletions
diff --git a/vendor/monolog/monolog/src/Monolog/Handler/SlackHandler.php b/vendor/monolog/monolog/src/Monolog/Handler/SlackHandler.php
index e3c8e11b..c7a1c7e9 100644
--- a/vendor/monolog/monolog/src/Monolog/Handler/SlackHandler.php
+++ b/vendor/monolog/monolog/src/Monolog/Handler/SlackHandler.php
@@ -53,16 +53,16 @@ class SlackHandler extends SocketHandler
private $useAttachment;
/**
- * Whether the the message that is added to Slack as attachment is in a short style (or not)
+ * Whether the the context/extra messages added to Slack as attachments are in a short style
* @var bool
*/
private $useShortAttachment;
/**
- * Whether the attachment should include extra data (or not)
+ * Whether the attachment should include context and extra data
* @var bool
*/
- private $includeExtra;
+ private $includeContextAndExtra;
/**
* @var LineFormatter
@@ -70,15 +70,17 @@ class SlackHandler extends SocketHandler
private $lineFormatter;
/**
- * @param string $token Slack API token
- * @param string $channel Slack channel (encoded ID or name)
- * @param string $username Name of a bot
- * @param bool $useAttachment Whether the message should be added to Slack as attachment (plain text otherwise)
- * @param string|null $iconEmoji The emoji name to use (or null)
- * @param int $level The minimum logging level at which this handler will be triggered
- * @param bool $bubble Whether the messages that are handled can bubble up the stack or not
+ * @param string $token Slack API token
+ * @param string $channel Slack channel (encoded ID or name)
+ * @param string $username Name of a bot
+ * @param bool $useAttachment Whether the message should be added to Slack as attachment (plain text otherwise)
+ * @param string|null $iconEmoji The emoji name to use (or null)
+ * @param int $level The minimum logging level at which this handler will be triggered
+ * @param bool $bubble Whether the messages that are handled can bubble up the stack or not
+ * @param bool $useShortAttachment Whether the the context/extra messages added to Slack as attachments are in a short style
+ * @param bool $includeContextAndExtra Whether the attachment should include context and extra data
*/
- public function __construct($token, $channel, $username = 'Monolog', $useAttachment = true, $iconEmoji = null, $level = Logger::CRITICAL, $bubble = true, $useShortAttachment = false, $includeExtra = false)
+ public function __construct($token, $channel, $username = 'Monolog', $useAttachment = true, $iconEmoji = null, $level = Logger::CRITICAL, $bubble = true, $useShortAttachment = false, $includeContextAndExtra = false)
{
if (!extension_loaded('openssl')) {
throw new MissingExtensionException('The OpenSSL PHP extension is required to use the SlackHandler');
@@ -92,8 +94,8 @@ class SlackHandler extends SocketHandler
$this->iconEmoji = trim($iconEmoji, ':');
$this->useAttachment = $useAttachment;
$this->useShortAttachment = $useShortAttachment;
- $this->includeExtra = $includeExtra;
- if ($this->includeExtra) {
+ $this->includeContextAndExtra = $includeContextAndExtra;
+ if ($this->includeContextAndExtra) {
$this->lineFormatter = new LineFormatter;
}
}
@@ -119,18 +121,31 @@ class SlackHandler extends SocketHandler
*/
private function buildContent($record)
{
+ $dataArray = $this->prepareContentData($record);
+
+ return http_build_query($dataArray);
+ }
+
+ /**
+ * Prepares content data
+ *
+ * @param array $record
+ * @return array
+ */
+ protected function prepareContentData($record)
+ {
$dataArray = array(
- 'token' => $this->token,
- 'channel' => $this->channel,
- 'username' => $this->username,
- 'text' => '',
+ 'token' => $this->token,
+ 'channel' => $this->channel,
+ 'username' => $this->username,
+ 'text' => '',
'attachments' => array()
);
if ($this->useAttachment) {
$attachment = array(
'fallback' => $record['message'],
- 'color' => $this->getAttachmentColor($record['level'])
+ 'color' => $this->getAttachmentColor($record['level'])
);
if ($this->useShortAttachment) {
@@ -156,19 +171,44 @@ class SlackHandler extends SocketHandler
);
}
- if ($this->includeExtra) {
- $extra = '';
- foreach ($record['extra'] as $var => $val) {
- $extra .= $var.': '.$this->lineFormatter->stringify($val)." | ";
+ if ($this->includeContextAndExtra) {
+ 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, " |");
-
- $attachment['fields'][] = array(
- 'title' => "Extra",
- 'value' => $extra,
- 'short' => false
- );
+ 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) {
+ $attachment['fields'][] = array(
+ 'title' => $var,
+ 'value' => $val,
+ 'short' => $this->useShortAttachment
+ );
+ }
+ }
+ }
}
$dataArray['attachments'] = json_encode(array($attachment));
@@ -179,8 +219,7 @@ class SlackHandler extends SocketHandler
if ($this->iconEmoji) {
$dataArray['icon_emoji'] = ":{$this->iconEmoji}:";
}
-
- return http_build_query($dataArray);
+ return $dataArray;
}
/**
@@ -231,4 +270,23 @@ class SlackHandler extends SocketHandler
return '#e3e4e6';
}
}
+
+ /**
+ * Stringifies 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;
+ }
}