diff --git a/ChromePhp.php b/ChromePhp.php index 577b1ce..5fef37f 100755 --- a/ChromePhp.php +++ b/ChromePhp.php @@ -26,13 +26,19 @@ class ChromePhp /** * @var string */ - const VERSION = '4.1.0'; + const VERSION = '4.1.1'; /** * @var string */ const HEADER_NAME = 'X-ChromeLogger-Data'; + /** + * @var int + * This should match Apache LimitRequestFieldSize Directive + */ + const HEADER_LIMIT = 8000; + /** * @var string */ @@ -391,9 +397,67 @@ protected function _addRow(array $logs, $backtrace, $type) protected function _writeHeader($data) { - header(self::HEADER_NAME . ': ' . $this->_encode($data)); + $encdata = $this->_shrinkLog($data); + header(self::HEADER_NAME . ': ' . $encdata); } + /** + * checks if headers will be too large. If so, it + * will remove notices first, then other types until + * the header will be small enough. + * @author Frank Forte + * @var array + * @return string + */ + protected function _shrinkLog($data) + { + $encdata = $this->_encode($data); + + if(mb_strlen($encdata) > self::HEADER_LIMIT) + { + array_unshift($data['rows'],[['!! WARNING !!! Headers too large, log truncated to prevent Apache 500 Server Error.', 'ChromePHP : '.__LINE__, self::ERROR]]); + } + + while(mb_strlen($encdata) > self::HEADER_LIMIT) + { + $shrinking = false; + // first remove regular status messages from tables from start to finish + // try to leave behind warnings, errors + foreach($data['rows'] as $j => $row) + { + if(isset($row[2]) && $row[2] === 'table') + { + foreach($row[0] as $k => $ro) + { + if($data['rows'][$j][0][$k][sizeof($data['rows'][$j][0][$k]) -1][1] == 'status') + { + array_pop($data['rows'][$j][0][$k]); + $shrinking = true; + break; + } + } + } + } + // no status messages to remove from the table log? + if(!$shrinking) + { + // remove regular logs + if(isset($row[0][2]) && $row[0][2] !== 'error') + { + unset($data['rows']); + $shrinking = true; + } + // ok, if nothing else, remove debug messages from start to finish + else + { + array_pop($data['rows']); + } + } + $encdata = $this->_encode($data); + } + return $encdata; + } + /** * encodes the data to be sent along with the request * @@ -443,4 +507,4 @@ public function getSetting($key) } return $this->_settings[$key]; } -} +} \ No newline at end of file diff --git a/composer.json b/composer.json index dc16853..1d179c9 100644 --- a/composer.json +++ b/composer.json @@ -1,9 +1,9 @@ { - "name": "ccampbell/chromephp", + "name": "frankforte/chromephp", "type": "library", "description": "Log variables to the Chrome console (via Chrome Logger Google Chrome extension).", "keywords": ["log","logging"], - "homepage": "http://github.com/ccampbell/chromephp", + "homepage": "http://github.com/frankforte/chromephp", "license": "Apache-2.0", "authors": [ { @@ -11,6 +11,12 @@ "email": "iamcraigcampbell@gmail.com", "homepage": "http://craig.is", "role": "Developer" + }, + { + "name": "Frank Forte", + "email": "frank.forte@gmail.com", + "homepage": "http://www.frankforte.ca", + "role": "Developer" } ], "require": {