Skip to content

Instantly share code, notes, and snippets.

@jmodjeska
Created July 10, 2018 02:07
Show Gist options
  • Save jmodjeska/b2ed67c6901622fe35094595b7e456ac to your computer and use it in GitHub Desktop.
Save jmodjeska/b2ed67c6901622fe35094595b7e456ac to your computer and use it in GitHub Desktop.
Simple Rollbar Error Reporting in Perl
#!/usr/bin/perl
use strict;
use warnings;
use LWP::UserAgent;
use Cwd;
use Sys::Hostname;
use Data::Dumper;
# ========================================================================
# Configuration
# ========================================================================
# Environment
our $dirname = Cwd::abs_path($0);
our $host = hostname;
our $version = '1.0';
# Rollbar Config
our $rollbar_token = 'abcdef01234567890';
our $rollbar_endpoint = 'https://api.rollbar.com/api/1/item/';
# ========================================================================
# Rollbar Function
# ========================================================================
sub rollbar {
my ($level, $message) = @_;
my $json = '{
"access_token": "' . $rollbar_token . '",
"data": {
"environment": "production",
"body": {
"message": {
"body": "' . $message .'"
}
},
"level": "' . $level . '",
"timestamp": "' . time() . '",
"code_version": "' . $version . '",
"language": "perl",
"server": {
"host": "' . $host . '",
"root": "' . $dirname . '"
}
}
}';
my $req = HTTP::Request->new( 'POST', $rollbar_endpoint );
$req->header( 'Content-Type' => 'application/json' );
$req->content( $json );
my $lwp = LWP::UserAgent->new;
my $response = $lwp->request( $req );
return $response;
}
# ========================================================================
# Example Usage
# ========================================================================
my $test = rollbar('info', 'Hello World!');
print Dumper( $test );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment