Skip to content

Commit

Permalink
More test refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
reidmorrison committed Jul 1, 2024
1 parent ff207fd commit 051b2ef
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 118 deletions.
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ gem "honeybadger"
# [optional] Kafka appender
gem "ruby-kafka"
# [optional] MongoDB appender
gem "base64"
gem "mongo"
# [optional] NewRelic appender ( Tests use a mock class )
# gem 'newrelic_rpm'
Expand All @@ -38,6 +39,7 @@ gem "sentry-raven"
# [optional] new Sentry appender
gem "sentry-ruby"
# [optional] Syslog appender when communicating with a remote syslogd over TCP
gem "syslog"
gem "syslog_protocol"

gem "rubocop", "~> 1.28.1", require: false
5 changes: 5 additions & 0 deletions lib/semantic_logger/test/capture_log_events.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ def log(log)
@events << log
end

# Supports batching of log events
def batch(logs)
@events += log
end

def clear
@events.clear
end
Expand Down
2 changes: 1 addition & 1 deletion test/appender/wrapper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def flush
log.backtrace = backtrace
appender.log(log.freeze)
assert_match(
/\d+-\d+-\d+ \d+:\d+:\d+.\d+ W \[\d+:\w+\ default_test.rb:35] User -- hello world/, mock_logger.message
/\d+-\d+-\d+ \d+:\d+:\d+.\d+ W \[\d+:\w+\ default_test.rb:35\] User -- hello world/, mock_logger.message
)
end

Expand Down
17 changes: 13 additions & 4 deletions test/appenders_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

class AppendersTest < Minitest::Test
describe SemanticLogger::Appenders do
let :appenders do
logger = SemanticLogger["Test"]
SemanticLogger::Appenders.new(logger)
end
let(:capture_logger) { SemanticLogger::Test::CaptureLogEvents.new }
let(:appenders) { SemanticLogger::Appenders.new(capture_logger) }
let(:logger) { SemanticLogger::Test::CaptureLogEvents.new }

describe "#add" do
it "adds file appender" do
Expand Down Expand Up @@ -90,6 +89,16 @@ class AppendersTest < Minitest::Test

assert_equal 2, appenders.size
end

it "adds batch proxy" do
appender = appenders.add(appender: logger, batch: true)
assert_instance_of SemanticLogger::Appender::AsyncBatch, appender
end

# it "adds async proxy" do
# appender = appenders.add(appender: logger, async: true)
# assert_instance_of SemanticLogger::Appender::Async, appender
# end
end
end
end
9 changes: 2 additions & 7 deletions test/formatters_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,8 @@
class FormattersTest < Minitest::Test
describe SemanticLogger::Formatters do
describe ".factory" do
let :log do
SemanticLogger::Log.new("Test", :info)
end

let :appender do
SemanticLogger::Appender::IO.new($stdout)
end
let(:log) { SemanticLogger::Log.new("Test", :info) }
let(:appender) { SemanticLogger::Appender::IO.new($stdout) }

it "from a symbol" do
assert formatter = SemanticLogger::Formatters.factory(:raw)
Expand Down
131 changes: 26 additions & 105 deletions test/logger_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Unit Test for SemanticLogger::Logger
class LoggerTest < Minitest::Test
describe SemanticLogger::Logger do
include InMemoryAppenderHelper
let(:logger) { SemanticLogger::Test::CaptureLogEvents.new }

# Complex filters
module ComplexFilter
Expand All @@ -17,96 +17,36 @@ def self.call(log)
[:debug].each do |level|
describe "##{level}" do
describe "#filter" do
describe "at the appender level" do
it "Proc" do
appender.filter = ->(log) { (/\AExclude/ =~ log.message).nil? }
logger.send(level, "Exclude this log message", @hash) { "Calculations" }
it "Proc" do
logger.filter = ->(log) { (/\AExclude/ =~ log.message).nil? }
logger.send(level, "Exclude this log message", @hash) { "Calculations" }

refute log_message
end

it "Module" do
appender.filter = ComplexFilter
logger.send(level, "Exclude this log message", @hash) { "Calculations" }

refute log_message
end

it "RegExp" do
appender.filter = ->(log) { (/\AExclude/ =~ log.message).nil? }
logger.send(level, "Exclude this log message", @hash) { "Calculations" }

refute log_message
end
assert logger.events.empty?
end

describe "at the logger level" do
it "Proc" do
logger.filter = ->(log) { (/\AExclude/ =~ log.message).nil? }
logger.send(level, "Exclude this log message", @hash) { "Calculations" }

refute log_message
end
it "Module" do
logger.filter = ComplexFilter
logger.send(level, "Exclude this log message", @hash) { "Calculations" }

it "Module" do
logger.filter = ComplexFilter
logger.send(level, "Exclude this log message", @hash) { "Calculations" }

refute log_message
end

it "RegExp" do
logger.filter = ->(log) { (/\AExclude/ =~ log.message).nil? }
logger.send(level, "Exclude this log message", @hash) { "Calculations" }

refute log_message
end
assert logger.events.empty?
end

