require 'spec_helper.rb' describe Pwwka::Logging do class ForLogging extend Pwwka::Logging end it "returns the logger" do Pwwka.configure do |c| c.logger = MonoLogger.new(STDOUT) end expect(ForLogging.logger).to be_instance_of(MonoLogger) end %w(debug info error fatal).each do |severity| it "logs #{severity} messages at the class level" do expect(ForLogging.respond_to?(severity.to_sym)).to eq true end end describe "#logf" do let(:logger) { double(Logger) } before do @original_logger = Pwwka.configuration.logger @original_log_level = Pwwka.configuration.log_level Pwwka.configuration.logger = logger allow(logger).to receive(:info) allow(logger).to receive(:error) end after do Pwwka.configuration.logger = @original_logger Pwwka.configuration.log_level = @original_log_level Pwwka.configuration.payload_logging = @original_payload_logging end it "logs a printf-style string at info" do ForLogging.logf("This is %{test} some %{data}", test: "a test of", data: "data and stuff", ignored: :hopefully) expect(logger).to have_received(:info).with("This is a test of some data and stuff") end it "logs at a different level if configured to do so" do Pwwka.configuration.log_level = :debug allow(logger).to receive(:debug) ForLogging.logf("This is %{test} some %{data}", test: "a test of", data: "data and stuff", ignored: :hopefully) expect(logger).to have_received(:debug).with("This is a test of some data and stuff") end it "logs a printf-style string at info" do ForLogging.logf("This is %{test} some %{data}", test: "a test of", data: "data and stuff", ignored: :hopefully) expect(logger).to have_received(:info).with("This is a test of some data and stuff") end it "can log an not-info" do ForLogging.logf("This is %{test} some %{data}", test: "a test of", data: "data and stuff", ignored: :hopefully, at: :error) expect(logger).to have_received(:error).with("This is a test of some data and stuff") expect(logger).not_to have_received(:info) end context "payload-stripping" do [ :payload, "payload", ].each do |name| it "will strip payload (given as a #{name.class}) if configured" do Pwwka.configuration.payload_logging = :error Pwwka.configuration.receive_raw_payload = false ForLogging.logf("This is the payload: %{payload}", name => { foo: "bar" }) ForLogging.logf("This is also the payload: %{payload}", name => { foo: "bar" }, at: :error) expect(logger).to have_received(:info).with("This is the payload: [omitted]") expect(logger).to have_received(:error).with("This is also the payload: {:foo=>\"bar\"}") end it "will strip payload (given as a #{name.class}) of errors, too" do Pwwka.configuration.payload_logging = :fatal Pwwka.configuration.receive_raw_payload = false ForLogging.logf("This is the payload: %{payload}", name => { foo: "bar" }) ForLogging.logf("This is also the payload: %{payload}", name => { foo: "bar" }, at: :error) expect(logger).to have_received(:info).with("This is the payload: [omitted]") expect(logger).to have_received(:error).with("This is also the payload: [omitted]") end it "will strip payload (given as a #{name.class}) if we AREN'T parsing payloads" do Pwwka.configuration.payload_logging = :info Pwwka.configuration.receive_raw_payload = true ForLogging.logf("This is the payload: %{payload}", name => { foo: "bar" }) ForLogging.logf("This is also the payload: %{payload}", name => { foo: "bar" }, at: :error) expect(logger).to have_received(:info).with("This is the payload: [omitted]") expect(logger).to have_received(:error).with("This is also the payload: [omitted]") end end context "log_hook matching" do def test_func(message, params) end before do Pwwka.configuration.log_hooks = { "test message" => ->(message, params){ test_func(message, params) } } end after do Pwwka.configuration.log_hooks = {} end context "message matches hook" do it "overrides logging" do ForLogging.logf("test message", {}) expect(logger).not_to have_received(:info) end it "calls the hook" do allow(self).to receive(:test_func) test_params = { param: :test } ForLogging.logf("test message", test_params) expect(self).to have_received(:test_func).with("test message", test_params) end end context "message doesn't match hook" do it "logs as normal" do ForLogging.logf("other message", {}) expect(logger).to have_received(:info).with("other message") end end end end end end