Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent Apache 500 internal server error, truncates logs that are too… #65

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 67 additions & 3 deletions ChromePhp.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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 <[email protected]>
* @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
*
Expand Down Expand Up @@ -443,4 +507,4 @@ public function getSetting($key)
}
return $this->_settings[$key];
}
}
}
10 changes: 8 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
{
"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": [
{
"name": "Craig Campbell",
"email": "[email protected]",
"homepage": "http://craig.is",
"role": "Developer"
},
{
"name": "Frank Forte",
"email": "[email protected]",
"homepage": "http://www.frankforte.ca",
"role": "Developer"
}
],
"require": {
Expand Down