test/logger_test.rb in semantic_logger-2.12.0 vs test/logger_test.rb in semantic_logger-2.13.0
- old
+ new
@@ -214,37 +214,102 @@
should "log when the block performs a return" do
assert_equal "Good", function_with_return(@logger)
SemanticLogger.flush
assert_match /\d+-\d+-\d+ \d+:\d+:\d+.\d+ I \[\d+:.+\] \(\d+\.\dms\) LoggerTest -- hello world -- #{@hash_str}/, @mock_logger.message
end
+
+ should 'not log at a level below the silence level' do
+ SemanticLogger.default_level = :info
+ @logger.benchmark_info('hello world', silence: :error) do
+ @logger.warn "don't log me"
+ end
+ SemanticLogger.flush
+ assert_match /\d+-\d+-\d+ \d+:\d+:\d+.\d+ I \[\d+:.+\] \(\d+\.\dms\) LoggerTest -- hello world/, @mock_logger.message
+ end
+
+ should 'log at a silence level below the default level' do
+ SemanticLogger.default_level = :info
+ first_message = nil
+ @logger.benchmark_info('hello world', silence: :trace) do
+ @logger.debug('hello world', @hash) { "Calculations" }
+ SemanticLogger.flush
+ first_message = @mock_logger.message
+ end
+ assert_match /\d+-\d+-\d+ \d+:\d+:\d+.\d+ D \[\d+:.+\] LoggerTest -- hello world -- Calculations -- #{@hash_str}/, first_message
+ SemanticLogger.flush
+ # Only the last log message is kept in mock logger
+ assert_match /\d+-\d+-\d+ \d+:\d+:\d+.\d+ I \[\d+:.+\] \(\d+\.\dms\) LoggerTest -- hello world/, @mock_logger.message
+ end
end
context '.default_level' do
setup do
SemanticLogger.default_level = :debug
end
should 'not log at a level below the global default' do
+ assert_equal :debug, SemanticLogger.default_level
assert_equal :debug, @logger.level
@logger.trace('hello world', @hash) { "Calculations" }
SemanticLogger.flush
assert_nil @mock_logger.message
end
should 'log at the instance level' do
+ assert_equal :debug, SemanticLogger.default_level
@logger.level = :trace
assert_equal :trace, @logger.level
@logger.trace('hello world', @hash) { "Calculations" }
SemanticLogger.flush
assert_match /\d+-\d+-\d+ \d+:\d+:\d+.\d+ T \[\d+:.+\] LoggerTest -- hello world -- Calculations -- #{@hash_str}/, @mock_logger.message
end
should 'not log at a level below the instance level' do
+ assert_equal :debug, SemanticLogger.default_level
@logger.level = :warn
assert_equal :warn, @logger.level
@logger.debug('hello world', @hash) { "Calculations" }
SemanticLogger.flush
assert_nil @mock_logger.message
+ end
+ end
+
+ context '.silence' do
+ setup do
+ SemanticLogger.default_level = :info
+ end
+
+ should 'not log at a level below the silence level' do
+ assert_equal :info, SemanticLogger.default_level
+ assert_equal :info, @logger.level
+ @logger.silence do
+ @logger.warn('hello world', @hash) { "Calculations" }
+ @logger.info('hello world', @hash) { "Calculations" }
+ @logger.debug('hello world', @hash) { "Calculations" }
+ @logger.trace('hello world', @hash) { "Calculations" }
+ end
+ SemanticLogger.flush
+ assert_nil @mock_logger.message
+ end
+
+ should 'log at the instance level even with the silencer at a higher level' do
+ @logger.level = :trace
+ assert_equal :trace, @logger.level
+ @logger.silence do
+ @logger.trace('hello world', @hash) { "Calculations" }
+ end
+ SemanticLogger.flush
+ assert_match /\d+-\d+-\d+ \d+:\d+:\d+.\d+ T \[\d+:.+\] LoggerTest -- hello world -- Calculations -- #{@hash_str}/, @mock_logger.message
+ end
+
+ should 'log at a silence level below the default level' do
+ assert_equal :info, SemanticLogger.default_level
+ assert_equal :info, @logger.level
+ @logger.silence(:debug) do
+ @logger.debug('hello world', @hash) { "Calculations" }
+ end
+ SemanticLogger.flush
+ assert_match /\d+-\d+-\d+ \d+:\d+:\d+.\d+ D \[\d+:.+\] LoggerTest -- hello world -- Calculations -- #{@hash_str}/, @mock_logger.message
end
end
context '.level?' do
should 'return true for debug? with :trace level' do
\ No newline at end of file