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

Add limit to batched records

This commit is contained in:
Leevi Graham
2014-01-24 00:31:57 +11:00
committed by Leevi Graham
parent 7f783c0ab0
commit d37e916040

View File

@@ -32,6 +32,11 @@ class HipChatHandler extends SocketHandler
*/ */
const MAXIMUM_NAME_LENGTH = 15; const MAXIMUM_NAME_LENGTH = 15;
/**
* The maximum allowed length for the message.
*/
const MAXIMUM_MESSAGE_LENGTH = 9500;
/** /**
* @var string * @var string
*/ */
@@ -63,7 +68,7 @@ class HipChatHandler extends SocketHandler
*/ */
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)
{ {
if (!$this->validateName($name)) { 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.'); throw new \InvalidArgumentException('The supplied name is too long. HipChat\'s v1 API supports names up to 15 UTF-8 characters.');
} }
@@ -168,14 +173,15 @@ class HipChatHandler extends SocketHandler
return true; return true;
} }
$batchRecord = $this->combineRecords($records); $batchRecords = $this->combineRecords($records);
if (!$this->isHandling($batchRecord)) { foreach($batchRecords as $batchRecord) {
return false;
if ($this->isHandling($batchRecord)) {
$this->write($batchRecord);
}
} }
$this->write($batchRecord);
return false === $this->bubble; return false === $this->bubble;
} }
@@ -189,6 +195,9 @@ class HipChatHandler extends SocketHandler
*/ */
private function combineRecords($records) private function combineRecords($records)
{ {
$batchRecord = null;
$batchRecords = array();
$batchedMessages = array();
$messages = array(); $messages = array();
$formattedMessages = array(); $formattedMessages = array();
$level = 0; $level = 0;
@@ -198,10 +207,6 @@ class HipChatHandler extends SocketHandler
foreach ($records as $record) { foreach ($records as $record) {
$record = $this->processRecord($record); $record = $this->processRecord($record);
$record['formatted'] = $this->getFormatter()->format($record);
$messages[] = $record['message'];
$formattedMessages[] = $record['formatted'];
if ($record['level'] > $level) { if ($record['level'] > $level) {
$level = $record['level']; $level = $record['level'];
@@ -211,23 +216,56 @@ class HipChatHandler extends SocketHandler
if (null === $datetime) { if (null === $datetime) {
$datetime = $record['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( if (null !== $batchRecord) {
'message' => implode(PHP_EOL, $messages), $batchRecords[] = $batchRecord;
'formatted' => implode('', $formattedMessages), }
'level' => $level,
'level_name' => $levelName,
'datetime' => $datetime,
'context' => array(),
'extra' => array(),
);
return $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 supplied name for the "from" field. * Validates the length of a string.
* *
* If the `mb_strlen()` function is available, it will use that, as HipChat * If the `mb_strlen()` function is available, it will use that, as HipChat
* allows UTF-8 characters. Otherwise, it will fall back to `strlen()`. * allows UTF-8 characters. Otherwise, it will fall back to `strlen()`.
@@ -236,15 +274,17 @@ class HipChatHandler extends SocketHandler
* a valid name with less than 16 characters, but 16 or more bytes, on a * a valid name with less than 16 characters, but 16 or more bytes, on a
* system where `mb_strlen()` is unavailable. * system where `mb_strlen()` is unavailable.
* *
* @param string $name Name to validate * @param string $str
* @return Boolean * @param int $length
*
* @return bool
*/ */
private function validateName($name) private function validateStringLength($str, $length)
{ {
if (function_exists('mb_strlen')) { if (function_exists('mb_strlen')) {
return (mb_strlen($name) <= static::MAXIMUM_NAME_LENGTH); return (mb_strlen($str) <= $length);
} }
return (strlen($name) <= static::MAXIMUM_NAME_LENGTH); return (strlen($str) <= $length);
} }
} }