Easy Airbrake error logging for simple ruby scripts
Add this code to any one-off scripts you might run without supervision (cron jobs are a great use case) and if an exception is raised while running it it will be logged to Airbrake. A good place to put this is in a shared "boot" script that any of your one-off scripts include.
# configure Airbrake and set it up to automatically
# report any non-standard errors when the program
# exits
Airbrake.configure do |config|
config.api_key = "<your-project-key-here>"
end
# this is one way to get the environment your script
# is running in, you can change out the body of this
# method with whatever works well for you.
def project_environment
require 'socket'
Socket.gethostname == "<your-production-server-hostname-here>" ? "production" : "development"
end
at_exit do
# SystemExit and Interrupt are normal exceptions
# that terminate the ruby process, ignore them
if $! and not [SystemExit, Interrupt].include? $!.class
params = {:argv => ARGV.join(" "), :script => $0}
environment = project_environment
if environment == "production"
Airbrake.notify($!, :parameters => params,
:environment_name => environment)
end
end
end
This is partly derived from a code example in @jeg2's "101 things you didn't know ruby could do" talk https://speakerdeck.com/jeg2/10-things-you-didnt-know-ruby-could-do
This is my first protip: be nice!
Written by William Howard
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Ruby
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#