test/logger_test.rb in semantic_logger-4.0.0 vs test/logger_test.rb in semantic_logger-4.1.0

- old
+ new

@@ -107,15 +107,15 @@ hash_str = hash.inspect.sub('{', '\{').sub('}', '\}') assert_match(/\d+-\d+-\d+ \d+:\d+:\d+.\d+ #{level_char} \[\d+:#{@thread_name}\] LoggerTest -- #{hash_str}/, @mock_logger.message) end it 'logs duration' do - @logger.send(level, duration: 123.45, message: 'Hello world', payload: {tracking_number: '123456', even: 2, more: 'data'}) + @logger.send(level, duration: 123.44, message: 'Hello world', payload: {tracking_number: '123456', even: 2, more: 'data'}) hash = {tracking_number: '123456', even: 2, more: 'data'} SemanticLogger.flush hash_str = hash.inspect.sub('{', '\{').sub('}', '\}') - duration_match = defined?(JRuby) ? '\(123ms\)' : '\(123\.5ms\)' + duration_match = SemanticLogger::Formatters::Base::PRECISION == 3 ? '\(123ms\)' : '\(123\.4ms\)' assert_match(/\d+-\d+-\d+ \d+:\d+:\d+.\d+ #{level_char} \[\d+:#{@thread_name}\] #{duration_match} LoggerTest -- Hello world -- #{hash_str}/, @mock_logger.message) end it 'does not log when below min_duration' do @logger.send(level, min_duration: 200, duration: 123.45, message: 'Hello world', payload: {tracking_number: '123456', even: 2, more: 'data'}) @@ -129,15 +129,15 @@ SemanticLogger.on_metric do |log| $last_metric = log.dup end metric_name = '/my/custom/metric' - @logger.send(level, metric: metric_name, duration: 123.45, message: 'Hello world', payload: {tracking_number: '123456', even: 2, more: 'data'}) + @logger.send(level, metric: metric_name, duration: 123.44, message: 'Hello world', payload: {tracking_number: '123456', even: 2, more: 'data'}) hash = {tracking_number: '123456', even: 2, more: 'data'} SemanticLogger.flush hash_str = hash.inspect.sub('{', '\{').sub('}', '\}') - duration_match = defined?(JRuby) ? '\(123ms\)' : '\(123\.5ms\)' + duration_match = SemanticLogger::Formatters::Base::PRECISION == 3 ? '\(123ms\)' : '\(123\.4ms\)' assert_match(/\d+-\d+-\d+ \d+:\d+:\d+.\d+ #{level_char} \[\d+:#{@thread_name}\] #{duration_match} LoggerTest -- Hello world -- #{hash_str}/, @mock_logger.message) assert metric_name, $last_metric.metric end end @@ -208,9 +208,52 @@ it 'return true for debug? with instance :info level' do @logger.level = :info assert_equal :info, @logger.level, @logger.inspect assert_equal false, @logger.debug?, @logger.inspect assert_equal false, @logger.trace?, @logger.inspect + end + end + + describe '.tagged' do + it 'add tags to log entries' do + @logger.tagged('12345', 'DJHSFK') do + @logger.info('Hello world') + SemanticLogger.flush + assert_match(/\d+-\d+-\d+ \d+:\d+:\d+.\d+ I \[\d+:#{@thread_name}\] \[12345\] \[DJHSFK\] LoggerTest -- Hello world/, @mock_logger.message) + end + end + + it 'add embedded tags to log entries' do + @logger.tagged('First Level', 'tags') do + @logger.tagged('Second Level') do + @logger.info('Hello world') + SemanticLogger.flush + assert_match(/\d+-\d+-\d+ \d+:\d+:\d+.\d+ I \[\d+:#{@thread_name}\] \[First Level\] \[tags\] \[Second Level\] LoggerTest -- Hello world/, @mock_logger.message) + end + assert_equal 2, @logger.tags.count, @logger.tags + assert_equal 'First Level', @logger.tags.first + assert_equal 'tags', @logger.tags.last + end + end + + it 'also supports named tagging' do + @logger.tagged(level1: 1) do + assert_equal({level1: 1}, SemanticLogger.named_tags) + @logger.tagged(level2: 2, more: 'data') do + assert_equal({level1: 1, level2: 2, more: 'data'}, SemanticLogger.named_tags) + @logger.tagged(level3: 3) do + assert_equal({level1: 1, level2: 2, more: 'data', level3: 3}, SemanticLogger.named_tags) + end + end + end + end + + it 'is compatible with rails logging that uses arrays and nils' do + @logger.tagged('', ['12345', 'DJHSFK'], nil) do + @logger.info('Hello world') + SemanticLogger.flush + assert_match(/\d+-\d+-\d+ \d+:\d+:\d+.\d+ I \[\d+:#{@thread_name}\] \[12345\] \[DJHSFK\] LoggerTest -- Hello world/, @mock_logger.message) + end end end end end