SlackRecord.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. <?php
  2. /*
  3. * This file is part of the Monolog package.
  4. *
  5. * (c) Jordi Boggiano <j.boggiano@seld.be>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Monolog\Handler\Slack;
  11. use Monolog\Logger;
  12. use Monolog\Formatter\LineFormatter;
  13. use Monolog\Formatter\FormatterInterface;
  14. /**
  15. * Slack record utility helping to log to Slack webhooks or API.
  16. *
  17. * @author Greg Kedzierski <greg@gregkedzierski.com>
  18. * @author Haralan Dobrev <hkdobrev@gmail.com>
  19. * @see https://api.slack.com/incoming-webhooks
  20. * @see https://api.slack.com/docs/message-attachments
  21. */
  22. class SlackRecord
  23. {
  24. const COLOR_DANGER = 'danger';
  25. const COLOR_WARNING = 'warning';
  26. const COLOR_GOOD = 'good';
  27. const COLOR_DEFAULT = '#e3e4e6';
  28. /**
  29. * Slack channel (encoded ID or name)
  30. * @var string|null
  31. */
  32. private $channel;
  33. /**
  34. * Name of a bot
  35. * @var string
  36. */
  37. private $username;
  38. /**
  39. * Emoji icon name
  40. * @var string
  41. */
  42. private $iconEmoji;
  43. /**
  44. * Whether the message should be added to Slack as attachment (plain text otherwise)
  45. * @var bool
  46. */
  47. private $useAttachment;
  48. /**
  49. * Whether the the context/extra messages added to Slack as attachments are in a short style
  50. * @var bool
  51. */
  52. private $useShortAttachment;
  53. /**
  54. * Whether the attachment should include context and extra data
  55. * @var bool
  56. */
  57. private $includeContextAndExtra;
  58. /**
  59. * @var FormatterInterface
  60. */
  61. private $formatter;
  62. /**
  63. * @var LineFormatter
  64. */
  65. private $lineFormatter;
  66. public function __construct($channel = null, $username = 'Monolog', $useAttachment = true, $iconEmoji = null, $useShortAttachment = false, $includeContextAndExtra = false, FormatterInterface $formatter = null)
  67. {
  68. $this->channel = $channel;
  69. $this->username = $username;
  70. $this->iconEmoji = trim($iconEmoji, ':');
  71. $this->useAttachment = $useAttachment;
  72. $this->useShortAttachment = $useShortAttachment;
  73. $this->includeContextAndExtra = $includeContextAndExtra;
  74. $this->formatter = $formatter;
  75. if ($this->includeContextAndExtra) {
  76. $this->lineFormatter = new LineFormatter();
  77. }
  78. }
  79. public function getSlackData(array $record)
  80. {
  81. $dataArray = array(
  82. 'username' => $this->username,
  83. 'text' => '',
  84. );
  85. if ($this->channel) {
  86. $dataArray['channel'] = $this->channel;
  87. }
  88. if ($this->formatter) {
  89. $message = $this->formatter->format($record);
  90. } else {
  91. $message = $record['message'];
  92. }
  93. if ($this->useAttachment) {
  94. $attachment = array(
  95. 'fallback' => $message,
  96. 'text' => $message,
  97. 'color' => $this->getAttachmentColor($record['level']),
  98. 'fields' => array(),
  99. );
  100. if ($this->useShortAttachment) {
  101. $attachment['title'] = $record['level_name'];
  102. } else {
  103. $attachment['title'] = 'Message';
  104. $attachment['fields'][] = $this->generateAttachmentField('Level', $record['level_name'], true);
  105. }
  106. if ($this->includeContextAndExtra) {
  107. foreach (array('extra', 'context') as $key) {
  108. if (empty($record[$key])) {
  109. continue;
  110. }
  111. if ($this->useShortAttachment) {
  112. $attachment['fields'][] = $this->generateAttachmentField(
  113. ucfirst($key),
  114. $this->stringify($record[$key]),
  115. true
  116. );
  117. } else {
  118. // Add all extra fields as individual fields in attachment
  119. $attachment['fields'] = array_merge(
  120. $attachment['fields'],
  121. $this->generateAttachmentFields($record[$key])
  122. );
  123. }
  124. }
  125. }
  126. $dataArray['attachments'] = array($attachment);
  127. } else {
  128. $dataArray['text'] = $message;
  129. }
  130. if ($this->iconEmoji) {
  131. $dataArray['icon_emoji'] = ":{$this->iconEmoji}:";
  132. }
  133. return $dataArray;
  134. }
  135. /**
  136. * Returned a Slack message attachment color associated with
  137. * provided level.
  138. *
  139. * @param int $level
  140. * @return string
  141. */
  142. public function getAttachmentColor($level)
  143. {
  144. switch (true) {
  145. case $level >= Logger::ERROR:
  146. return self::COLOR_DANGER;
  147. case $level >= Logger::WARNING:
  148. return self::COLOR_WARNING;
  149. case $level >= Logger::INFO:
  150. return self::COLOR_GOOD;
  151. default:
  152. return self::COLOR_DEFAULT;
  153. }
  154. }
  155. /**
  156. * Stringifies an array of key/value pairs to be used in attachment fields
  157. *
  158. * @param array $fields
  159. * @return string|null
  160. */
  161. public function stringify($fields)
  162. {
  163. if (!$this->lineFormatter) {
  164. return null;
  165. }
  166. $string = '';
  167. foreach ($fields as $var => $val) {
  168. $string .= $var.': '.$this->lineFormatter->stringify($val)." | ";
  169. }
  170. $string = rtrim($string, " |");
  171. return $string;
  172. }
  173. /**
  174. * Sets the formatter
  175. *
  176. * @param FormatterInterface $formatter
  177. */
  178. public function setFormatter(FormatterInterface $formatter)
  179. {
  180. $this->formatter = $formatter;
  181. }
  182. /**
  183. * Generates attachment field
  184. *
  185. * @param string $title
  186. * @param string|array $value
  187. * @param bool $short
  188. * @return array
  189. */
  190. private function generateAttachmentField($title, $value, $short)
  191. {
  192. return array(
  193. 'title' => $title,
  194. 'value' => is_array($value) ? $this->lineFormatter->stringify($value) : $value,
  195. 'short' => $short
  196. );
  197. }
  198. /**
  199. * Generates a collection of attachment fields from array
  200. *
  201. * @param array $data
  202. * @return array
  203. */
  204. private function generateAttachmentFields(array $data)
  205. {
  206. $fields = array();
  207. foreach ($data as $key => $value) {
  208. $fields[] = $this->generateAttachmentField($key, $value, false);
  209. }
  210. return $fields;
  211. }
  212. }