Skip to content

Commit

Permalink
Merge branch 'master' into github-177-phpinput
Browse files Browse the repository at this point in the history
  • Loading branch information
ArturMoczulski authored Jun 11, 2017
2 parents 602bb41 + 507462f commit b700357
Show file tree
Hide file tree
Showing 15 changed files with 1,084 additions and 33 deletions.
69 changes: 67 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,39 @@ Rollbar::init(array(
?>
```

## Integration with Rollbar.js

In case you want to report your JavaScript errors using [Rollbar.js](https://github.com/rollbar/rollbar.js), you can configure the SDK to enable Rollbar.js on your site. Example:

```php
$rollbarJs = Rollbar\RollbarJsHelper::buildJs(
array(
"accessToken" => "POST_CLIENT_ITEM_ACCESS_TOKEN",
"captureUncaught" => true,
"payload" => array(
"environment" => "production"
),
/* other configuration you want to pass to RollbarJS */
)
);
```

Or if you are using Content-Security-Policy: script-src 'unsafe-inline'
```php
$rollbarJs = Rollbar\RollbarJsHelper::buildJs(
array(
"accessToken" => "POST_CLIENT_ITEM_ACCESS_TOKEN",
"captureUncaught" => true,
"payload" => array(
"environment" => "production"
),
/* other configuration you want to pass to RollbarJS */
),
headers_list(),
$yourNonceString
);
```

## Basic Usage

That's it! Uncaught errors and exceptions will now be reported to Rollbar.
Expand Down Expand Up @@ -165,6 +198,23 @@ Rollbar::log(
?>
```

## Using dependency injection

If you're using dependency injection containers, you can create and get a `RollbarLogger` from the container and use it
to initialize Rollbar error logging.

It's up to the container to properly create and configure the logger.

```php
use Rollbar\Rollbar;
use Rollbar\RollbarLogger;

$logger = $container->get(RollbarLogger::class);

// installs global error and exception handlers
Rollbar::init($logger);
```

## Using Monolog

Here is an example of how to use Rollbar as a handler for Monolog:
Expand Down Expand Up @@ -242,9 +292,16 @@ All of the following options can be passed as keys in the `$config` array.
Default: `/var/www`
</dd>

<dt>endpoint
</dt>
<dd>The API URL to post to. Note: the URL has to end with a trailing slash.

Default: `https://api.rollbar.com/api/1/`
</dd>

<dt>base_api_url
</dt>
<dd>The base api url to post to.
<dd><strong>Deprecated (use <i>endpoint</i> instead).</strong> The base api url to post to.

Default: `https://api.rollbar.com/api/1/`
</dd>
Expand Down Expand Up @@ -460,6 +517,7 @@ Default: No proxy
Default: `false`
</dd>


<dt>include_raw_request_body</dt>
<dd>Include the raw request body from php://input in payloads.
Note: in PHP < 5.6 if you enable this, php://input will be empty for PUT requests
Expand All @@ -468,6 +526,9 @@ as Rollbar SDK will read from it
If you still want to read the request body for your PUT requests Rollbar SDK saves
the content of php://input in $_SERVER['php://input']

<dt>local_vars_dump</dt>
<dd>Should backtraces include arguments passed to stack frames.

Default: `false`
</dd>

Expand Down Expand Up @@ -508,13 +569,17 @@ $config['person_fn'] = 'get_current_user';

## Related projects

A Laravel-specific package is available for integrating with Laravel: [Laravel-Rollbar](https://github.com/jenssegers/Laravel-Rollbar)
A Laravel-specific package is available for integrating with Laravel: [Rollbar Laravel](https://github.com/rollbar/rollbar-php-laravel)

A CakePHP-specific package is avaliable for integrating with CakePHP 2.x:
[CakeRollbar](https://github.com/tranfuga25s/CakeRollbar)

A Flow-specific package is available for integrating with Neos Flow: [m12/flow-rollbar](https://packagist.org/packages/m12/flow-rollbar)

Yii package: [baibaratsky/yii-rollbar](https://github.com/baibaratsky/yii-rollbar)

Yii2 package: [baibaratsky/yii2-rollbar](https://github.com/baibaratsky/yii2-rollbar)

## Help / Support

If you run into any issues, please email us at [[email protected]](mailto:[email protected])
Expand Down
1 change: 1 addition & 0 deletions data/rollbar.snippet.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 10 additions & 1 deletion src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,11 @@ private function setSender($c)
$default = "Rollbar\Senders\CurlSender";

if (array_key_exists('base_api_url', $c)) {
$c['senderOptions']['endpoint'] = $c['base_api_url'];
$c['senderOptions']['endpoint'] = $c['base_api_url'] . 'item/';
}

if (array_key_exists('endpoint', $c)) {
$c['senderOptions']['endpoint'] = $c['endpoint'] . 'item/';
}

if (array_key_exists('timeout', $c)) {
Expand Down Expand Up @@ -303,6 +307,11 @@ public function getDataBuilder()
{
return $this->dataBuilder;
}

public function getSender()
{
return $this->sender;
}

/**
* @param Payload $payload
Expand Down
104 changes: 85 additions & 19 deletions src/DataBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ class DataBuilder implements DataBuilderInterface
protected $shiftFunction;
protected $sendMessageTrace;
protected $rawRequestBody;
protected $localVarsDump;
protected $captureErrorStacktraces;

public function __construct($config)
{
Expand Down Expand Up @@ -92,6 +94,8 @@ public function __construct($config)
$this->setIncludeCodeContext($config);
$this->setIncludeExcCodeContext($config);
$this->setSendMessageTrace($config);
$this->setLocalVarsDump($config);
$this->setCaptureErrorStacktraces($config);

$this->shiftFunction = $this->tryGet($config, 'shift_function');
if (!isset($this->shiftFunction)) {
Expand Down Expand Up @@ -169,6 +173,18 @@ protected function setRawRequestBody($config)
$this->rawRequestBody = self::$defaults->rawRequestBody($fromConfig);
}

protected function setLocalVarsDump($config)
{
$fromConfig = $this->tryGet($config, 'local_vars_dump');
$this->localVarsDump = self::$defaults->localVarsDump($fromConfig);
}

protected function setCaptureErrorStacktraces($config)
{
$fromConfig = $this->tryGet($config, 'capture_error_stacktraces');
$this->captureErrorStacktraces = self::$defaults->captureErrorStacktraces($fromConfig);
}

protected function setCodeVersion($config)
{
$fromConfig = $this->tryGet($config, 'codeVersion');
Expand Down Expand Up @@ -392,7 +408,12 @@ public function getExceptionTrace($exc)
*/
public function makeTrace($exception, $includeContext, $classOverride = null)
{
$frames = $this->makeFrames($exception, $includeContext);
if ($this->captureErrorStacktraces) {
$frames = $this->makeFrames($exception, $includeContext);
} else {
$frames = array();
}

$excInfo = new ExceptionInfo(
Utilities::coalesce($classOverride, get_class($exception)),
$exception->getMessage()
Expand All @@ -407,11 +428,15 @@ public function makeFrames($exception, $includeContext)
$filename = Utilities::coalesce($this->tryGet($frameInfo, 'file'), '<internal>');
$lineno = Utilities::coalesce($this->tryGet($frameInfo, 'line'), 0);
$method = $frameInfo['function'];
// TODO 4 (arguments are in $frame)
$args = Utilities::coalesce($this->tryGet($frameInfo, 'args'), null);

$frame = new Frame($filename);
$frame->setLineno($lineno)
->setMethod($method);

if ($this->localVarsDump && $args !== null) {
$frame->setArgs($args);
}

if ($includeContext) {
$this->addCodeContextToFrame($frame, $filename, $lineno);
Expand Down Expand Up @@ -474,7 +499,9 @@ protected function getMessage($toLog, $context)
return new Message(
(string)$toLog,
$context,
$this->sendMessageTrace ? debug_backtrace() : null
$this->sendMessageTrace ?
debug_backtrace($this->localVarsDump ? 0 : DEBUG_BACKTRACE_IGNORE_ARGS) :
null
);
}

Expand Down Expand Up @@ -528,15 +555,24 @@ protected function getContext()
protected function getRequest()
{
$request = new Request();

$request->setUrl($this->getUrl())
->setMethod($this->tryGet($_SERVER, 'REQUEST_METHOD'))
->setHeaders($this->getHeaders())
->setParams($this->getRequestParams())
->setGet($_GET)
->setQueryString($this->tryGet($_SERVER, "QUERY_STRING"))
->setPost($_POST)
->setBody($this->getRequestBody())
->setUserIp($this->getUserIp());

if (isset($_SERVER)) {
$request->setMethod($this->tryGet($_SERVER, 'REQUEST_METHOD'))
->setQueryString($this->tryGet($_SERVER, "QUERY_STRING"));
}

if (isset($_GET)) {
$request->setGet($_GET);
}
if (isset($_POST)) {
$request->setPost($_POST);
}
$extras = $this->getRequestExtras();
if (!$extras) {
$extras = array();
Expand Down Expand Up @@ -670,13 +706,15 @@ public function getUrl()
$port = $this->getUrlPort($proto);


$path = Utilities::coalesce($this->tryGet($_SERVER, 'REQUEST_URI'), '/');
$url = $proto . '://' . $host;
if (($proto == 'https' && $port != 443) || ($proto == 'http' && $port != 80)) {
$url .= ':' . $port;
}

$url .= $path;
if (isset($_SERVER)) {
$path = Utilities::coalesce($this->tryGet($_SERVER, 'REQUEST_URI'), '/');
$url .= $path;
}

if ($host == 'unknown') {
$url = null;
Expand All @@ -688,16 +726,18 @@ public function getUrl()
protected function getHeaders()
{
$headers = array();
foreach ($_SERVER as $key => $val) {
if (substr($key, 0, 5) == 'HTTP_') {
// convert HTTP_CONTENT_TYPE to Content-Type, HTTP_HOST to Host, etc.
$name = strtolower(substr($key, 5));
if (strpos($name, '_') != -1) {
$name = preg_replace('/ /', '-', ucwords(preg_replace('/_/', ' ', $name)));
} else {
$name = ucfirst($name);
if (isset($_SERVER)) {
foreach ($_SERVER as $key => $val) {
if (substr($key, 0, 5) == 'HTTP_') {
// convert HTTP_CONTENT_TYPE to Content-Type, HTTP_HOST to Host, etc.
$name = strtolower(substr($key, 5));
if (strpos($name, '_') != -1) {
$name = preg_replace('/ /', '-', ucwords(preg_replace('/_/', ' ', $name)));
} else {
$name = ucfirst($name);
}
$headers[$name] = $val;
}
$headers[$name] = $val;
}
}
if (count($headers) > 0) {
Expand All @@ -720,6 +760,9 @@ protected function getRequestBody()

protected function getUserIp()
{
if (!isset($_SERVER)) {
return null;
}
$forwardFor = $this->tryGet($_SERVER, 'HTTP_X_FORWARDED_FOR');
if ($forwardFor) {
// return everything until the first comma
Expand Down Expand Up @@ -788,7 +831,7 @@ protected function getServer()
foreach ($extras as $key => $val) {
$server->$key = $val;
}
if (array_key_exists('argv', $_SERVER)) {
if (isset($_SERVER) && array_key_exists('argv', $_SERVER)) {
$server->argv = $_SERVER['argv'];
}
return $server;
Expand Down Expand Up @@ -994,4 +1037,27 @@ public function needsTruncating(array $payload)
{
return strlen(json_encode($payload)) > self::MAX_PAYLOAD_SIZE;
}

/**
* Wrap a PHP error in an ErrorWrapper class and add backtrace information
*
* @param string $errno
* @param string $errstr
* @param string $errfile
* @param string $errline
*
* @return ErrorWrapper
*/
public function generateErrorWrapper($errno, $errstr, $errfile, $errline)
{
if ($this->captureErrorStacktraces) {
$backTrace = array_slice(
debug_backtrace($this->localVarsDump ? 0 : DEBUG_BACKTRACE_IGNORE_ARGS),
2
);
} else {
$backTrace = array();
}
return new ErrorWrapper($errno, $errstr, $errfile, $errline, $backTrace);
}
}
18 changes: 17 additions & 1 deletion src/Defaults.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,19 @@ private static function getScrubFields()

public function sendMessageTrace($sendMessageTrace = null)
{
return $sendMessageTrace ? $sendMessageTrace : $this->defaultSendMessageTrace;
return $sendMessageTrace !== null ? $sendMessageTrace : $this->defaultSendMessageTrace;
}

public function captureErrorStacktraces($captureErrorStracktraces = null)
{
return $captureErrorStracktraces !== null ?
$captureErrorStracktraces :
$this->defaultCaptureErrorStacktraces;
}

public function localVarsDump($localVarsDump = null)
{
return $localVarsDump !== null ? $localVarsDump : $this->defaultLocalVarsDump;
}

public function rawRequestBody($rawRequestBody = null)
Expand All @@ -108,6 +120,8 @@ public function rawRequestBody($rawRequestBody = null)
private $defaultIncludeCodeContext;
private $defaultIncludeExcCodeContext;
private $defaultRawRequestBody;
private $defaultLocalVarsDump;
private $defaultCaptureErrorStacktraces;

public function __construct()
{
Expand Down Expand Up @@ -158,6 +172,8 @@ public function __construct()
$this->defaultIncludeCodeContext = false;
$this->defaultIncludeExcCodeContext = false;
$this->defaultRawRequestBody = false;
$this->defaultLocalVarsDump = false;
$this->defaultCaptureErrorStacktraces = true;
}

public function messageLevel($level = null)
Expand Down
Loading

0 comments on commit b700357

Please sign in to comment.