scratch – Blame information for rev

Subversion Repositories:
Rev:
Rev Author Line No. Line
115 office 1 <?php
2  
3 /*
4 * This file is part of the Monolog package.
5 *
6 * (c) Jordi Boggiano <j.boggiano@seld.be>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11  
12 namespace Monolog\Handler;
13  
14 use Monolog\Formatter\FormatterInterface;
15 use Monolog\Logger;
16 use Monolog\Handler\Slack\SlackRecord;
17  
18 /**
19 * Sends notifications through Slack Webhooks
20 *
21 * @author Haralan Dobrev <hkdobrev@gmail.com>
22 * @see https://api.slack.com/incoming-webhooks
23 */
24 class SlackWebhookHandler extends AbstractProcessingHandler
25 {
26 /**
27 * Slack Webhook token
28 * @var string
29 */
30 private $webhookUrl;
31  
32 /**
33 * Instance of the SlackRecord util class preparing data for Slack API.
34 * @var SlackRecord
35 */
36 private $slackRecord;
37  
38 /**
39 * @param string $webhookUrl Slack Webhook URL
40 * @param string|null $channel Slack channel (encoded ID or name)
41 * @param string|null $username Name of a bot
42 * @param bool $useAttachment Whether the message should be added to Slack as attachment (plain text otherwise)
43 * @param string|null $iconEmoji The emoji name to use (or null)
44 * @param bool $useShortAttachment Whether the the context/extra messages added to Slack as attachments are in a short style
45 * @param bool $includeContextAndExtra Whether the attachment should include context and extra data
46 * @param int $level The minimum logging level at which this handler will be triggered
47 * @param bool $bubble Whether the messages that are handled can bubble up the stack or not
48 * @param array $excludeFields Dot separated list of fields to exclude from slack message. E.g. ['context.field1', 'extra.field2']
49 */
50 public function __construct($webhookUrl, $channel = null, $username = null, $useAttachment = true, $iconEmoji = null, $useShortAttachment = false, $includeContextAndExtra = false, $level = Logger::CRITICAL, $bubble = true, array $excludeFields = array())
51 {
52 parent::__construct($level, $bubble);
53  
54 $this->webhookUrl = $webhookUrl;
55  
56 $this->slackRecord = new SlackRecord(
57 $channel,
58 $username,
59 $useAttachment,
60 $iconEmoji,
61 $useShortAttachment,
62 $includeContextAndExtra,
63 $excludeFields,
64 $this->formatter
65 );
66 }
67  
68 public function getSlackRecord()
69 {
70 return $this->slackRecord;
71 }
72  
73 /**
74 * {@inheritdoc}
75 *
76 * @param array $record
77 */
78 protected function write(array $record)
79 {
80 $postData = $this->slackRecord->getSlackData($record);
81 $postString = json_encode($postData);
82  
83 $ch = curl_init();
84 $options = array(
85 CURLOPT_URL => $this->webhookUrl,
86 CURLOPT_POST => true,
87 CURLOPT_RETURNTRANSFER => true,
88 CURLOPT_HTTPHEADER => array('Content-type: application/json'),
89 CURLOPT_POSTFIELDS => $postString
90 );
91 if (defined('CURLOPT_SAFE_UPLOAD')) {
92 $options[CURLOPT_SAFE_UPLOAD] = true;
93 }
94  
95 curl_setopt_array($ch, $options);
96  
97 Curl\Util::execute($ch);
98 }
99  
100 public function setFormatter(FormatterInterface $formatter)
101 {
102 parent::setFormatter($formatter);
103 $this->slackRecord->setFormatter($formatter);
104  
105 return $this;
106 }
107  
108 public function getFormatter()
109 {
110 $formatter = parent::getFormatter();
111 $this->slackRecord->setFormatter($formatter);
112  
113 return $formatter;
114 }
115 }