Permalink
Browse files

ERP-54 - Fixed logging to respect log levels as well as respect enabl…

…e/disable setting in config file.
  • Loading branch information...
1 parent 8113bc0 commit 7cd0cab5a326515a48bc132f94f3ea07a52aa8de @honest-thanh honest-thanh committed May 22, 2014
Showing with 109 additions and 0 deletions.
  1. +1 −0 lib/honest_pubsub/logger.rb
  2. +108 −0 spec/logger_spec.rb
@@ -10,6 +10,7 @@ def initialize(enable_publish_logging = true)
@enabled = enable_publish_logging if @enabled.nil?
@log_file = ::Logger.new(@config[:file])
+ @log_file.level = ::Logger::Severity.const_get(@level.upcase)
@logger = ActiveSupport::TaggedLogging.new(@log_file)
@log_method = @level.to_sym.to_proc
end
View
@@ -0,0 +1,108 @@
+require 'spec_helper'
+
+describe HonestPubsub::Logger do
+ let(:uri) { "test/logger" }
+ let(:subject) { HonestPubsub::Logger.new }
+ let(:payload) { {"hit"=>"me"} }
+ let(:message) { HonestPubsub::Message.new.serialize(uri, payload) }
+ let(:config_buffer) { StringIO.new }
+ let(:output_string) { config_buffer.string }
+ let(:config_override) { {} }
+
+ before do
+ directory = File::dirname(__FILE__)
+ full_name = File.join(directory, "config.yml")
+ @config_data = HashWithIndifferentAccess.new( YAML.load_file(full_name)[ENV["RAILS_ENV"]] )
+ @config_data.merge!(config_override)
+ @config_data[:logger][:file] = config_buffer
+ HonestPubsub::Configuration.should_receive(:configuration).and_return(@config_data)
+ end
+
+ describe "#log_publish" do
+ let(:result) { subject.log_publish(uri, message)}
+
+
+ it "should not fail" do
+ expect{ result }.not_to raise_error()
+ end
+
+ it "should write a row to the log" do
+ result
@ricaurte

ricaurte Jun 26, 2014

why are we doing an explicit result declare here and everywhere else in this file? We should remove these and either put result in a before or do let!(:result)

+ expect(output_string.length).not_to eq(0)
+ end
+
+ end
+
+ describe "#failed_publish" do
+ let(:result) { subject.failed_publish(uri, {}, message)}
+
+ context "warning log level" do
+
+ it "should not fail" do
+ expect{ result }.not_to raise_error()
+ end
+
+ it "should respect the log level of the file" do
+ result
+ expect(output_string.length).not_to eq(0)
+ end
+ end
+
+ context "fatal log level" do
+ let(:config_override) { { 'logger'=> { 'level'=>'fatal' } } }
+
+ it "should not fail" do
+ expect{ result }.not_to raise_error()
+ end
+
+ it "should respect the log level of the file" do
+ result
+ expect(output_string.length).to eq(0)
+ end
+ end
+ end
+
+ describe "#log_receive" do
+ let(:result) { subject.log_receive(uri, message)}
+
+ it "should not fail" do
+ expect{ result }.not_to raise_error()
+ end
+
+ it "should respect the log level of the file" do
+ result
+ expect(output_string.length).not_to eq(0)
+ end
+ end
+
+ describe "#log_service" do
+ let(:service_name) { "test" }
+ let(:result) { subject.log_service(service_name, log_level, message)}
+
+ context "debugger log" do
+ let(:log_level) { :debug }
+ it "should not fail" do
+ expect{ result }.not_to raise_error()
+ end
+
+ it "should respect the log level of the file" do
+ result
+ expect(output_string.length).to eq(0)
+ end
+ end
+
+ context "warn log" do
+ let(:log_level) { :warn }
+ it "should not fail" do
+ expect{ result }.not_to raise_error()
+ end
+
+ it "should respect the log level of the file" do
+ result
+ expect(output_string.length).not_to eq(0)
+ end
+ end
+
+ end
+
+end

0 comments on commit 7cd0cab

Please sign in to comment.