spec/timber/logger_spec.rb in timber-2.6.2 vs spec/timber/logger_spec.rb in timber-3.0.0
- old
+ new
@@ -1,12 +1,12 @@
require "spec_helper"
-describe Timber::Logger, :rails_23 => true do
+describe Timber::Logger do
describe "#initialize" do
it "shoud select the augmented formatter" do
logger = described_class.new(nil)
- expect(logger.formatter).to be_kind_of(Timber::Logger::AugmentedFormatter)
+ expect(logger.formatter).to be_kind_of(Timber::Logger::JSONFormatter)
end
context "development environment" do
around(:each) do |example|
old_env = Timber::Config.instance.environment
@@ -22,25 +22,25 @@
end
it "should allow multiple io devices" do
io1 = StringIO.new
io2 = StringIO.new
- logger = Timber::Logger.new(STDOUT, io1, io2)
+ logger = Timber::Logger.new(io1, io2)
logger.info("hello world")
- expect(io1.string).to start_with("hello world @metadata {")
- expect(io2.string).to start_with("hello world @metadata {")
+ expect(io1.string).to include("hello world")
+ expect(io2.string).to include("hello world")
end
it "should allow multiple io devices and loggers" do
io1 = StringIO.new
io2 = StringIO.new
io3 = StringIO.new
extra_logger = ::Logger.new(io3)
- logger = Timber::Logger.new(STDOUT, io1, io2, extra_logger)
+ logger = Timber::Logger.new(io1, io2, extra_logger)
logger.info("hello world")
- expect(io1.string).to start_with("hello world @metadata {")
- expect(io2.string).to start_with("hello world @metadata {")
+ expect(io1.string).to include("hello world")
+ expect(io2.string).to include("hello world")
expect(io3.string).to end_with("hello world\n")
end
end
describe "#add" do
@@ -86,11 +86,11 @@
expect(io.string).to start_with("this is a test @metadata {\"level\":\"info\",\"dt\":\"2016-09-01T12:00:00.000000Z\"")
end
it "should accept non-strings" do
logger.info(true)
- expect(io.string).to start_with("true @metadata")
+ expect(io.string).to include("true")
end
context "with a context" do
let(:http_context) do
Timber::Contexts::HTTP.new(
@@ -99,39 +99,32 @@
remote_addr: "123.456.789.10",
request_id: "abcd1234"
)
end
- around(:each) do |example|
- Timber::CurrentContext.with(http_context) do
- example.run
- end
+ before(:each) do |example|
+ Timber::CurrentContext.add(http_context)
end
+ after(:each) do |example|
+ Timber::CurrentContext.remove(:http)
+ end
it "should snapshot and include the context" do
expect(Timber::CurrentContext.instance).to receive(:snapshot).and_call_original
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\"")
expect(io.string).to include("\"http\":{\"method\":\"POST\",\"path\":\"/checkout\",\"remote_addr\":\"123.456.789.10\",\"request_id\":\"abcd1234\"}")
end
end
- it "should call and use Timber::Events.build" do
+ it "should pass hash as metadata" do
message = {message: "payment rejected", payment_rejected: {customer_id: "abcde1234", amount: 100}}
- expect(Timber::Events).to receive(:build).with(message).and_call_original
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}}}")
+ expect(io.string).to include("\"payment_rejected\":{\"customer_id\":\"abcde1234\",\"amount\":100}")
end
- 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
-
it "should allow :tag" do
logger.info("event complete", tag: "tag1")
expect(io.string).to include("\"tags\":[\"tag1\"]")
end
@@ -147,11 +140,11 @@
it "should allow functions" do
logger.info do
{message: "payment rejected", payment_rejected: {customer_id: "abcde1234", amount: 100}}
end
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}}}")
+ expect(io.string).to include("\"payment_rejected\":{\"customer_id\":\"abcde1234\",\"amount\":100}")
end
it "should escape new lines" do
logger.info "first\nsecond"
expect(io.string).to start_with("first\\nsecond @metadata")
@@ -179,18 +172,18 @@
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")
+ logger.error("log message")
+ expect(io.string).to include("log message")
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")
+ logger.error("log message", tag: "tag")
+ expect(io.string).to include("log message")
expect(io.string).to include('"level":"error"')
expect(io.string).to include('"tags":["tag"]')
end
end
@@ -212,57 +205,23 @@
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")
+ logger.info("log message")
+ expect(io.string).to include("log message")
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")
+ logger.info("log message", tag: "tag")
+ expect(io.string).to include("log message")
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")
+ expect(io.string).to include("true")
end
end
-
- describe "#silence" do
- let(:io) { StringIO.new }
- let(:logger) { Timber::Logger.new(io) }
-
- it "should silence the logs" do
- logger.silence do
- logger.info("test")
- end
-
- expect(io.string).to eq("")
- end
- end
-
- describe "#with_context" do
- let(:io) { StringIO.new }
- let(:logger) { Timber::Logger.new(io) }
-
- it "should add context" do
- expect(Timber::CurrentContext.instance.send(:hash)[:custom]).to be_nil
-
- logger.with_context(build: {version: "1.0.0"}) do
- expect(Timber::CurrentContext.instance.send(:hash)[:custom]).to eq({:build=>{:version=>"1.0.0"}})
-
- logger.with_context({testing: {key: "value"}}) do
- expect(Timber::CurrentContext.instance.send(:hash)[:custom]).to eq({:build=>{:version=>"1.0.0"}, :testing=>{:key=>"value"}})
- end
-
- 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
-end
\ No newline at end of file
+end