ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
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
84 {
85 if (!extension_loaded('openssl')) {
86 throw new MissingExtensionException('The OpenSSL PHP extension is required to use the SlackHandler');
87 }
88
89 parent::__construct('ssl://slack.com:443', $level, $bubble);
90
91 $this->token = $token;
92 $this->channel = $channel;
93 $this->username = $username;
94 $this->iconEmoji = trim($iconEmoji, ':');
95 $this->useAttachment = $useAttachment;
96 $this->useShortAttachment = $useShortAttachment;
97 $this->includeContextAndExtra = $includeContextAndExtra;
98 if ($this->includeContextAndExtra) {
99 $this->lineFormatter = new LineFormatter;
100 }
101 }
102
109 protected function generateDataStream($record)
110 {
111 $content = $this->buildContent($record);
112
113 return $this->buildHeader($content) . $content;
114 }
115
122 private function buildContent($record)
123 {
124 $dataArray = $this->prepareContentData($record);
125
126 return http_build_query($dataArray);
127 }
128
135 protected function prepareContentData($record)
136 {
137 $dataArray = array(
138 'token' => $this->token,
139 'channel' => $this->channel,
140 'username' => $this->username,
141 'text' => '',
142 'attachments' => array()
143 );
144
145 if ($this->useAttachment) {
146 $attachment = array(
147 'fallback' => $record['message'],
148 'color' => $this->getAttachmentColor($record['level'])
149 );
150
151 if ($this->useShortAttachment) {
152 $attachment['fields'] = array(
153 array(
154 'title' => $record['level_name'],
155 'value' => $record['message'],
156 'short' => false
157 )
158 );
159 } else {
160 $attachment['fields'] = array(
161 array(
162 'title' => 'Message',
163 'value' => $record['message'],
164 'short' => false
165 ),
166 array(
167 'title' => 'Level',
168 'value' => $record['level_name'],
169 'short' => true
170 )
171 );
172 }
173
174 if ($this->includeContextAndExtra) {
175 if (!empty($record['extra'])) {
176 if ($this->useShortAttachment) {
177 $attachment['fields'][] = array(
178 'title' => "Extra",
179 'value' => $this->stringify($record['extra']),
180 'short' => $this->useShortAttachment
181 );
182 } else {
183 // Add all extra fields as individual fields in attachment
184 foreach ($record['extra'] as $var => $val) {
185 $attachment['fields'][] = array(
186 'title' => $var,
187 'value' => $val,
188 'short' => $this->useShortAttachment
189 );
190 }
191 }
192 }
193
194 if (!empty($record['context'])) {
195 if ($this->useShortAttachment) {
196 $attachment['fields'][] = array(
197 'title' => "Context",
198 'value' => $this->stringify($record['context']),
199 'short' => $this->useShortAttachment
200 );
201 } else {
202 // Add all context fields as individual fields in attachment
203 foreach ($record['context'] as $var => $val) {
204 $attachment['fields'][] = array(
205 'title' => $var,
206 'value' => $val,
207 'short' => $this->useShortAttachment
208 );
209 }
210 }
211 }
212 }
213
214 $dataArray['attachments'] = json_encode(array($attachment));
215 } else {
216 $dataArray['text'] = $record['message'];
217 }
218
219 if ($this->iconEmoji) {
220 $dataArray['icon_emoji'] = ":{$this->iconEmoji}:";
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 $this->closeSocket();
251 }
252
260 protected function getAttachmentColor($level)
261 {
262 switch (true) {
263 case $level >= Logger::ERROR:
264 return 'danger';
265 case $level >= Logger::WARNING:
266 return 'warning';
267 case $level >= Logger::INFO:
268 return 'good';
269 default:
270 return '#e3e4e6';
271 }
272 }
273
281 protected function stringify($fields)
282 {
283 $string = '';
284 foreach ($fields as $var => $val) {
285 $string .= $var.': '.$this->lineFormatter->stringify($val)." | ";
286 }
287
288 $string = rtrim($string, " |");
289
290 return $string;
291 }
292}
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
$header