Kaynağa Gözat

Merge pull request #507 from jamiel/master

#493 Include context in slack extra info.
Jordi Boggiano 11 yıl önce
ebeveyn
işleme
f05ed3a44a
1 değiştirilmiş dosya ile 58 ekleme ve 9 silme
  1. 58 9
      src/Monolog/Handler/SlackHandler.php

+ 58 - 9
src/Monolog/Handler/SlackHandler.php

@@ -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;
+    }
 }