ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
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 ($boolean)
 Set socket connection to nbe persistent. More...
 
 setConnectionTimeout ($seconds)
 Set connection timeout. More...
 
 setTimeout ($seconds)
 Set write 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...
 
 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)
 
- 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

Definition at line 83 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.

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  }

Member Function Documentation

◆ buildContent()

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

Builds the body of API call.

Parameters
array$record
Returns
string

Definition at line 122 of file SlackHandler.php.

References Monolog\Handler\SlackHandler\prepareContentData().

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

123  {
124  $dataArray = $this->prepareContentData($record);
125 
126  return http_build_query($dataArray);
127  }
prepareContentData($record)
Prepares content data.
+ 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  }
$header
+ Here is the caller graph for this function:

◆ generateDataStream()

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

{}

Parameters
array$record
Returns
string

Definition at line 109 of file SlackHandler.php.

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

110  {
111  $content = $this->buildContent($record);
112 
113  return $this->buildHeader($content) . $content;
114  }
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 260 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().

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  }
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 135 of file SlackHandler.php.

References Monolog\Handler\SlackHandler\getAttachmentColor(), and Monolog\Handler\SlackHandler\stringify().

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

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  }
getAttachmentColor($level)
Returned a Slack message attachment color associated with provided level.
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$fieldsprotected
Returns
string

Definition at line 281 of file SlackHandler.php.

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

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  }
+ 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 Monolog\Handler\SocketHandler\closeSocket().

248  {
249  parent::write($record);
250  $this->closeSocket();
251  }
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: