From 2887655cd355caf9138b047ddcb844c12ec471d4 Mon Sep 17 00:00:00 2001 From: Alon Salant Date: Fri, 11 Feb 2011 17:56:02 -0800 Subject: [PATCH] HoptoadNotifier logging, Rack middleware --- Gemfile.lock | 5 +++++ lib/log_weasel.rb | 8 +++++++- lib/log_weasel/hoptoad_notifier.rb | 23 +++++++++++++++++++++++ lib/log_weasel/middleware.rb | 13 +++++++++++++ log_weasel.gemspec | 1 + spec/log_weasel/buffered_logger_spec.rb | 1 + spec/log_weasel/hoptoad_notifier_spec.rb | 22 ++++++++++++++++++++++ spec/log_weasel/resque_spec.rb | 1 + spec/spec_helper.rb | 2 +- 9 files changed, 74 insertions(+), 2 deletions(-) create mode 100644 lib/log_weasel/middleware.rb diff --git a/Gemfile.lock b/Gemfile.lock index 6b61f3d..07a6bb3 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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 @@ -41,6 +45,7 @@ PLATFORMS DEPENDENCIES activesupport (~> 3.0) + hoptoad_notifier log_weasel! mocha resque (~> 1.0) diff --git a/lib/log_weasel.rb b/lib/log_weasel.rb index 2970760..a51cca6 100644 --- a/lib/log_weasel.rb +++ b/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 \ No newline at end of file diff --git a/lib/log_weasel/hoptoad_notifier.rb b/lib/log_weasel/hoptoad_notifier.rb index e69de29..6d53a5c 100644 --- a/lib/log_weasel/hoptoad_notifier.rb +++ b/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 \ No newline at end of file diff --git a/lib/log_weasel/middleware.rb b/lib/log_weasel/middleware.rb new file mode 100644 index 0000000..b315001 --- /dev/null +++ b/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 \ No newline at end of file diff --git a/log_weasel.gemspec b/log_weasel.gemspec index 75153b1..31b27c1 100644 --- a/log_weasel.gemspec +++ b/log_weasel.gemspec @@ -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") diff --git a/spec/log_weasel/buffered_logger_spec.rb b/spec/log_weasel/buffered_logger_spec.rb index 949a8e7..2d1aeaf 100644 --- a/spec/log_weasel/buffered_logger_spec.rb +++ b/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 diff --git a/spec/log_weasel/hoptoad_notifier_spec.rb b/spec/log_weasel/hoptoad_notifier_spec.rb index e69de29..8d1e408 100644 --- a/spec/log_weasel/hoptoad_notifier_spec.rb +++ b/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 \ No newline at end of file diff --git a/spec/log_weasel/resque_spec.rb b/spec/log_weasel/resque_spec.rb index d475888..1686008 100644 --- a/spec/log_weasel/resque_spec.rb +++ b/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 diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 1681df4..989e3a8 100644 --- a/spec/spec_helper.rb +++ b/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'