Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

allow configuration of middleware with existing logger

  • Loading branch information...
commit ab95bf978db060e258f4f373688b35fc257183d1 1 parent 9a9140a
Eirik Dentz Sinclair & Noah Davis authored
View
15 lib/oink/middleware.rb
@@ -5,11 +5,9 @@
module Oink
class Middleware
- DEFAULT_LOG_PATH = "log/oink.log"
-
def initialize(app, options = {})
@app = app
- @log_path = options[:log_path] || DEFAULT_LOG_PATH
+ @logger = options[:logger] || Hodel3000CompliantLogger.new("log/oink.log")
@instruments = options[:instruments] || [:memory, :activerecord]
ActiveRecord::Base.send(:include, Oink::Instrumentation::ActiveRecord) if @instruments.include?(:activerecord)
@@ -28,21 +26,21 @@ def call(env)
end
def log_completed
- logger.info("Completed in")
+ @logger.info("Completed in")
end
def log_routing(env)
if env.has_key?('action_dispatch.request.parameters')
controller = env['action_dispatch.request.parameters']['controller']
action = env['action_dispatch.request.parameters']['action']
- logger.info("Processing #{controller}##{action}")
+ @logger.info("Processing #{controller}##{action}")
end
end
def log_memory
if @instruments.include?(:memory)
memory = Oink::Instrumentation::MemorySnapshot.memory
- logger.info("Memory usage: #{memory} | PID: #{$$}")
+ @logger.info("Memory usage: #{memory} | PID: #{$$}")
end
end
@@ -50,7 +48,7 @@ def log_activerecord
if @instruments.include?(:activerecord)
sorted_list = Oink::HashUtils.to_sorted_array(ActiveRecord::Base.instantiated_hash)
sorted_list.unshift("Total: #{ActiveRecord::Base.total_objects_instantiated}")
- logger.info("Instantiation Breakdown: #{sorted_list.join(' | ')}")
+ @logger.info("Instantiation Breakdown: #{sorted_list.join(' | ')}")
end
end
@@ -60,8 +58,5 @@ def reset_objects_instantiated
ActiveRecord::Base.reset_instance_type_count
end
- def logger
- @logger ||= Hodel3000CompliantLogger.new(@log_path)
- end
end
end
View
22 spec/oink/middleware_configuration_spec.rb
@@ -14,28 +14,6 @@ def call(env)
let(:app) { Oink::Middleware.new(SampleApplication.new, oink_configuration) }
let(:oink_configuration) { @oink_configuration || {} }
- context "log_path option" do
- before do
- Oink::Instrumentation::MemorySnapshot.stub(:memory => 4092)
- FileUtils.mkdir_p("log")
- end
-
- after do
- FileUtils.remove_dir("log")
- end
-
- it "writes to the default log path when not specified" do
- get "/"
- File.should exist(Oink::Middleware::DEFAULT_LOG_PATH)
- end
-
- it "writes to the log path specified" do
- @oink_configuration = {:log_path => "log/custom_path.log"}
- get "/"
- File.should exist("log/custom_path.log")
- end
- end
-
context "instruments options" do
before do
@log_output = StringIO.new
View
21 spec/oink/middleware_spec.rb
@@ -21,53 +21,52 @@ def call(env)
end
end
- let(:app) { Oink::Middleware.new(SampleApplication.new) }
+ let(:log_output) { StringIO.new }
+ let(:logger) { Hodel3000CompliantLogger.new(log_output) }
+ let(:app) { Oink::Middleware.new(SampleApplication.new, :logger => logger) }
before do
- @log_output = StringIO.new
- Hodel3000CompliantLogger.stub(:new => Hodel3000CompliantLogger.new(@log_output))
Oink::Instrumentation::MemorySnapshot.stub(:memory => 4092)
Pig.delete_all
Pen.delete_all
end
-
context "support legacy rails log format in transition to oink's own log format" do
it "writes rails[pid] to the log even if the app isn't a rails app (for now)" do
get "/no_pigs"
- @log_output.string.should include("rails[#{$$}]")
+ log_output.string.should include("rails[#{$$}]")
end
it "writes 'Completed in' after the request has completed" do
get "/no_pigs"
- @log_output.string.should include("Completed in")
+ log_output.string.should include("Completed in")
end
it "logs the action and controller" do
get "/no_pigs", {}, {'action_dispatch.request.parameters' => {'controller' => 'oinkoink', 'action' => 'piggie'}}
- @log_output.string.should include("Processing oinkoink#piggie")
+ log_output.string.should include("Processing oinkoink#piggie")
end
end
it "reports 0 totals" do
get "/no_pigs"
- @log_output.string.should include("Instantiation Breakdown: Total: 0")
+ log_output.string.should include("Instantiation Breakdown: Total: 0")
end
it "reports totals first even if it's a tie" do
get "/two_pigs"
- @log_output.string.should include("Instantiation Breakdown: Total: 2 | Pig: 2")
+ log_output.string.should include("Instantiation Breakdown: Total: 2 | Pig: 2")
end
it "reports pigs and pens instantiated" do
get "/two_pigs_in_a_pen"
- @log_output.string.should include("Instantiation Breakdown: Total: 3 | Pig: 2 | Pen: 1")
+ log_output.string.should include("Instantiation Breakdown: Total: 3 | Pig: 2 | Pen: 1")
end
it "logs memory usage" do
Oink::Instrumentation::MemorySnapshot.should_receive(:memory).and_return(4092)
get "/two_pigs_in_a_pen"
- @log_output.string.should include("Memory usage: 4092 | PID: #{$$}")
+ log_output.string.should include("Memory usage: 4092 | PID: #{$$}")
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.