Skip to content

Commit

Permalink
HoptoadNotifier logging, Rack middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
asalant committed Feb 12, 2011
1 parent 5baf6c8 commit 2887655
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 2 deletions.
5 changes: 5 additions & 0 deletions Gemfile.lock
Expand Up @@ -7,7 +7,11 @@ GEM
remote: http://rubygems.org/
specs:
activesupport (3.0.4)
builder (3.0.0)
diff-lcs (1.1.2)
hoptoad_notifier (2.4.5)
activesupport
builder
json (1.4.6)
mocha (0.9.11)
rake
Expand Down Expand Up @@ -41,6 +45,7 @@ PLATFORMS

DEPENDENCIES
activesupport (~> 3.0)
hoptoad_notifier
log_weasel!
mocha
resque (~> 1.0)
Expand Down
8 changes: 7 additions & 1 deletion lib/log_weasel.rb
@@ -1,3 +1,9 @@
require 'log_weasel/transaction'
require 'log_weasel/resque'
require 'log_weasel/buffered_logger'
require 'log_weasel/hoptoad_notifier'
require 'log_weasel/middleware'
require 'log_weasel/resque'

class << ::HoptoadNotifier
include LogWeasel::HoptoadNotifier;
end if defined? ::HoptoadNotifier
23 changes: 23 additions & 0 deletions lib/log_weasel/hoptoad_notifier.rb
@@ -0,0 +1,23 @@
module LogWeasel::HoptoadNotifier
def notify_with_transaction_id(exception, opts = {})
add_transaction_id(opts) if LogWeasel::Transaction.id
notify_without_transaction_id exception, opts
end

def notify_or_ignore_with_transaction_id(exception, opts = {})
add_transaction_id(opts) if LogWeasel::Transaction.id
notify_or_ignore_without_transaction_id exception, opts
end

def add_transaction_id(opts)
opts[:parameters] ||= {}
opts[:parameters]['log_weasel_id'] = LogWeasel::Transaction.id
end

def self.included(base)
base.send :alias_method, :notify_without_transaction_id, :notify
base.send :alias_method, :notify, :notify_with_transaction_id
base.send :alias_method, :notify_or_ignore_without_transaction_id, :notify_or_ignore
base.send :alias_method, :notify_or_ignore, :notify_or_ignore_with_transaction_id
end
end
13 changes: 13 additions & 0 deletions lib/log_weasel/middleware.rb
@@ -0,0 +1,13 @@
class LogWeasel::Middleware
def initialize(app, options = {})
@app = app
@key = options[:key] || 'RAILS'
end

def call(env)
LogWeasel::Transaction.create "#{@key}-WEB"
@app.call(env)
ensure
LogWeasel::Transaction.destroy
end
end
1 change: 1 addition & 0 deletions log_weasel.gemspec
Expand Up @@ -18,6 +18,7 @@ Gem::Specification.new do |s|
s.add_development_dependency('mocha')
s.add_development_dependency('resque', ['~> 1.0'])
s.add_development_dependency('activesupport', ['~> 3.0'])
s.add_development_dependency('hoptoad_notifier',)

s.files = `git ls-files`.split("\n")
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
Expand Down
1 change: 1 addition & 0 deletions spec/log_weasel/buffered_logger_spec.rb
@@ -1,4 +1,5 @@
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
require 'log_weasel/buffered_logger'

describe LogWeasel::BufferedLogger do
before do
Expand Down
22 changes: 22 additions & 0 deletions spec/log_weasel/hoptoad_notifier_spec.rb
@@ -0,0 +1,22 @@
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
require 'hoptoad_notifier'
require 'log_weasel/hoptoad_notifier'

describe LogWeasel::HoptoadNotifier do
before do
class << ::HoptoadNotifier
include LogWeasel::HoptoadNotifier;
end if defined? ::HoptoadNotifier

HoptoadNotifier.configure {}
LogWeasel::Transaction.stubs(:id).returns('123')
end

it "adds transaction id to parameters with no parameters" do
HoptoadNotifier.expects(:send_notice).with do |notice|
notice.parameters.should have_key('log_weasel_id')
end
HoptoadNotifier.notify(RuntimeError.new('failure'))
end

end
1 change: 1 addition & 0 deletions spec/log_weasel/resque_spec.rb
@@ -1,5 +1,6 @@
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
require 'resque'
require 'log_weasel/resque'

describe LogWeasel::Resque do

Expand Down
2 changes: 1 addition & 1 deletion spec/spec_helper.rb
@@ -1,7 +1,7 @@
$LOAD_PATH.unshift(File.dirname(__FILE__))
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))

require 'log_weasel'
require 'log_weasel/transaction'
require 'rspec'

require 'active_support/secure_random'
Expand Down

0 comments on commit 2887655

Please sign in to comment.