1
0
mirror of https://github.com/Seldaek/monolog.git synced 2025-08-11 07:34:12 +02:00

Remove code duplication, refs #435

This commit is contained in:
Jordi Boggiano
2014-12-28 16:28:20 +00:00
parent 33ebc242eb
commit 3e1b8a7afe
3 changed files with 25 additions and 62 deletions

View File

@@ -73,7 +73,7 @@ class LineFormatter extends NormalizerFormatter
foreach ($vars['extra'] as $var => $val) { foreach ($vars['extra'] as $var => $val) {
if (false !== strpos($output, '%extra.'.$var.'%')) { if (false !== strpos($output, '%extra.'.$var.'%')) {
$output = str_replace('%extra.'.$var.'%', $this->replaceNewlines($this->convertToString($val)), $output); $output = str_replace('%extra.'.$var.'%', $this->stringify($val), $output);
unset($vars['extra'][$var]); unset($vars['extra'][$var]);
} }
} }
@@ -92,7 +92,7 @@ class LineFormatter extends NormalizerFormatter
foreach ($vars as $var => $val) { foreach ($vars as $var => $val) {
if (false !== strpos($output, '%'.$var.'%')) { if (false !== strpos($output, '%'.$var.'%')) {
$output = str_replace('%'.$var.'%', $this->replaceNewlines($this->convertToString($val)), $output); $output = str_replace('%'.$var.'%', $this->stringify($val), $output);
} }
} }
@@ -109,6 +109,11 @@ class LineFormatter extends NormalizerFormatter
return $message; return $message;
} }
public function stringify($value)
{
return $this->replaceNewlines($this->convertToString($value));
}
protected function normalizeException(Exception $e) protected function normalizeException(Exception $e)
{ {
$previousText = ''; $previousText = '';

View File

@@ -12,6 +12,7 @@
namespace Monolog\Handler; namespace Monolog\Handler;
use Monolog\Logger; use Monolog\Logger;
use Monolog\Formatter\LineFormatter;
/** /**
* Sends notifications through Slack API * Sends notifications through Slack API
@@ -63,6 +64,11 @@ class SlackHandler extends SocketHandler
*/ */
private $includeExtra; private $includeExtra;
/**
* @var LineFormatter
*/
private $lineFormatter;
/** /**
* @param string $token Slack API token * @param string $token Slack API token
* @param string $channel Slack channel (encoded ID or name) * @param string $channel Slack channel (encoded ID or name)
@@ -87,6 +93,9 @@ class SlackHandler extends SocketHandler
$this->useAttachment = $useAttachment; $this->useAttachment = $useAttachment;
$this->useShortAttachment = $useShortAttachment; $this->useShortAttachment = $useShortAttachment;
$this->includeExtra = $includeExtra; $this->includeExtra = $includeExtra;
if ($this->includeExtra) {
$this->lineFormatter = new LineFormatter;
}
} }
/** /**
@@ -118,15 +127,7 @@ class SlackHandler extends SocketHandler
'attachments' => array() 'attachments' => array()
); );
$extra = '';
foreach ($record['extra'] as $var => $val) {
$extra .= $var.': '.$this->replaceNewlines($this->convertToString($val))." | ";
}
$extra = rtrim($extra, " |");
if ($this->useAttachment) { if ($this->useAttachment) {
$attachment = array( $attachment = array(
'fallback' => $record['message'], 'fallback' => $record['message'],
'color' => $this->getAttachmentColor($record['level']) 'color' => $this->getAttachmentColor($record['level'])
@@ -156,6 +157,13 @@ class SlackHandler extends SocketHandler
} }
if ($this->includeExtra) { if ($this->includeExtra) {
$extra = '';
foreach ($record['extra'] as $var => $val) {
$extra .= $var.': '.$this->lineFormatter->stringify($val)." | ";
}
$extra = rtrim($extra, " |");
$attachment['fields'][] = array( $attachment['fields'][] = array(
'title' => "Extra", 'title' => "Extra",
'value' => $extra, 'value' => $extra,
@@ -223,54 +231,4 @@ class SlackHandler extends SocketHandler
return '#e3e4e6'; return '#e3e4e6';
} }
} }
/**
* Copy from LineFormater (any better idea?)
*/
protected function convertToString($data)
{
if (null === $data || is_bool($data)) {
return var_export($data, true);
}
if (is_scalar($data)) {
return (string) $data;
}
if (version_compare(PHP_VERSION, '5.4.0', '>=')) {
return $this->toJson($data, true);
}
return str_replace('\\/', '/', @json_encode($data));
}
/**
* Copy from LineFormater (any better idea?)
*/
protected function toJson($data, $ignoreErrors = false)
{
// suppress json_encode errors since it's twitchy with some inputs
if ($ignoreErrors) {
if (version_compare(PHP_VERSION, '5.4.0', '>=')) {
return @json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
}
return @json_encode($data);
}
if (version_compare(PHP_VERSION, '5.4.0', '>=')) {
return json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
}
return json_encode($data);
}
/**
* Copy from LineFormater (any better idea?)
*/
protected function replaceNewlines($str)
{
return strtr($str, array("\r\n" => ' ', "\r" => ' ', "\n" => ' '));
}
} }

View File

@@ -104,9 +104,9 @@ class SlackHandlerTest extends TestCase
); );
} }
private function createHandler($token = 'myToken', $channel = 'channel1', $username = 'Monolog', $useAttachment = true, $iconEmoji = null) private function createHandler($token = 'myToken', $channel = 'channel1', $username = 'Monolog', $useAttachment = true, $iconEmoji = null, $useShortAttachment = false, $includeExtra = false)
{ {
$constructorArgs = array($token, $channel, $username, $useAttachment, $iconEmoji, Logger::DEBUG, true); $constructorArgs = array($token, $channel, $username, $useAttachment, $iconEmoji, Logger::DEBUG, true, $useShortAttachment, $includeExtra);
$this->res = fopen('php://memory', 'a'); $this->res = fopen('php://memory', 'a');
$this->handler = $this->getMock( $this->handler = $this->getMock(
'\Monolog\Handler\SlackHandler', '\Monolog\Handler\SlackHandler',