Created
July 10, 2018 02:07
-
-
Save jmodjeska/b2ed67c6901622fe35094595b7e456ac to your computer and use it in GitHub Desktop.
Simple Rollbar Error Reporting in Perl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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