spec/timber/logger_spec.rb in timber-2.1.0.rc4 vs spec/timber/logger_spec.rb in timber-2.1.0.rc5

- old
+ new

@@ -13,11 +13,11 @@ Timber::Config.instance.environment = "development" example.run Timber::Config.instance.environment = old_env end - it "shoud select the augmented formatter" do + it "shoud select the message only formatter" do logger = described_class.new(nil) expect(logger.formatter).to be_kind_of(Timber::Logger::MessageOnlyFormatter) end end end @@ -29,19 +29,45 @@ around(:each) do |example| Timecop.freeze(time) { example.run } end + it "should respect the level via Logger constants" do + logger.formatter = Timber::Logger::MessageOnlyFormatter.new + + logger.level = ::Logger::DEBUG + logger.info("message") + expect(io.string).to eq("message\n") + + io.string = "" + logger.level = ::Logger::WARN + logger.info("message") + expect(io.string).to eq("") + end + + it "should respect the level via level symbols" do + logger.formatter = Timber::Logger::MessageOnlyFormatter.new + + logger.level = :debug + logger.info("message") + expect(io.string).to eq("message\n") + + io.string = "" + logger.level = :warn + logger.info("message") + expect(io.string).to eq("") + end + context "with the AugmentedFormatter" do before(:each) { logger.formatter = Timber::Logger::AugmentedFormatter.new } it "should accept strings" do logger.info("this is a test") expect(io.string).to start_with("this is a test @metadata {\"level\":\"info\",\"dt\":\"2016-09-01T12:00:00.000000Z\"") end - it "should non-strings" do + it "should accept non-strings" do logger.info(true) expect(io.string).to start_with("true @metadata") end context "with a context" do @@ -74,11 +100,11 @@ logger.info(message) expect(io.string).to start_with("payment rejected @metadata {\"level\":\"info\",\"dt\":\"2016-09-01T12:00:00.000000Z\",") expect(io.string).to include("\"event\":{\"custom\":{\"payment_rejected\":{\"customer_id\":\"abcde1234\",\"amount\":100}}}") end - it "should log properly when an event is passed" do + it "should log properly when a Timber::Event object is passed" do message = Timber::Events::SQLQuery.new(sql: "select * from users", time_ms: 56, message: "select * from users") logger.info(message) expect(io.string).to start_with("select * from users @metadata {\"level\":\"info\",\"dt\":\"2016-09-01T12:00:00.000000Z\",") expect(io.string).to include("\"event\":{\"sql_query\":{\"sql\":\"select * from users\",\"time_ms\":56.0}}") end @@ -92,12 +118,16 @@ logger.info("event complete", tag: "tag1") expect(io.string).to include("\"tags\":[\"tag1\"]") end it "should allow :tags" do - logger.info("event complete", tags: ["tag1", "tag2"]) + tags = ["tag1", "tag2"] + logger.info("event complete", tags: tags) expect(io.string).to include("\"tags\":[\"tag1\",\"tag2\"]") + + # Ensure the tags object is not modified + expect(tags).to eq(["tag1", "tag2"]) end it "should allow functions" do logger.info do {message: "payment rejected", payment_rejected: {customer_id: "abcde1234", amount: 100}} @@ -128,10 +158,28 @@ expect(logger.formatter).to be_kind_of(Timber::Logger::PassThroughFormatter) end end end + describe "#error" do + let(:io) { StringIO.new } + let(:logger) { Timber::Logger.new(io) } + + it "should allow default usage" do + logger.error("message") + expect(io.string).to start_with("message @metadata") + expect(io.string).to include('"level":"error"') + end + + it "should allow messages with options" do + logger.error("message", tag: "tag") + expect(io.string).to start_with("message @metadata") + expect(io.string).to include('"level":"error"') + expect(io.string).to include('"tags":["tag"]') + end + end + describe "#formatter=" do it "should not allow changing the formatter when the device is HTTP" do http_device = Timber::LogDevices::HTTP.new("api_key") logger = Timber::Logger.new(http_device) expect { logger.formatter = ::Logger::Formatter.new }.to raise_error(ArgumentError) @@ -143,10 +191,33 @@ logger.formatter = formatter expect(logger.formatter).to eq(formatter) end end + describe "#info" do + let(:io) { StringIO.new } + let(:logger) { Timber::Logger.new(io) } + + it "should allow default usage" do + logger.info("message") + expect(io.string).to start_with("message @metadata") + expect(io.string).to include('"level":"info"') + end + + it "should allow messages with options" do + logger.info("message", tag: "tag") + expect(io.string).to start_with("message @metadata") + expect(io.string).to include('"level":"info"') + expect(io.string).to include('"tags":["tag"]') + end + + it "should accept non-string messages" do + logger.info(true) + expect(io.string).to start_with("true @metadata") + end + end + describe "#silence" do let(:io) { StringIO.new } let(:logger) { Timber::Logger.new(io) } it "should silence the logs" do @@ -174,49 +245,8 @@ expect(Timber::CurrentContext.instance.send(:hash)[:custom]).to eq({:build=>{:version=>"1.0.0"}}) end expect(Timber::CurrentContext.instance.send(:hash)[:custom]).to be_nil - end - end - - describe "#info" do - let(:io) { StringIO.new } - let(:logger) { Timber::Logger.new(io) } - - it "should allow default usage" do - logger.info("message") - expect(io.string).to start_with("message @metadata") - expect(io.string).to include('"level":"info"') - end - - it "should allow messages with options" do - logger.info("message", tag: "tag") - expect(io.string).to start_with("message @metadata") - expect(io.string).to include('"level":"info"') - expect(io.string).to include('"tags":["tag"]') - end - - it "should accept non-string messages" do - logger.info(true) - expect(io.string).to start_with("true @metadata") - end - end - - describe "#error" do - let(:io) { StringIO.new } - let(:logger) { Timber::Logger.new(io) } - - it "should allow default usage" do - logger.error("message") - expect(io.string).to start_with("message @metadata") - expect(io.string).to include('"level":"error"') - end - - it "should allow messages with options" do - logger.error("message", tag: "tag") - expect(io.string).to start_with("message @metadata") - expect(io.string).to include('"level":"error"') - expect(io.string).to include('"tags":["tag"]') end end end \ No newline at end of file