PHP code example of talentrydev / rollbar

1. Go to this page and download the library: Download talentrydev/rollbar library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

talentrydev / rollbar example snippets



// installs global error and exception handlers
Rollbar::init(array('access_token' => 'POST_SERVER_ITEM_ACCESS_TOKEN'));

try {
    throw new Exception('test exception');
} catch (Exception $e) {
    Rollbar::report_exception($e);
}

// Message at level 'info'
Rollbar::report_message('testing 123', Level::INFO);

// With extra data (3rd arg) and custom payload options (4th arg)
Rollbar::report_message('testing 123', Level::INFO,
                        // key-value additional data
                        array("some_key" => "some value"),  
                        // payload options (overrides defaults) - see api docs
                        array("fingerprint" => "custom-fingerprint-here"));

// raises an E_NOTICE which will *not* be reported by the error handler
$foo = $bar;

// will be reported by the exception handler
throw new Exception('test 2');


fig = array(
    // ST_SERVER_ITEM_ACCESS_TOKEN',
    // optional - environment name. any string will do.
    'environment' => 'production',
    // optional - path to directory your code is in. used for linking stack traces.
    'root' => '/Users/brian/www/myapp'
);
Rollbar::init($config);


$set_exception_handler = false;
$set_error_handler = false;
Rollbar::init($config, $set_exception_handler, $set_error_handler);


Rollbar::init(array(
    'environment' => 'production'
));


try {
    do_something();
} catch (Exception $e) {
    Rollbar::report_exception($e);
    // or
    Rollbar::report_exception($e, array("my" => "extra", "data" => 42));
}


Rollbar::report_message('could not connect to mysql server', Level::WARNING);
Rollbar::report_message('Here is a message with some additional data',
    Level::INFO, array('x' => 10, 'code' => 'blue'));

Rollbar::flush();


$config = array(
  // ... rest of current config
  'handler' => 'agent',
  'agent_log_location' => '/var/www'  // not including final slash. must be writeable by the user php runs as.
);


$config['proxy'] = "127.0.0.1:8080";


$config['proxy'] = array(
    'address' => '127.0.0.1:8080',
    'username' => 'my_user',
    'password' => 'my_password'
);


$config['error_sample_rates'] = array(
    // E_WARNING omitted, so defaults to 1
    E_NOTICE => 0.1,
    E_USER_ERROR => 0.5,
    // E_USER_WARNING will take the same value, 0.5
    E_USER_NOTICE => 0.1,
    // E_STRICT and beyond will all be 0.1
);


function get_current_user() {
    if ($_SESSION['user_id']) {
        return array(
            'id' => $_SESSION['user_id'], // ional - value is a string
        );
    }
    return null;
}
$config['person_fn'] = 'get_current_user';
POST_SERVER_ITEM_ACCESS_TOKEN

$config = array(
    'access_token' => '...',
    'checkIgnore' => function ($isUncaught, $exception, $payload) {
        if (isset($_SERVER['HTTP_USER_AGENT']) && strpos($_SERVER['HTTP_USER_AGENT'], 'Baiduspider') !== false) {
          // ignore baidu spider
          return true;
        }

        // no other ignores
        return false;
    }; 
);
Rollbar::init($config);