SlackHandler
extends SocketHandler
in package
Sends notifications through Slack API
Tags
Table of Contents
Properties
- $bubble : bool
- $formatter : FormatterInterface|null
- $level : int
- $processors : array<string|int, callable>
- $slackRecord : SlackRecord
- Instance of the SlackRecord util class preparing data for Slack API.
- $token : string
- Slack API token
Methods
- __construct() : mixed
- __destruct() : mixed
- __sleep() : mixed
- close() : void
- We will not close a PersistentSocket instance so it can be reused in other requests.
- closeSocket() : void
- Close socket, if open
- excludeFields() : self
- getBubble() : bool
- Gets the bubbling behavior.
- getChunkSize() : int|null
- Get current chunk size
- getConnectionString() : string
- Get current connection string
- getConnectionTimeout() : float
- Get current connection timeout setting
- getFormatter() : FormatterInterface
- Gets the formatter.
- getLevel() : int
- Gets minimum logging level at which this handler will be triggered.
- getSlackRecord() : SlackRecord
- getTimeout() : float
- Get current in-transfer timeout
- getToken() : string
- getWritingTimeout() : float
- Get current local writing timeout
- handle() : bool
- Handles a record.
- handleBatch() : void
- Handles a set of records at once.
- includeContextAndExtra() : self
- isConnected() : bool
- Check to see if the socket is currently available.
- isHandling() : bool
- Checks whether the given record will be handled by this handler.
- isPersistent() : bool
- Get persistent setting
- popProcessor() : callable
- {@inheritDoc}
- pushProcessor() : HandlerInterface
- {@inheritDoc}
- reset() : void
- setBubble() : self
- Sets the bubbling behavior.
- setChannel() : self
- Channel used by the bot when posting
- setChunkSize() : self
- Set chunk size. Only has effect during connection in the writing cycle.
- setConnectionTimeout() : self
- Set connection timeout. Only has effect before we connect.
- setFormatter() : HandlerInterface
- Sets the formatter.
- setIconEmoji() : self
- setLevel() : self
- Sets minimum logging level at which this handler will be triggered.
- setPersistent() : self
- Set socket connection to be persistent. It only has effect before the connection is initiated.
- setTimeout() : self
- Set write timeout. Only has effect before we connect.
- setUsername() : self
- Username used by the bot when posting
- setWritingTimeout() : self
- Set writing timeout. Only has effect during connection in the writing cycle.
- useAttachment() : self
- useShortAttachment() : self
- finalizeWrite() : void
- Finalizes the request by reading some bytes and then closing the socket
- fsockopen() : resource|false
- Wrapper to allow mocking
- fwrite() : int|bool
- Wrapper to allow mocking
- generateDataStream() : string
- getDefaultFormatter() : FormatterInterface
- Gets the default formatter.
- getResource() : resource|null
- pfsockopen() : resource|false
- Wrapper to allow mocking
- prepareContentData() : array<string|int, string>
- processRecord() : array<string|int, mixed>
- Processes a record.
- resetProcessors() : void
- streamGetMetadata() : array<string|int, mixed>|bool
- Wrapper to allow mocking
- streamSetChunkSize() : int|bool
- Wrapper to allow mocking
- streamSetTimeout() : bool
- Wrapper to allow mocking
- write() : void
- Connect (if necessary) and write to the socket
- buildContent() : string
- Builds the body of API call
- buildHeader() : string
- Builds the header of the API Call
Properties
$bubble
protected
bool
$bubble
= true
$formatter
protected
FormatterInterface|null
$formatter
$level
protected
int
$level
= \Monolog\Logger::DEBUG
Tags
$processors
protected
array<string|int, callable>
$processors
= []
Tags
$slackRecord
Instance of the SlackRecord util class preparing data for Slack API.
private
SlackRecord
$slackRecord
$token
Slack API token
private
string
$token
Methods
__construct()
public
__construct(string $token, string $channel[, string|null $username = null ][, bool $useAttachment = true ][, string|null $iconEmoji = null ][, mixed $level = Logger::CRITICAL ][, bool $bubble = true ][, bool $useShortAttachment = false ][, bool $includeContextAndExtra = false ][, array<string|int, string> $excludeFields = array() ][, bool $persistent = false ][, float $timeout = 0.0 ][, float $writingTimeout = 10.0 ][, float|null $connectionTimeout = null ][, int|null $chunkSize = null ]) : mixed
Parameters
- $token : string
-
Slack API token
- $channel : string
-
Slack channel (encoded ID or name)
- $username : string|null = null
-
Name of a bot
- $useAttachment : bool = true
-
Whether the message should be added to Slack as attachment (plain text otherwise)
- $iconEmoji : string|null = null
-
The emoji name to use (or null)
- $level : mixed = Logger::CRITICAL
-
The minimum logging level at which this handler will be triggered
- $bubble : bool = true
-
Whether the messages that are handled can bubble up the stack or not
- $useShortAttachment : bool = false
-
Whether the context/extra messages added to Slack as attachments are in a short style
- $includeContextAndExtra : bool = false
-
Whether the attachment should include context and extra data
- $excludeFields : array<string|int, string> = array()
-
Dot separated list of fields to exclude from slack message. E.g. ['context.field1', 'extra.field2']
- $persistent : bool = false
-
Flag to enable/disable persistent connections
- $timeout : float = 0.0
-
Socket timeout to wait until the request is being aborted
- $writingTimeout : float = 10.0
-
Socket timeout to wait until the request should've been sent/written
- $connectionTimeout : float|null = null
-
Socket connect timeout to wait until the connection should've been established
- $chunkSize : int|null = null
-
Sets the chunk size. Only has effect during connection in the writing cycle
Tags
__destruct()
public
__destruct() : mixed
__sleep()
public
__sleep() : mixed
close()
We will not close a PersistentSocket instance so it can be reused in other requests.
public
close() : void
closeSocket()
Close socket, if open
public
closeSocket() : void
excludeFields()
public
excludeFields(array<string|int, string> $excludeFields) : self
Parameters
- $excludeFields : array<string|int, string>
Return values
selfgetBubble()
Gets the bubbling behavior.
public
getBubble() : bool
Return values
bool —true means that this handler allows bubbling. false means that bubbling is not permitted.
getChunkSize()
Get current chunk size
public
getChunkSize() : int|null
Return values
int|nullgetConnectionString()
Get current connection string
public
getConnectionString() : string
Return values
stringgetConnectionTimeout()
Get current connection timeout setting
public
getConnectionTimeout() : float
Return values
floatgetFormatter()
Gets the formatter.
public
getFormatter() : FormatterInterface
Return values
FormatterInterfacegetLevel()
Gets minimum logging level at which this handler will be triggered.
public
getLevel() : int
Tags
Return values
intgetSlackRecord()
public
getSlackRecord() : SlackRecord
Return values
SlackRecordgetTimeout()
Get current in-transfer timeout
public
getTimeout() : float
Return values
floatgetToken()
public
getToken() : string
Return values
stringgetWritingTimeout()
Get current local writing timeout
public
getWritingTimeout() : float
Return values
floathandle()
Handles a record.
public
handle(array<string|int, mixed> $record) : bool
Parameters
- $record : array<string|int, mixed>
-
The record to handle
Return values
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.
handleBatch()
Handles a set of records at once.
public
handleBatch(array<string|int, mixed> $records) : void
Parameters
- $records : array<string|int, mixed>
-
The records to handle (an array of record arrays)
includeContextAndExtra()
public
includeContextAndExtra(bool $includeContextAndExtra) : self
Parameters
- $includeContextAndExtra : bool
Return values
selfisConnected()
Check to see if the socket is currently available.
public
isConnected() : bool
UDP might appear to be connected but might fail when writing. See http://php.net/fsockopen for details.
Return values
boolisHandling()
Checks whether the given record will be handled by this handler.
public
isHandling(array<string|int, mixed> $record) : bool
Parameters
- $record : array<string|int, mixed>
-
Partial log record containing only a level key
Return values
boolisPersistent()
Get persistent setting
public
isPersistent() : bool
Return values
boolpopProcessor()
{@inheritDoc}
public
popProcessor() : callable
Return values
callablepushProcessor()
{@inheritDoc}
public
pushProcessor(callable $callback) : HandlerInterface
Parameters
- $callback : callable
Return values
HandlerInterfacereset()
public
reset() : void
setBubble()
Sets the bubbling behavior.
public
setBubble(bool $bubble) : self
Parameters
- $bubble : bool
-
true means that this handler allows bubbling. false means that bubbling is not permitted.
Return values
selfsetChannel()
Channel used by the bot when posting
public
setChannel(string $channel) : self
Parameters
- $channel : string
Return values
selfsetChunkSize()
Set chunk size. Only has effect during connection in the writing cycle.
public
setChunkSize(int $bytes) : self
Parameters
- $bytes : int
Return values
selfsetConnectionTimeout()
Set connection timeout. Only has effect before we connect.
public
setConnectionTimeout(float $seconds) : self
Parameters
- $seconds : float
Tags
Return values
selfsetFormatter()
Sets the formatter.
public
setFormatter(FormatterInterface $formatter) : HandlerInterface
Parameters
- $formatter : FormatterInterface
Return values
HandlerInterface —self
setIconEmoji()
public
setIconEmoji(string $iconEmoji) : self
Parameters
- $iconEmoji : string
Return values
selfsetLevel()
Sets minimum logging level at which this handler will be triggered.
public
setLevel(Level|LevelName|LogLevel::* $level) : self
Parameters
- $level : Level|LevelName|LogLevel::*
-
Level or level name
Return values
selfsetPersistent()
Set socket connection to be persistent. It only has effect before the connection is initiated.
public
setPersistent(bool $persistent) : self
Parameters
- $persistent : bool
Return values
selfsetTimeout()
Set write timeout. Only has effect before we connect.
public
setTimeout(float $seconds) : self
Parameters
- $seconds : float
Tags
Return values
selfsetUsername()
Username used by the bot when posting
public
setUsername(string $username) : self
Parameters
- $username : string
Return values
selfsetWritingTimeout()
Set writing timeout. Only has effect during connection in the writing cycle.
public
setWritingTimeout(float $seconds) : self
Parameters
- $seconds : float
-
0 for no timeout
Return values
selfuseAttachment()
public
useAttachment(bool $useAttachment) : self
Parameters
- $useAttachment : bool
Return values
selfuseShortAttachment()
public
useShortAttachment(bool $useShortAttachment) : self
Parameters
- $useShortAttachment : bool
Return values
selffinalizeWrite()
Finalizes the request by reading some bytes and then closing the socket
protected
finalizeWrite() : void
If we do not read some but close the socket too early, slack sometimes drops the request entirely.
fsockopen()
Wrapper to allow mocking
protected
fsockopen() : resource|false
Return values
resource|falsefwrite()
Wrapper to allow mocking
protected
fwrite(string $data) : int|bool
Parameters
- $data : string
Return values
int|boolgenerateDataStream()
protected
generateDataStream(array<string|int, mixed> $record) : string
Parameters
- $record : array<string|int, mixed>
Return values
stringgetDefaultFormatter()
Gets the default formatter.
protected
getDefaultFormatter() : FormatterInterface
Overwrite this if the LineFormatter is not a good default for your handler.
Return values
FormatterInterfacegetResource()
protected
getResource() : resource|null
Return values
resource|nullpfsockopen()
Wrapper to allow mocking
protected
pfsockopen() : resource|false
Return values
resource|falseprepareContentData()
protected
prepareContentData(array<string|int, mixed> $record) : array<string|int, string>
Parameters
- $record : array<string|int, mixed>
Tags
Return values
array<string|int, string>processRecord()
Processes a record.
protected
processRecord(array<string|int, mixed> $record) : array<string|int, mixed>
Parameters
- $record : array<string|int, mixed>
Tags
Return values
array<string|int, mixed>resetProcessors()
protected
resetProcessors() : void
streamGetMetadata()
Wrapper to allow mocking
protected
streamGetMetadata() : array<string|int, mixed>|bool
Return values
array<string|int, mixed>|boolstreamSetChunkSize()
Wrapper to allow mocking
protected
streamSetChunkSize() : int|bool
Tags
Return values
int|boolstreamSetTimeout()
Wrapper to allow mocking
protected
streamSetTimeout() : bool
Tags
Return values
boolwrite()
Connect (if necessary) and write to the socket
protected
write(array<string|int, mixed> $record) : void
Parameters
- $record : array<string|int, mixed>
buildContent()
Builds the body of API call
private
buildContent(array<string|int, mixed> $record) : string
Parameters
- $record : array<string|int, mixed>
Tags
Return values
stringbuildHeader()
Builds the header of the API Call
private
buildHeader(string $content) : string
Parameters
- $content : string