mirror of
https://github.com/Seldaek/monolog.git
synced 2025-08-09 06:36:46 +02:00
Add a Slack Webhooks handler using the same SlackRecord util
This commit is contained in:
96
src/Monolog/Handler/SlackWebhookHandler.php
Normal file
96
src/Monolog/Handler/SlackWebhookHandler.php
Normal file
@@ -0,0 +1,96 @@
|
||||
<?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;
|
||||
use Monolog\Formatter\LineFormatter;
|
||||
use Monolog\Handler\Slack\SlackRecord;
|
||||
|
||||
/**
|
||||
* Sends notifications through Slack Webhooks
|
||||
*
|
||||
* @author Haralan Dobrev <hkdobrev@gmail.com>
|
||||
* @see https://api.slack.com/incoming-webhooks
|
||||
*/
|
||||
class SlackWebhookHandler extends AbstractProcessingHandler
|
||||
{
|
||||
/**
|
||||
* Slack Webhook token
|
||||
* @var string
|
||||
*/
|
||||
private $webhookUrl;
|
||||
|
||||
/**
|
||||
* Instance of the SlackRecord util class preparing data for Slack API.
|
||||
* @var SlackRecord
|
||||
*/
|
||||
private $slackRecord;
|
||||
|
||||
/**
|
||||
* @param string $webhookUrl Slack Webhook URL
|
||||
* @param string|null $channel Slack channel (encoded ID or name)
|
||||
* @param string $username Name of a bot
|
||||
* @param bool $useAttachment Whether the message should be added to Slack as attachment (plain text otherwise)
|
||||
* @param string|null $iconEmoji The emoji name to use (or null)
|
||||
* @param bool $useShortAttachment Whether the the context/extra messages added to Slack as attachments are in a short style
|
||||
* @param bool $includeContextAndExtra Whether the attachment should include context and extra data
|
||||
* @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(
|
||||
$webhookUrl,
|
||||
$channel = null,
|
||||
$username = 'Monolog',
|
||||
$useAttachment = true,
|
||||
$iconEmoji = null,
|
||||
$useShortAttachment = false,
|
||||
$includeContextAndExtra = false,
|
||||
$level = Logger::CRITICAL,
|
||||
$bubble = true
|
||||
) {
|
||||
parent::__construct($level, $bubble);
|
||||
|
||||
$this->webhookUrl = $webhookUrl;
|
||||
|
||||
$this->slackRecord = new SlackRecord(
|
||||
$channel,
|
||||
$username,
|
||||
$useAttachment,
|
||||
$iconEmoji,
|
||||
$useShortAttachment,
|
||||
$includeContextAndExtra,
|
||||
$this->formatter
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @param array $record
|
||||
*/
|
||||
protected function write(array $record)
|
||||
{
|
||||
$postData = $this->slackRecord->getSlackData($record);
|
||||
$postString = json_encode($postData);
|
||||
|
||||
$ch = curl_init();
|
||||
curl_setopt($ch, CURLOPT_URL, $this->webhookUrl);
|
||||
curl_setopt($ch, CURLOPT_POST, true);
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||
if (defined('CURLOPT_SAFE_UPLOAD')) {
|
||||
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, true);
|
||||
}
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, array('payload' => $postString));
|
||||
|
||||
Curl\Util::execute($ch);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user