ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
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
16
24{
29 private $token;
30
35 private $channel;
36
41 private $username;
42
47 private $iconEmoji;
48
54
60
66
71
85 {
86 if (!extension_loaded('openssl')) {
87 throw new MissingExtensionException('The OpenSSL PHP extension is required to use the SlackHandler');
88 }
89
90 parent::__construct('ssl://slack.com:443', $level, $bubble);
91
92 $this->token = $token;
93 $this->channel = $channel;
94 $this->username = $username;
95 $this->iconEmoji = trim($iconEmoji, ':');
96 $this->useAttachment = $useAttachment;
97 $this->useShortAttachment = $useShortAttachment;
98 $this->includeContextAndExtra = $includeContextAndExtra;
99
100 if ($this->includeContextAndExtra && $this->useShortAttachment) {
101 $this->lineFormatter = new LineFormatter;
102 }
103 }
104
111 protected function generateDataStream($record)
112 {
113 $content = $this->buildContent($record);
114
115 return $this->buildHeader($content) . $content;
116 }
117
124 private function buildContent($record)
125 {
126 $dataArray = $this->prepareContentData($record);
127
128 return http_build_query($dataArray);
129 }
130
137 protected function prepareContentData($record)
138 {
139 $dataArray = array(
140 'token' => $this->token,
141 'channel' => $this->channel,
142 'username' => $this->username,
143 'text' => '',
144 'attachments' => array(),
145 );
146
147 if ($this->formatter) {
148 $message = $this->formatter->format($record);
149 } else {
150 $message = $record['message'];
151 }
152
153 if ($this->useAttachment) {
154 $attachment = array(
155 'fallback' => $message,
156 'color' => $this->getAttachmentColor($record['level']),
157 'fields' => array(),
158 );
159
160 if ($this->useShortAttachment) {
161 $attachment['title'] = $record['level_name'];
162 $attachment['text'] = $message;
163 } else {
164 $attachment['title'] = 'Message';
165 $attachment['text'] = $message;
166 $attachment['fields'][] = array(
167 'title' => 'Level',
168 'value' => $record['level_name'],
169 'short' => true,
170 );
171 }
172
173 if ($this->includeContextAndExtra) {
174 if (!empty($record['extra'])) {
175 if ($this->useShortAttachment) {
176 $attachment['fields'][] = array(
177 'title' => "Extra",
178 'value' => $this->stringify($record['extra']),
179 'short' => $this->useShortAttachment,
180 );
181 } else {
182 // Add all extra fields as individual fields in attachment
183 foreach ($record['extra'] as $var => $val) {
184 $attachment['fields'][] = array(
185 'title' => $var,
186 'value' => $val,
187 'short' => $this->useShortAttachment,
188 );
189 }
190 }
191 }
192
193 if (!empty($record['context'])) {
194 if ($this->useShortAttachment) {
195 $attachment['fields'][] = array(
196 'title' => "Context",
197 'value' => $this->stringify($record['context']),
198 'short' => $this->useShortAttachment,
199 );
200 } else {
201 // Add all context fields as individual fields in attachment
202 foreach ($record['context'] as $var => $val) {
203 $attachment['fields'][] = array(
204 'title' => $var,
205 'value' => $val,
206 'short' => $this->useShortAttachment,
207 );
208 }
209 }
210 }
211 }
212
213 $dataArray['attachments'] = json_encode(array($attachment));
214 } else {
215 $dataArray['text'] = $message;
216 }
217
218 if ($this->iconEmoji) {
219 $dataArray['icon_emoji'] = ":{$this->iconEmoji}:";
220 }
221
222 return $dataArray;
223 }
224
231 private function buildHeader($content)
232 {
233 $header = "POST /api/chat.postMessage HTTP/1.1\r\n";
234 $header .= "Host: slack.com\r\n";
235 $header .= "Content-Type: application/x-www-form-urlencoded\r\n";
236 $header .= "Content-Length: " . strlen($content) . "\r\n";
237 $header .= "\r\n";
238
239 return $header;
240 }
241
247 protected function write(array $record)
248 {
249 parent::write($record);
250 $res = $this->getResource();
251 if (is_resource($res)) {
252 @fread($res, 2048);
253 }
254 $this->closeSocket();
255 }
256
264 protected function getAttachmentColor($level)
265 {
266 switch (true) {
267 case $level >= Logger::ERROR:
268 return 'danger';
269 case $level >= Logger::WARNING:
270 return 'warning';
271 case $level >= Logger::INFO:
272 return 'good';
273 default:
274 return '#e3e4e6';
275 }
276 }
277
284 protected function stringify($fields)
285 {
286 $string = '';
287 foreach ($fields as $var => $val) {
288 $string .= $var.': '.$this->lineFormatter->stringify($val)." | ";
289 }
290
291 $string = rtrim($string, " |");
292
293 return $string;
294 }
295}
$dataArray
An exception for terminatinating execution or to throw for unit testing.
Formats incoming records into a one-line string.
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.
__construct($token, $channel, $username='Monolog', $useAttachment=true, $iconEmoji=null, $level=Logger::CRITICAL, $bubble=true, $useShortAttachment=false, $includeContextAndExtra=false)
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.
prepareContentData($record)
Prepares content data.
Stores to any socket - uses fsockopen() or pfsockopen().
closeSocket()
Close socket, if open.
Monolog log channel.
Definition: Logger.php:28
const ERROR
Runtime errors.
Definition: Logger.php:57
const CRITICAL
Critical conditions.
Definition: Logger.php:64
const WARNING
Exceptional occurrences that are not errors.
Definition: Logger.php:52
const INFO
Interesting events.
Definition: Logger.php:39
catch(Exception $e) $message
foreach($_POST as $key=> $value) $res