ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
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=null, $useAttachment=true, $iconEmoji=null, $level=Logger::CRITICAL, $bubble=true, $useShortAttachment=false, $includeContextAndExtra=false, array $excludeFields=array())
 
 getSlackRecord ()
 
 getToken ()
 
 setFormatter (FormatterInterface $formatter)
 Sets the formatter. More...
 
 getFormatter ()
 Gets the formatter. More...
 
- 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...
 
 setChunkSize ($bytes)
 Set chunk size. 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...
 
 getChunkSize ()
 Get current chunk size. 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
bool 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
bool
} 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 ()
 
 reset ()
 

Protected Member Functions

 generateDataStream ($record)
 {} More...
 
 prepareContentData ($record)
 Prepares content data. More...
 
 write (array $record)
 {} More...
 
 finalizeWrite ()
 Finalizes the request by reading some bytes and then closing the socket. 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...
 
 streamSetChunkSize ()
 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
 
 $slackRecord
 

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

Constructor & Destructor Documentation

◆ __construct()

Monolog\Handler\SlackHandler::__construct (   $token,
  $channel,
  $username = null,
  $useAttachment = true,
  $iconEmoji = null,
  $level = Logger::CRITICAL,
  $bubble = true,
  $useShortAttachment = false,
  $includeContextAndExtra = false,
array  $excludeFields = array() 
)
Parameters
string$tokenSlack API token
string$channelSlack channel (encoded ID or name)
string | null$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
array$excludeFieldsDot separated list of fields to exclude from slack message. E.g. ['context.field1', 'extra.field2']
Exceptions
MissingExtensionExceptionIf no OpenSSL PHP extension configured

Definition at line 51 of file SlackHandler.php.

References Monolog\Handler\AbstractHandler\$bubble, Monolog\Handler\AbstractHandler\$level, and Monolog\Handler\SlackHandler\$token.

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  }

Member Function Documentation

◆ buildContent()

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

Builds the body of API call.

Parameters
array$record
Returns
string

Definition at line 102 of file SlackHandler.php.

References Monolog\Handler\SlackHandler\prepareContentData().

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

103  {
104  $dataArray = $this->prepareContentData($record);
105 
106  return http_build_query($dataArray);
107  }
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 133 of file SlackHandler.php.

References $header.

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

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

◆ finalizeWrite()

Monolog\Handler\SlackHandler::finalizeWrite ( )
protected

Finalizes the request by reading some bytes and then closing the socket.

If we do not read some but close the socket too early, slack sometimes drops the request entirely.

Definition at line 161 of file SlackHandler.php.

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

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

162  {
163  $res = $this->getResource();
164  if (is_resource($res)) {
165  @fread($res, 2048);
166  }
167  $this->closeSocket();
168  }
foreach($_POST as $key=> $value) $res
closeSocket()
Close socket, if open.
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ generateDataStream()

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

{}

Parameters
array$record
Returns
string

Definition at line 89 of file SlackHandler.php.

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

90  {
91  $content = $this->buildContent($record);
92 
93  return $this->buildHeader($content) . $content;
94  }
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
Deprecated:
Use underlying SlackRecord instead

Definition at line 178 of file SlackHandler.php.

References Monolog\Handler\AbstractHandler\$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  }

◆ getFormatter()

Monolog\Handler\SlackHandler::getFormatter ( )

Gets the formatter.

Returns
FormatterInterface

Implements Monolog\Handler\HandlerInterface.

Definition at line 213 of file SlackHandler.php.

References Monolog\Handler\AbstractHandler\$formatter.

214  {
215  $formatter = parent::getFormatter();
216  $this->slackRecord->setFormatter($formatter);
217 
218  return $formatter;
219  }

◆ getSlackRecord()

Monolog\Handler\SlackHandler::getSlackRecord ( )

Definition at line 73 of file SlackHandler.php.

References Monolog\Handler\SlackHandler\$slackRecord.

74  {
75  return $this->slackRecord;
76  }

◆ getToken()

Monolog\Handler\SlackHandler::getToken ( )

Definition at line 78 of file SlackHandler.php.

References Monolog\Handler\SlackHandler\$token.

79  {
80  return $this->token;
81  }

◆ prepareContentData()

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

Prepares content data.

Parameters
array$record
Returns
array

Definition at line 115 of file SlackHandler.php.

References Monolog\Handler\SlackHandler\$token.

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

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

◆ setFormatter()

Monolog\Handler\SlackHandler::setFormatter ( FormatterInterface  $formatter)

Sets the formatter.

Parameters
FormatterInterface$formatter
Returns
self

Implements Monolog\Handler\HandlerInterface.

Definition at line 205 of file SlackHandler.php.

206  {
207  parent::setFormatter($formatter);
208  $this->slackRecord->setFormatter($formatter);
209 
210  return $this;
211  }

◆ 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
Deprecated:
Use underlying SlackRecord instead

Definition at line 195 of file SlackHandler.php.

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  }

◆ write()

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

{}

Parameters
array$record

Definition at line 149 of file SlackHandler.php.

References Monolog\Handler\SlackHandler\finalizeWrite(), and Sabre\VObject\write().

150  {
151  parent::write($record);
152  $this->finalizeWrite();
153  }
finalizeWrite()
Finalizes the request by reading some bytes and then closing the socket.
+ Here is the call graph for this function:

Field Documentation

◆ $slackRecord

Monolog\Handler\SlackHandler::$slackRecord
private

Definition at line 36 of file SlackHandler.php.

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

◆ $token

Monolog\Handler\SlackHandler::$token
private

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