mirror of
https://github.com/Seldaek/monolog.git
synced 2025-10-19 23:56:17 +02:00
Merge branch '1.x'
This commit is contained in:
@@ -11,8 +11,9 @@
|
||||
|
||||
namespace Monolog\Handler;
|
||||
|
||||
use Monolog\Formatter\FormatterInterface;
|
||||
use Monolog\Logger;
|
||||
use Monolog\Formatter\LineFormatter;
|
||||
use Monolog\Handler\Slack\SlackRecord;
|
||||
|
||||
/**
|
||||
* Sends notifications through Slack API
|
||||
@@ -29,45 +30,10 @@ class SlackHandler extends SocketHandler
|
||||
private $token;
|
||||
|
||||
/**
|
||||
* Slack channel (encoded ID or name)
|
||||
* @var string
|
||||
* Instance of the SlackRecord util class preparing data for Slack API.
|
||||
* @var SlackRecord
|
||||
*/
|
||||
private $channel;
|
||||
|
||||
/**
|
||||
* Name of a bot
|
||||
* @var string
|
||||
*/
|
||||
private $username;
|
||||
|
||||
/**
|
||||
* Emoji icon name
|
||||
* @var string
|
||||
*/
|
||||
private $iconEmoji;
|
||||
|
||||
/**
|
||||
* Whether the message should be added to Slack as attachment (plain text otherwise)
|
||||
* @var bool
|
||||
*/
|
||||
private $useAttachment;
|
||||
|
||||
/**
|
||||
* Whether the the context/extra messages added to Slack as attachments are in a short style
|
||||
* @var bool
|
||||
*/
|
||||
private $useShortAttachment;
|
||||
|
||||
/**
|
||||
* Whether the attachment should include context and extra data
|
||||
* @var bool
|
||||
*/
|
||||
private $includeContextAndExtra;
|
||||
|
||||
/**
|
||||
* @var LineFormatter
|
||||
*/
|
||||
private $lineFormatter;
|
||||
private $slackRecord;
|
||||
|
||||
/**
|
||||
* @param string $token Slack API token
|
||||
@@ -79,10 +45,9 @@ class SlackHandler extends SocketHandler
|
||||
* @param bool $bubble Whether the messages that are handled can bubble up the stack or not
|
||||
* @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 bool $printSimpleMessage Whether the attachment should include only the message without extradata
|
||||
* @throws MissingExtensionException If no OpenSSL PHP extension configured
|
||||
*/
|
||||
public function __construct($token, $channel, $username = 'Monolog', $useAttachment = true, $iconEmoji = null, $level = Logger::CRITICAL, $bubble = true, $useShortAttachment = false, $includeContextAndExtra = false, $printSimpleMessage = false)
|
||||
public function __construct($token, $channel, $username = 'Monolog', $useAttachment = true, $iconEmoji = null, $level = Logger::CRITICAL, $bubble = true, $useShortAttachment = false, $includeContextAndExtra = false)
|
||||
{
|
||||
if (!extension_loaded('openssl')) {
|
||||
throw new MissingExtensionException('The OpenSSL PHP extension is required to use the SlackHandler');
|
||||
@@ -90,17 +55,22 @@ class SlackHandler extends SocketHandler
|
||||
|
||||
parent::__construct('ssl://slack.com:443', $level, $bubble);
|
||||
|
||||
$this->token = $token;
|
||||
$this->channel = $channel;
|
||||
$this->username = $username;
|
||||
$this->iconEmoji = is_string($iconEmoji) ? trim($iconEmoji, ':') : null;
|
||||
$this->useAttachment = $useAttachment;
|
||||
$this->useShortAttachment = $useShortAttachment;
|
||||
$this->includeContextAndExtra = $includeContextAndExtra;
|
||||
$this->slackRecord = new SlackRecord(
|
||||
$channel,
|
||||
$username,
|
||||
$useAttachment,
|
||||
$iconEmoji,
|
||||
$useShortAttachment,
|
||||
$includeContextAndExtra,
|
||||
$this->formatter
|
||||
);
|
||||
|
||||
if ($this->includeContextAndExtra && $this->useShortAttachment) {
|
||||
$this->lineFormatter = new LineFormatter;
|
||||
}
|
||||
$this->token = $token;
|
||||
}
|
||||
|
||||
public function getSlackRecord()
|
||||
{
|
||||
return $this->slackRecord;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -137,87 +107,11 @@ class SlackHandler extends SocketHandler
|
||||
*/
|
||||
protected function prepareContentData($record)
|
||||
{
|
||||
$dataArray = [
|
||||
'token' => $this->token,
|
||||
'channel' => $this->channel,
|
||||
'username' => $this->username,
|
||||
'text' => '',
|
||||
'attachments' => [],
|
||||
];
|
||||
$dataArray = $this->slackRecord->getSlackData($record);
|
||||
$dataArray['token'] = $this->token;
|
||||
|
||||
if ($this->formatter && !$printSimpleMessage) {
|
||||
$message = $this->formatter->format($record);
|
||||
} else {
|
||||
$message = $record['message'];
|
||||
}
|
||||
|
||||
if ($this->useAttachment) {
|
||||
$attachment = [
|
||||
'fallback' => $message,
|
||||
'color' => $this->getAttachmentColor($record['level']),
|
||||
'fields' => [],
|
||||
];
|
||||
|
||||
if ($this->useShortAttachment) {
|
||||
$attachment['title'] = $record['level_name'];
|
||||
$attachment['text'] = $message;
|
||||
} else {
|
||||
$attachment['title'] = 'Message';
|
||||
$attachment['text'] = $message;
|
||||
$attachment['fields'][] = [
|
||||
'title' => 'Level',
|
||||
'value' => $record['level_name'],
|
||||
'short' => true,
|
||||
];
|
||||
}
|
||||
|
||||
if ($this->includeContextAndExtra) {
|
||||
if (!empty($record['extra'])) {
|
||||
if ($this->useShortAttachment) {
|
||||
$attachment['fields'][] = [
|
||||
'title' => "Extra",
|
||||
'value' => $this->stringify($record['extra']),
|
||||
'short' => $this->useShortAttachment,
|
||||
];
|
||||
} else {
|
||||
// Add all extra fields as individual fields in attachment
|
||||
foreach ($record['extra'] as $var => $val) {
|
||||
$attachment['fields'][] = [
|
||||
'title' => $var,
|
||||
'value' => $val,
|
||||
'short' => $this->useShortAttachment,
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!empty($record['context'])) {
|
||||
if ($this->useShortAttachment) {
|
||||
$attachment['fields'][] = [
|
||||
'title' => "Context",
|
||||
'value' => $this->stringify($record['context']),
|
||||
'short' => $this->useShortAttachment,
|
||||
];
|
||||
} else {
|
||||
// Add all context fields as individual fields in attachment
|
||||
foreach ($record['context'] as $var => $val) {
|
||||
$attachment['fields'][] = [
|
||||
'title' => $var,
|
||||
'value' => $val,
|
||||
'short' => $this->useShortAttachment,
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$dataArray['attachments'] = json_encode([$attachment]);
|
||||
} else {
|
||||
$dataArray['text'] = $message;
|
||||
}
|
||||
|
||||
if ($this->iconEmoji) {
|
||||
$dataArray['icon_emoji'] = ":{$this->iconEmoji}:";
|
||||
if (!empty($dataArray['attachments'])) {
|
||||
$dataArray['attachments'] = json_encode($dataArray['attachments']);
|
||||
}
|
||||
|
||||
return $dataArray;
|
||||
@@ -261,19 +155,16 @@ class SlackHandler extends SocketHandler
|
||||
*
|
||||
* @param int $level
|
||||
* @return string
|
||||
* @deprecated Use underlying SlackRecord instead
|
||||
*/
|
||||
protected function getAttachmentColor($level)
|
||||
{
|
||||
switch (true) {
|
||||
case $level >= Logger::ERROR:
|
||||
return 'danger';
|
||||
case $level >= Logger::WARNING:
|
||||
return 'warning';
|
||||
case $level >= Logger::INFO:
|
||||
return 'good';
|
||||
default:
|
||||
return '#e3e4e6';
|
||||
}
|
||||
trigger_error(
|
||||
'SlackHandler::getAttachmentColor() is deprecated. Use underlying SlackRecord instead.',
|
||||
E_USER_DEPRECATED
|
||||
);
|
||||
|
||||
return $this->slackRecord->getAttachmentColor($level);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -281,16 +172,31 @@ class SlackHandler extends SocketHandler
|
||||
*
|
||||
* @param array $fields
|
||||
* @return string
|
||||
* @deprecated Use underlying SlackRecord instead
|
||||
*/
|
||||
protected function stringify($fields)
|
||||
{
|
||||
$string = '';
|
||||
foreach ($fields as $var => $val) {
|
||||
$string .= $var.': '.$this->lineFormatter->stringify($val)." | ";
|
||||
}
|
||||
trigger_error(
|
||||
'SlackHandler::stringify() is deprecated. Use underlying SlackRecord instead.',
|
||||
E_USER_DEPRECATED
|
||||
);
|
||||
|
||||
$string = rtrim($string, " |");
|
||||
return $this->slackRecord->stringify($fields);
|
||||
}
|
||||
|
||||
return $string;
|
||||
public function setFormatter(FormatterInterface $formatter)
|
||||
{
|
||||
parent::setFormatter($formatter);
|
||||
$this->slackRecord->setFormatter($formatter);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getFormatter()
|
||||
{
|
||||
$formatter = parent::getFormatter();
|
||||
$this->slackRecord->setFormatter($formatter);
|
||||
|
||||
return $formatter;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user