|
|
@@ -27,6 +27,16 @@ use Monolog\Logger;
|
|
|
*/
|
|
|
class HipChatHandler extends SocketHandler
|
|
|
{
|
|
|
+ /**
|
|
|
+ * The maximum allowed length for the name used in the "from" field.
|
|
|
+ */
|
|
|
+ const MAXIMUM_NAME_LENGTH = 15;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * The maximum allowed length for the message.
|
|
|
+ */
|
|
|
+ const MAXIMUM_MESSAGE_LENGTH = 9500;
|
|
|
+
|
|
|
/**
|
|
|
* @var string
|
|
|
*/
|
|
|
@@ -47,6 +57,11 @@ class HipChatHandler extends SocketHandler
|
|
|
*/
|
|
|
private $notify;
|
|
|
|
|
|
+ /**
|
|
|
+ * @var string
|
|
|
+ */
|
|
|
+ private $format;
|
|
|
+
|
|
|
/**
|
|
|
* @param string $token HipChat API Token
|
|
|
* @param string $room The room that should be alerted of the message (Id or Name)
|
|
|
@@ -55,9 +70,14 @@ class HipChatHandler extends SocketHandler
|
|
|
* @param int $level The minimum logging level at which this handler will be triggered
|
|
|
* @param Boolean $bubble Whether the messages that are handled can bubble up the stack or not
|
|
|
* @param Boolean $useSSL Whether to connect via SSL.
|
|
|
+ * @param string $format The format of the messages (default to text, can be set to html if you have html in the messages)
|
|
|
*/
|
|
|
- public function __construct($token, $room, $name = 'Monolog', $notify = false, $level = Logger::CRITICAL, $bubble = true, $useSSL = true)
|
|
|
+ public function __construct($token, $room, $name = 'Monolog', $notify = false, $level = Logger::CRITICAL, $bubble = true, $useSSL = true, $format = 'text')
|
|
|
{
|
|
|
+ if (!$this->validateStringLength($name, static::MAXIMUM_NAME_LENGTH)) {
|
|
|
+ throw new \InvalidArgumentException('The supplied name is too long. HipChat\'s v1 API supports names up to 15 UTF-8 characters.');
|
|
|
+ }
|
|
|
+
|
|
|
$connectionString = $useSSL ? 'ssl://api.hipchat.com:443' : 'api.hipchat.com:80';
|
|
|
parent::__construct($connectionString, $level, $bubble);
|
|
|
|
|
|
@@ -65,6 +85,7 @@ class HipChatHandler extends SocketHandler
|
|
|
$this->name = $name;
|
|
|
$this->notify = $notify;
|
|
|
$this->room = $room;
|
|
|
+ $this->format = $format;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -93,7 +114,7 @@ class HipChatHandler extends SocketHandler
|
|
|
'room_id' => $this->room,
|
|
|
'notify' => $this->notify,
|
|
|
'message' => $record['formatted'],
|
|
|
- 'message_format' => 'text',
|
|
|
+ 'message_format' => $this->format,
|
|
|
'color' => $this->getAlertColor($record['level']),
|
|
|
);
|
|
|
|
|
|
@@ -159,13 +180,19 @@ class HipChatHandler extends SocketHandler
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
- $batchRecord = $this->combineRecords($records);
|
|
|
+ $batchRecords = $this->combineRecords($records);
|
|
|
|
|
|
- if (!$this->isHandling($batchRecord)) {
|
|
|
- return false;
|
|
|
+ $handled = false;
|
|
|
+ foreach ($batchRecords as $batchRecord) {
|
|
|
+ if ($this->isHandling($batchRecord)) {
|
|
|
+ $this->write($batchRecord);
|
|
|
+ $handled = true;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- $this->write($batchRecord);
|
|
|
+ if (!$handled) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
|
|
|
return false === $this->bubble;
|
|
|
}
|
|
|
@@ -180,6 +207,9 @@ class HipChatHandler extends SocketHandler
|
|
|
*/
|
|
|
private function combineRecords($records)
|
|
|
{
|
|
|
+ $batchRecord = null;
|
|
|
+ $batchRecords = array();
|
|
|
+ $batchedMessages = array();
|
|
|
$messages = array();
|
|
|
$formattedMessages = array();
|
|
|
$level = 0;
|
|
|
@@ -187,12 +217,7 @@ class HipChatHandler extends SocketHandler
|
|
|
$datetime = null;
|
|
|
|
|
|
foreach ($records as $record) {
|
|
|
-
|
|
|
$record = $this->processRecord($record);
|
|
|
- $record['formatted'] = $this->getFormatter()->format($record);
|
|
|
-
|
|
|
- $messages[] = $record['message'];
|
|
|
- $formattedMessages[] = $record['formatted'];
|
|
|
|
|
|
if ($record['level'] > $level) {
|
|
|
$level = $record['level'];
|
|
|
@@ -202,18 +227,74 @@ class HipChatHandler extends SocketHandler
|
|
|
if (null === $datetime) {
|
|
|
$datetime = $record['datetime'];
|
|
|
}
|
|
|
+
|
|
|
+ $messages[] = $record['message'];
|
|
|
+ $messgeStr = implode(PHP_EOL, $messages);
|
|
|
+ $formattedMessages[] = $this->getFormatter()->format($record);
|
|
|
+ $formattedMessageStr = implode('', $formattedMessages);
|
|
|
+
|
|
|
+ $batchRecord = array(
|
|
|
+ 'message' => $messgeStr,
|
|
|
+ 'formatted' => $formattedMessageStr,
|
|
|
+ 'context' => array(),
|
|
|
+ 'extra' => array(),
|
|
|
+ );
|
|
|
+
|
|
|
+ if (!$this->validateStringLength($batchRecord['formatted'], static::MAXIMUM_MESSAGE_LENGTH)) {
|
|
|
+ // Pop the last message and implode the remainging messages
|
|
|
+ $lastMessage = array_pop($messages);
|
|
|
+ $lastFormattedMessage = array_pop($formattedMessages);
|
|
|
+ $batchRecord['message'] = implode(PHP_EOL, $messages);
|
|
|
+ $batchRecord['formatted'] = implode('', $formattedMessages);
|
|
|
+
|
|
|
+ $batchRecords[] = $batchRecord;
|
|
|
+ $messages = array($lastMessage);
|
|
|
+ $formattedMessages = array($lastFormattedMessage);
|
|
|
+
|
|
|
+ $batchRecord = null;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- $batchRecord = array(
|
|
|
- 'message' => implode(PHP_EOL, $messages),
|
|
|
- 'formatted' => implode('', $formattedMessages),
|
|
|
- 'level' => $level,
|
|
|
- 'level_name' => $levelName,
|
|
|
- 'datetime' => $datetime,
|
|
|
- 'context' => array(),
|
|
|
- 'extra' => array(),
|
|
|
- );
|
|
|
+ if (null !== $batchRecord) {
|
|
|
+ $batchRecords[] = $batchRecord;
|
|
|
+ }
|
|
|
+
|
|
|
+ // Set the max level and datetime for all records
|
|
|
+ foreach ($batchRecords as &$batchRecord) {
|
|
|
+ $batchRecord = array_merge(
|
|
|
+ $batchRecord,
|
|
|
+ array(
|
|
|
+ 'level' => $level,
|
|
|
+ 'level_name' => $levelName,
|
|
|
+ 'datetime' => $datetime
|
|
|
+ )
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ return $batchRecords;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Validates the length of a string.
|
|
|
+ *
|
|
|
+ * If the `mb_strlen()` function is available, it will use that, as HipChat
|
|
|
+ * allows UTF-8 characters. Otherwise, it will fall back to `strlen()`.
|
|
|
+ *
|
|
|
+ * Note that this might cause false failures in the specific case of using
|
|
|
+ * a valid name with less than 16 characters, but 16 or more bytes, on a
|
|
|
+ * system where `mb_strlen()` is unavailable.
|
|
|
+ *
|
|
|
+ * @param string $str
|
|
|
+ * @param int $length
|
|
|
+ *
|
|
|
+ * @return bool
|
|
|
+ */
|
|
|
+ private function validateStringLength($str, $length)
|
|
|
+ {
|
|
|
+ if (function_exists('mb_strlen')) {
|
|
|
+ return (mb_strlen($str) <= $length);
|
|
|
+ }
|
|
|
|
|
|
- return $batchRecord;
|
|
|
+ return (strlen($str) <= $length);
|
|
|
}
|
|
|
}
|