$level
$level :
Sends notifications through Slack's Slackbot
Classes extending it should (in most cases) only implement write($record)
$formatter : \Monolog\Formatter\FormatterInterface
__construct(string $slackTeam, string $token, string $channel, integer $level = \Monolog\Logger::CRITICAL, boolean $bubble = true)
string | $slackTeam | Slack team slug |
string | $token | Slackbot token |
string | $channel | Slack channel (encoded ID or name) |
integer | $level | The minimum logging level at which this handler will be triggered |
boolean | $bubble | Whether the messages that are handled can bubble up the stack or not |
isHandling(array $record) : Boolean
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.
array | $record | Partial log record containing only a level key |
setFormatter(\Monolog\Formatter\FormatterInterface $formatter) : self
Sets the formatter.
\Monolog\Formatter\FormatterInterface | $formatter |
getFormatter() : \Monolog\Formatter\FormatterInterface
Gets the formatter.
getDefaultFormatter() : \Monolog\Formatter\FormatterInterface
Gets the default formatter.
<?php
/*
* This file is part of the Monolog package.
*
* (c) Jordi Boggiano <j.boggiano@seld.be>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Monolog\Handler;
use Monolog\Logger;
/**
* Sends notifications through Slack's Slackbot
*
* @author Haralan Dobrev <hkdobrev@gmail.com>
* @see https://slack.com/apps/A0F81R8ET-slackbot
*/
class SlackbotHandler extends AbstractProcessingHandler
{
/**
* The slug of the Slack team
* @var string
*/
private $slackTeam;
/**
* Slackbot token
* @var string
*/
private $token;
/**
* Slack channel name
* @var string
*/
private $channel;
/**
* @param string $slackTeam Slack team slug
* @param string $token Slackbot token
* @param string $channel Slack channel (encoded ID or name)
* @param int $level The minimum logging level at which this handler will be triggered
* @param bool $bubble Whether the messages that are handled can bubble up the stack or not
*/
public function __construct($slackTeam, $token, $channel, $level = Logger::CRITICAL, $bubble = true)
{
parent::__construct($level, $bubble);
$this->slackTeam = $slackTeam;
$this->token = $token;
$this->channel = $channel;
}
/**
* {@inheritdoc}
*
* @param array $record
*/
protected function write(array $record)
{
$slackbotUrl = sprintf(
'https://%s.slack.com/services/hooks/slackbot?token=%s&channel=%s',
$this->slackTeam,
$this->token,
$this->channel
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $slackbotUrl);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $record['message']);
Curl\Util::execute($ch);
}
}