ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
Monolog\Handler\SlackHandler Class Reference

Sends notifications through Slack API. More...

+ Inheritance diagram for Monolog\Handler\SlackHandler:
+ Collaboration diagram for Monolog\Handler\SlackHandler:

Public Member Functions

 __construct ($token, $channel, $username='Monolog', $useAttachment=true, $iconEmoji=null, $level=Logger::CRITICAL, $bubble=true, $useShortAttachment=false, $includeContextAndExtra=false)
 
- Public Member Functions inherited from Monolog\Handler\SocketHandler
 __construct ($connectionString, $level=Logger::DEBUG, $bubble=true)
 
 close ()
 We will not close a PersistentSocket instance so it can be reused in other requests. More...
 
 closeSocket ()
 Close socket, if open. More...
 
 setPersistent ($persistent)
 Set socket connection to nbe persistent. More...
 
 setConnectionTimeout ($seconds)
 Set connection timeout. More...
 
 setTimeout ($seconds)
 Set write timeout. More...
 
 setWritingTimeout ($seconds)
 Set writing timeout. More...
 
 getConnectionString ()
 Get current connection string. More...
 
 isPersistent ()
 Get persistent setting. More...
 
 getConnectionTimeout ()
 Get current connection timeout setting. More...
 
 getTimeout ()
 Get current in-transfer timeout. More...
 
 getWritingTimeout ()
 Get current local writing timeout. More...
 
 isConnected ()
 Check to see if the socket is currently available. More...
 
- Public Member Functions inherited from Monolog\Handler\AbstractProcessingHandler
 handle (array $record)
 {Handles a record.All records may be passed to this method, and the handler should discard those that it does not want to handle.The return value of this function controls the bubbling process of the handler stack. Unless the bubbling is interrupted (by returning true), the Logger class will keep on calling further handlers in the stack with a given log record.
Parameters
array$recordThe record to handle
Returns
Boolean true means that this handler handled the record, and that bubbling is not permitted. false means the record was either not processed or that this handler allows bubbling.
} More...
 
- Public Member Functions inherited from Monolog\Handler\AbstractHandler
 __construct ($level=Logger::DEBUG, $bubble=true)
 
 isHandling (array $record)
 {Checks whether the given record will be handled by this handler.This is mostly done for performance reasons, to avoid calling processors for nothing.Handlers should still check the record levels within handle(), returning false in isHandling() is no guarantee that handle() will not be called, and isHandling() might not be called for a given record.
Parameters
array$recordPartial log record containing only a level key
Returns
Boolean
} More...
 
 handleBatch (array $records)
 {Handles a set of records at once.
Parameters
array$recordsThe records to handle (an array of record arrays)
} More...
 
 close ()
 Closes the handler. More...
 
 pushProcessor ($callback)
 {Adds a processor in the stack.
Parameters
callable$callback
Returns
self
} More...
 
 popProcessor ()
 {Removes the processor on top of the stack and returns it.
Returns
callable
} More...
 
 setFormatter (FormatterInterface $formatter)
 {Sets the formatter.
Parameters
FormatterInterface$formatter
Returns
self
} More...
 
 getFormatter ()
 {Gets the formatter.
Returns
FormatterInterface
} More...
 
 setLevel ($level)
 Sets minimum logging level at which this handler will be triggered. More...
 
 getLevel ()
 Gets minimum logging level at which this handler will be triggered. More...
 
 setBubble ($bubble)
 Sets the bubbling behavior. More...
 
 getBubble ()
 Gets the bubbling behavior. More...
 
 __destruct ()
 

Protected Member Functions

 generateDataStream ($record)
 {} More...
 
 prepareContentData ($record)
 Prepares content data. More...
 
 write (array $record)
 {} More...
 
 getAttachmentColor ($level)
 Returned a Slack message attachment color associated with provided level. More...
 
 stringify ($fields)
 Stringifies an array of key/value pairs to be used in attachment fields. More...
 
- Protected Member Functions inherited from Monolog\Handler\SocketHandler
 write (array $record)
 Connect (if necessary) and write to the socket. More...
 
 pfsockopen ()
 Wrapper to allow mocking. More...
 
 fsockopen ()
 Wrapper to allow mocking. More...
 
 streamSetTimeout ()
 Wrapper to allow mocking. More...
 
 fwrite ($data)
 Wrapper to allow mocking. More...
 
 streamGetMetadata ()
 Wrapper to allow mocking. More...
 
 generateDataStream ($record)
 
 getResource ()
 
