ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
SlackHandler.php
Go to the documentation of this file.
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
12namespace Monolog\Handler;
13
17
25{
30 private $token;
31
36 private $slackRecord;
37
51 public function __construct($token, $channel, $username = null, $useAttachment = true, $iconEmoji = null, $level = Logger::CRITICAL, $bubble = true, $useShortAttachment = false, $includeContextAndExtra = false, array $excludeFields = array())
52 {
53 if (!extension_loaded('openssl')) {
54 throw new MissingExtensionException('The OpenSSL PHP extension is required to use the SlackHandler');
55 }
56
57 parent::__construct('ssl://slack.com:443', $level, $bubble);
58
59 $this->slackRecord = new SlackRecord(
60 $channel,
61 $username,
62 $useAttachment,
63 $iconEmoji,
64 $useShortAttachment,
65 $includeContextAndExtra,
66 $excludeFields,
67 $this->formatter
68 );
69
70 $this->token = $token;
71 }
72
73 public function getSlackRecord()
74 {
75 return $this->slackRecord;
76 }
77
78 public function getToken()
79 {
80 return $this->token;
81 }
82
89 protected function generateDataStream($record)
90 {
91 $content = $this->buildContent($record);
92
93 return $this->buildHeader($content) . $content;
94 }
95
102 private function buildContent($record)
103 {
104 $dataArray = $this->prepareContentData($record);
105
106 return http_build_query($dataArray);
107 }
108
115 protected function prepareContentData($record)
116 {
117 $dataArray = $this->slackRecord->getSlackData($record);
118 $dataArray['token'] = $this->token;
119
120 if (!empty($dataArray['attachments'])) {
121 $dataArray['attachments'] = json_encode($dataArray['attachments']);
122 }
123
124 return $dataArray;
125 }
126
133 private function buildHeader($content)
134 {
135 $header = "POST /api/chat.postMessage HTTP/1.1\r\n";
136 $header .= "Host: slack.com\r\n";
137 $header .= "Content-Type: application/x-www-form-urlencoded\r\n";
138 $header .= "Content-Length: " . strlen($content) . "\r\n";
139 $header .= "\r\n";
140
141 return $header;
142 }
143
149 protected function write(array $record)
150 {
151 parent::write($record);
152 $this->finalizeWrite();
153 }
154
161 protected function finalizeWrite()
162 {
163 $res = $this->getResource();
164 if (is_resource($res)) {
165 @fread($res, 2048);
166 }
167 $this->closeSocket();
168 }
169
178 protected function getAttachmentColor($level)
179 {
180 trigger_error(
181 'SlackHandler::getAttachmentColor() is deprecated. Use underlying SlackRecord instead.',
182 E_USER_DEPRECATED
183 );
184
185 return $this->slackRecord->getAttachmentColor($level);
186 }
187
195 protected function stringify($fields)
196 {
197 trigger_error(
198 'SlackHandler::stringify() is deprecated. Use underlying SlackRecord instead.',
199 E_USER_DEPRECATED
200 );
201
202 return $this->slackRecord->stringify($fields);
203 }
204
206 {
207 parent::setFormatter($formatter);
208 $this->slackRecord->setFormatter($formatter);
209
210 return $this;
211 }
212
213 public function getFormatter()
214 {
215 $formatter = parent::getFormatter();
216 $this->slackRecord->setFormatter($formatter);
217
218 return $formatter;
219 }
220}
An exception for terminatinating execution or to throw for unit testing.
Exception can be thrown if an extension for an handler is missing.
Sends notifications through Slack API.
buildContent($record)
Builds the body of API call.
getAttachmentColor($level)
Returned a Slack message attachment color associated with provided level.
getFormatter()
{{Gets the formatter.FormatterInterface}}
stringify($fields)
Stringifies an array of key/value pairs to be used in attachment fields.
write(array $record)
{Connect (if necessary) and write to the socket.UnexpectedValueException RuntimeException}
buildHeader($content)
Builds the header of the API Call.
finalizeWrite()
Finalizes the request by reading some bytes and then closing the socket.
setFormatter(FormatterInterface $formatter)
{{Sets the formatter.self}}
__construct($token, $channel, $username=null, $useAttachment=true, $iconEmoji=null, $level=Logger::CRITICAL, $bubble=true, $useShortAttachment=false, $includeContextAndExtra=false, array $excludeFields=array())
prepareContentData($record)
Prepares content data.
Slack record utility helping to log to Slack webhooks or API.
Definition: SlackRecord.php:27
Stores to any socket - uses fsockopen() or pfsockopen().
closeSocket()
Close socket, if open.
Monolog log channel.
Definition: Logger.php:29
const CRITICAL
Critical conditions.
Definition: Logger.php:65
foreach($_POST as $key=> $value) $res