Kouhei Sutou
null+****@clear*****
Thu Nov 9 14:50:59 JST 2017
Kouhei Sutou 2017-11-09 14:50:59 +0900 (Thu, 09 Nov 2017) New Revision: 42573d6db1f1c8d796c6adb5104f7eba7e4057dd https://github.com/groonga/groonga-log/commit/42573d6db1f1c8d796c6adb5104f7eba7e4057dd Message: Simplify Modified files: lib/groonga-log/parser.rb test/test-parser.rb Modified: lib/groonga-log/parser.rb (+5 -15) =================================================================== --- lib/groonga-log/parser.rb 2017-11-09 14:40:25 +0900 (c6c6384) +++ lib/groonga-log/parser.rb 2017-11-09 14:50:59 +0900 (0f54d61) @@ -19,32 +19,21 @@ require "groonga-log/statistic" module GroongaLog class Parser - PATTERNS = { - :context_id_pattern => + PATTERN = /\A(?<year>\d{4})-(?<month>\d\d)-(?<day>\d\d) \ (?<hour>\d\d):(?<minute>\d\d):(?<second>\d\d)\.(?<micro_second>\d+) \|(?<log_level>.) - \|(?<context_id>.+?) - \|(?<message>.*)/x, - :no_context_id_pattern => - /\A(?<year>\d{4})-(?<month>\d\d)-(?<day>\d\d) - \ (?<hour>\d\d):(?<minute>\d\d):(?<second>\d\d)\.(?<micro_second>\d+) - \|(?<log_level>.) - \|(?<message>.*)/x - } + \|(?:(?<context_id>.+?):)? + \ (?<message>.*)/x def parse(input) return to_enum(:parse, input) unless block_given? input.each_line do |line| next unless line.valid_encoding? + m = PATTERN.match(line) statistic = Statistic.new - if m = PATTERNS[:context_id_pattern].match(line) then - statistic.context_id = m['context_id'] - else - m = PATTERNS[:no_context_id_pattern].match(line) - end year = m['year'].to_i month = m['month'].to_i @@ -56,6 +45,7 @@ module GroongaLog statistic.timestamp = Time.local(year, month, day, hour, minute, second, micro_second) statistic.log_level = log_level_to_symbol(m['log_level']) + statistic.context_id = m['context_id'] statistic.message = m['message'] yield statistic end Modified: test/test-parser.rb (+12 -12) =================================================================== --- test/test-parser.rb 2017-11-09 14:40:25 +0900 (a45413b) +++ test/test-parser.rb 2017-11-09 14:50:59 +0900 (2892fc7) @@ -22,11 +22,11 @@ class ParserTest < Test::Unit::TestCase raw_statistic = { :timestamp => Time.local(2017, 7, 19, 14, 9, 5, 663978), :log_level => :notice, - :context_id => "18c61700", + :context_id => "29", :message => "spec:2:update:Object:32(type):8", } statistics = parse(<<-LOG) -2017-07-19 14:09:05.663978|n|18c61700|spec:2:update:Object:32(type):8 +2017-07-19 14:09:05.663978|n|29: spec:2:update:Object:32(type):8 LOG assert_equal([raw_statistic], statistics.collect(&:to_h)) @@ -45,15 +45,15 @@ class ParserTest < Test::Unit::TestCase :dump ] statistics = parse(<<-LOG) -2017-07-19 14:41:05.663978|E|18c61700|emergency -2017-07-19 14:41:06.663978|A|18c61700|alert -2017-07-19 14:41:06.663978|C|18c61700|critical -2017-07-19 14:41:06.663978|e|18c61700|error -2017-07-19 14:41:06.663978|w|18c61700|warning -2017-07-19 14:41:06.663978|n|18c61700|notice -2017-07-19 14:41:06.663978|i|18c61700|information -2017-07-19 14:41:06.663978|d|18c61700|debug -2017-07-19 14:41:06.663978|-|18c61700|dump +2017-07-19 14:41:05.663978|E|29: emergency +2017-07-19 14:41:06.663978|A|29: alert +2017-07-19 14:41:06.663978|C|29: critical +2017-07-19 14:41:06.663978|e|29: error +2017-07-19 14:41:06.663978|w|29: warning +2017-07-19 14:41:06.663978|n|29: notice +2017-07-19 14:41:06.663978|i|29: information +2017-07-19 14:41:06.663978|d|29: debug +2017-07-19 14:41:06.663978|-|29: dump LOG assert_equal(expected, statistics.collect(&:log_level)) @@ -64,7 +64,7 @@ class ParserTest < Test::Unit::TestCase :timestamp => Time.local(2017, 7, 19, 14, 9, 5, 663978), :log_level => :notice, :context_id => nil, - :message => " spec:2:update:Object:32(type):8", + :message => "spec:2:update:Object:32(type):8", } statistics = parse(<<-LOG) 2017-07-19 14:09:05.663978|n| spec:2:update:Object:32(type):8 -------------- next part -------------- HTML����������������������������... URL: https://lists.osdn.me/mailman/archives/groonga-commit/attachments/20171109/5dad4a2d/attachment-0001.htm