- Protected Member Functions inherited from Monolog\Handler\AbstractProcessingHandler
 write (array $record)
 Writes the record down to the log of the implementing handler. More...
 
 processRecord (array $record)
 Processes a record. More...
 
- Protected Member Functions inherited from Monolog\Handler\AbstractHandler
 getDefaultFormatter ()
 Gets the default formatter. More...
 

Private Member Functions

 buildContent ($record)
 Builds the body of API call. More...
 
 buildHeader ($content)
 Builds the header of the API Call. More...
 

Private Attributes

 $token
 
 $channel
 
 $username
 
 $iconEmoji
 
 $useAttachment
 
 $useShortAttachment
 
 $includeContextAndExtra
 
 $lineFormatter
 

Additional Inherited Members

- Protected Attributes inherited from Monolog\Handler\AbstractHandler
 $level = Logger::DEBUG
 
 $bubble = true
 
 $formatter
 
 $processors = array()
 

Detailed Description

Sends notifications through Slack API.

Author
Greg Kedzierski greg@.nosp@m.greg.nosp@m.kedzi.nosp@m.ersk.nosp@m.i.com
See also
https://api.slack.com/

Definition at line 23 of file SlackHandler.php.

Constructor & Destructor Documentation

◆ __construct()

Monolog\Handler\SlackHandler::__construct (   $token,
  $channel,
  $username = 'Monolog',
  $useAttachment = true,
  $iconEmoji = null,
  $level = Logger::CRITICAL,
  $bubble = true,
  $useShortAttachment = false,
  $includeContextAndExtra = false 
)
Parameters
string$tokenSlack API token
string$channelSlack channel (encoded ID or name)
string$usernameName of a bot
bool$useAttachmentWhether the message should be added to Slack as attachment (plain text otherwise)
string | null$iconEmojiThe emoji name to use (or null)
int$levelThe minimum logging level at which this handler will be triggered
bool$bubbleWhether the messages that are handled can bubble up the stack or not
bool$useShortAttachmentWhether the the context/extra messages added to Slack as attachments are in a short style
bool$includeContextAndExtraWhether the attachment should include context and extra data
Exceptions
MissingExtensionExceptionIf no OpenSSL PHP extension configured

Definition at line 84 of file SlackHandler.php.

References Monolog\Handler\AbstractHandler\$bubble, Monolog\Handler\SlackHandler\$channel, Monolog\Handler\SlackHandler\$iconEmoji, Monolog\Handler\SlackHandler\$includeContextAndExtra, Monolog\Handler\AbstractHandler\$level, Monolog\Handler\SlackHandler\$token, Monolog\Handler\SlackHandler\$useAttachment, Monolog\Handler\SlackHandler\$username, and Monolog\Handler\SlackHandler\$useShortAttachment.

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  }

Member Function Documentation

◆ buildContent()

Monolog\Handler\SlackHandler::buildContent (   $record)
private

Builds the body of API call.

Parameters
array$record
Returns
string

Definition at line 124 of file SlackHandler.php.

References $dataArray, and Monolog\Handler\SlackHandler\prepareContentData().

Referenced by Monolog\Handler\SlackHandler\generateDataStream().

125  {
126  $dataArray = $this->prepareContentData($record);
127 
128  return http_build_query($dataArray);
129  }
prepareContentData($record)
Prepares content data.
$dataArray
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ buildHeader()

Monolog\Handler\SlackHandler::buildHeader (   $content)
private

Builds the header of the API Call.

Parameters
string$content
Returns
string

Definition at line 231 of file SlackHandler.php.

References $header.

Referenced by Monolog\Handler\SlackHandler\generateDataStream().

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  }
+ Here is the caller graph for this function:

◆ generateDataStream()

Monolog\Handler\SlackHandler::generateDataStream (   $record)
protected

{}

Parameters
array$record
Returns
string

Definition at line 111 of file SlackHandler.php.

References Monolog\Handler\SlackHandler\buildContent(), and Monolog\Handler\SlackHandler\buildHeader().

