Skip to content

Instantly share code, notes, and snippets.

@jmodjeska jmodjeska/rollbar.pl
Created Jul 10, 2018

Embed
What would you like to do?
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
You can’t perform that action at this time.