describe "on the logger initializer" do
describe "Proc" do
let :log_filter do
->(log) { (/\AExclude/ =~ log.message).nil? }
end

it "filters" do
logger.send(level, "Exclude this log message", @hash) { "Calculations" }
refute log_message
end
end

describe "Module" do
let :log_filter do
ComplexFilter
end
it "RegExp" do
logger.filter = ->(log) { (/\AExclude/ =~ log.message).nil? }
logger.send(level, "Exclude this log message", @hash) { "Calculations" }

it "filters" do
logger.send(level, "Exclude this log message", @hash) { "Calculations" }
refute log_message
end
end

describe "RegExp" do
let :log_filter do
/\ALogger/
end

it "filters" do
logger.send(level, "Exclude this log message", @hash) { "Calculations" }
refute log_message
end
end
assert logger.events.empty?
end
end
end
end

describe "when level is too high" do
it "does not log" do
SemanticLogger.default_level = :error
logger.level = :error
logger.info("Exclude this log message")

refute log_message
assert logger.events.empty?
end
end

Expand All @@ -126,26 +66,12 @@ def self.call(log)

describe "#level?" do
it "return true for debug? with :trace level" do
SemanticLogger.default_level = :trace
logger.level = :trace
assert_equal :trace, logger.level
assert_equal true, logger.debug?
assert_equal true, logger.trace?
end

it "return false for debug? with global :debug level" do
SemanticLogger.default_level = :debug
assert_equal :debug, logger.level, logger.inspect
assert_equal true, logger.debug?, logger.inspect
assert_equal false, logger.trace?, logger.inspect
end

it "return true for debug? with global :info level" do
SemanticLogger.default_level = :info
assert_equal :info, logger.level, logger.inspect
assert_equal false, logger.debug?, logger.inspect
assert_equal false, logger.trace?, logger.inspect
end

it "return false for debug? with instance :debug level" do
logger.level = :debug
assert_equal :debug, logger.level, logger.inspect
Expand All @@ -162,16 +88,11 @@ def self.call(log)
end

describe ".tagged" do
it "sets global defaults" do
assert_equal [], SemanticLogger.tags
assert_equal 0, SemanticLogger.backtrace_level_index
end

it "add tags to log entries" do
logger.tagged("12345", "DJHSFK") do
logger.info("hello world")

assert log = log_message
assert log = logger.events.first
assert_equal "hello world", log.message
assert_equal %w[12345 DJHSFK], log.tags
end
Expand All @@ -183,7 +104,7 @@ def self.call(log)
assert_equal ["First Level", "tags", "Second Level"], logger.tags
logger.info("hello world")

assert log = log_message
assert log = logger.events.first
assert_equal "hello world", log.message
assert_equal ["First Level", "tags", "Second Level"], log.tags
end
Expand All @@ -193,14 +114,14 @@ def self.call(log)

it "also supports named tagging" do
logger.tagged(level1: 1) do
assert_equal({level1: 1}, SemanticLogger.named_tags)
assert_equal({level1: 1}, logger.named_tags)
assert_equal({ level1: 1 }, SemanticLogger.named_tags)
assert_equal({ level1: 1 }, logger.named_tags)
logger.tagged(level2: 2, more: "data") do
assert_equal({level1: 1, level2: 2, more: "data"}, SemanticLogger.named_tags)
assert_equal({level1: 1, level2: 2, more: "data"}, logger.named_tags)
assert_equal({ level1: 1, level2: 2, more: "data" }, SemanticLogger.named_tags)
assert_equal({ level1: 1, level2: 2, more: "data" }, logger.named_tags)
logger.tagged(level3: 3) do
assert_equal({level1: 1, level2: 2, more: "data", level3: 3}, SemanticLogger.named_tags)
assert_equal({level1: 1, level2: 2, more: "data", level3: 3}, logger.named_tags)
assert_equal({ level1: 1, level2: 2, more: "data", level3: 3 }, SemanticLogger.named_tags)
assert_equal({ level1: 1, level2: 2, more: "data", level3: 3 }, logger.named_tags)
end
end
end
Expand All @@ -210,7 +131,7 @@ def self.call(log)
logger.tagged("", %w[12345 DJHSFK], nil) do
logger.info("hello world")

assert log = log_message
assert log = logger.events.first
assert_equal "hello world", log.message
assert_equal %w[12345 DJHSFK], log.tags
end
Expand All @@ -220,7 +141,7 @@ def self.call(log)
logger.tagged(%w[first second]) do
logger.info("hello world")

assert log = log_message
assert log = logger.events.first
assert_equal "hello world", log.message
assert_equal %w[first second], log.tags
end
Expand Down
2 changes: 1 addition & 1 deletion test/measure_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ class MeasureTest < Minitest::Test
assert_equal 1, appender.events.count, -> { appender.events.to_h.ai }
assert log = appender.events.first
assert_equal "hello world", log.message
assert_equal :warn, log.message
assert_equal :info, log.level
end

it "does not silence higher level messages" do
Expand Down

0 comments on commit 051b2ef

Please sign in to comment.