112  {
113  $content = $this->buildContent($record);
114 
115  return $this->buildHeader($content) . $content;
116  }
buildContent($record)
Builds the body of API call.
buildHeader($content)
Builds the header of the API Call.
+ Here is the call graph for this function:

◆ getAttachmentColor()

Monolog\Handler\SlackHandler::getAttachmentColor (   $level)
protected

Returned a Slack message attachment color associated with provided level.

Parameters
int$level
Returns
string

Definition at line 264 of file SlackHandler.php.

References Monolog\Handler\AbstractHandler\$level, Monolog\Logger\ERROR, Monolog\Logger\INFO, and Monolog\Logger\WARNING.

Referenced by Monolog\Handler\SlackHandler\prepareContentData().

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  }
const ERROR
Runtime errors.
Definition: Logger.php:57
const WARNING
Exceptional occurrences that are not errors.
Definition: Logger.php:52
const INFO
Interesting events.
Definition: Logger.php:39
+ Here is the caller graph for this function:

◆ prepareContentData()

Monolog\Handler\SlackHandler::prepareContentData (   $record)
protected

Prepares content data.

Parameters
array$record
Returns
array

Definition at line 137 of file SlackHandler.php.

References $dataArray, $message, array, Monolog\Handler\SlackHandler\getAttachmentColor(), and Monolog\Handler\SlackHandler\stringify().

Referenced by Monolog\Handler\SlackHandler\buildContent().

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  }
getAttachmentColor($level)
Returned a Slack message attachment color associated with provided level.
$dataArray
catch(Exception $e) $message
Create styles array
The data for the language used.
stringify($fields)
Stringifies an array of key/value pairs to be used in attachment fields.
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ stringify()

Monolog\Handler\SlackHandler::stringify (   $fields)
protected

Stringifies an array of key/value pairs to be used in attachment fields.

Parameters
array$fields
Returns
string

Definition at line 284 of file SlackHandler.php.

Referenced by Monolog\Handler\SlackHandler\prepareContentData().

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  }
+ Here is the caller graph for this function:

◆ write()

Monolog\Handler\SlackHandler::write ( array  $record)
protected

{}

Parameters
array$record

Definition at line 247 of file SlackHandler.php.

References $res, Monolog\Handler\SocketHandler\closeSocket(), and Monolog\Handler\SocketHandler\getResource().

248  {
249  parent::write($record);
250  $res = $this->getResource();
251  if (is_resource($res)) {
252  @fread($res, 2048);
253  }
254  $this->closeSocket();
255  }
foreach($_POST as $key=> $value) $res
closeSocket()
Close socket, if open.
+ Here is the call graph for this function:

Field Documentation

◆ $channel

Monolog\Handler\SlackHandler::$channel
private

Definition at line 35 of file SlackHandler.php.

Referenced by Monolog\Handler\SlackHandler\__construct().

◆ $iconEmoji

Monolog\Handler\SlackHandler::$iconEmoji
private

Definition at line 47 of file SlackHandler.php.

Referenced by Monolog\Handler\SlackHandler\__construct().

◆ $includeContextAndExtra

Monolog\Handler\SlackHandler::$includeContextAndExtra
private

Definition at line 65 of file SlackHandler.php.

Referenced by Monolog\Handler\SlackHandler\__construct().

◆ $lineFormatter

Monolog\Handler\SlackHandler::$lineFormatter
private

Definition at line 70 of file SlackHandler.php.

◆ $token

Monolog\Handler\SlackHandler::$token
private

Definition at line 29 of file SlackHandler.php.

Referenced by Monolog\Handler\SlackHandler\__construct().

◆ $useAttachment

Monolog\Handler\SlackHandler::$useAttachment
private

Definition at line 53 of file SlackHandler.php.

Referenced by Monolog\Handler\SlackHandler\__construct().

◆ $username

Monolog\Handler\SlackHandler::$username
private

Definition at line 41 of file SlackHandler.php.

Referenced by Monolog\Handler\SlackHandler\__construct().

◆ $useShortAttachment

Monolog\Handler\SlackHandler::$useShortAttachment
private

Definition at line 59 of file SlackHandler.php.

Referenced by Monolog\Handler\SlackHandler\__construct().


The documentation for this class was generated from the following file: