[Groonga-commit] groonga/groonga-log at d09ce42 [master] Added parse pattern of groonga log

Back to archive index

HorimotoYasuhiro null+****@clear*****
Thu Nov 9 13:50:05 JST 2017


HorimotoYasuhiro	2017-11-09 13:50:05 +0900 (Thu, 09 Nov 2017)

  New Revision: d09ce423ca4baf66e8e2bbf265db29a607775391
  https://github.com/groonga/groonga-log/commit/d09ce423ca4baf66e8e2bbf265db29a607775391

  Merged 89585aa: Merge pull request #7 from komainu8/add_pattern_match_for_no_pid_log

  Message:
    Added parse pattern of groonga log

  Modified files:
    lib/groonga-log/parser.rb
    test/test-parser.rb

  Modified: lib/groonga-log/parser.rb (+14 -3)
===================================================================
--- lib/groonga-log/parser.rb    2017-09-28 09:15:45 +0900 (293ffc2)
+++ lib/groonga-log/parser.rb    2017-11-09 13:50:05 +0900 (c6c6384)
@@ -19,21 +19,33 @@ require "groonga-log/statistic"
 
 module GroongaLog
   class Parser
-    PATTERN =
+    PATTERNS = {
+      :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>.)
           \|(?<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
+    }
 
     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
         day = m['day'].to_i
@@ -44,7 +56,6 @@ 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 (+41 -0)
===================================================================
--- test/test-parser.rb    2017-09-28 09:15:45 +0900 (8b1a8bf)
+++ test/test-parser.rb    2017-11-09 13:50:05 +0900 (a45413b)
@@ -59,6 +59,47 @@ class ParserTest < Test::Unit::TestCase
                  statistics.collect(&:log_level))
   end
 
+  def test_extract_field_no_context_id
+    raw_statistic = {
+      :timestamp => Time.local(2017, 7, 19, 14, 9, 5, 663978),
+      :log_level => :notice,
+      :context_id => nil,
+      :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
+    LOG
+    assert_equal([raw_statistic],
+                 statistics.collect(&:to_h))
+  end
+
+  def test_log_level_no_context_id
+    expected = [
+      :emergency,
+      :alert,
+      :critical,
+      :error,
+      :warning,
+      :notice,
+      :information,
+      :debug,
+      :dump
+    ]
+    statistics = parse(<<-LOG)
+2017-07-19 14:41:05.663978|E| emergency
+2017-07-19 14:41:06.663978|A| alert
+2017-07-19 14:41:06.663978|C| critical
+2017-07-19 14:41:06.663978|e| error
+2017-07-19 14:41:06.663978|w| warning
+2017-07-19 14:41:06.663978|n| notice
+2017-07-19 14:41:06.663978|i| information
+2017-07-19 14:41:06.663978|d| debug
+2017-07-19 14:41:06.663978|-| dump
+    LOG
+    assert_equal(expected,
+                 statistics.collect(&:log_level))
+  end
+
   private
   def parse(log)
     parser = GroongaLog::Parser.new
-------------- next part --------------
HTML����������������������������...
URL: https://lists.osdn.me/mailman/archives/groonga-commit/attachments/20171109/09644d06/attachment-0001.htm 



More information about the Groonga-commit mailing list
Back to archive index