[Groonga-commit] groonga/grntest at 9cb22be [master] Add log parser

Zurück zum Archiv-Index

Kouhei Sutou null+****@clear*****
Sun Sep 28 12:34:19 JST 2014


Kouhei Sutou	2014-09-28 12:34:19 +0900 (Sun, 28 Sep 2014)

  New Revision: 9cb22bea37bca6386e7f4d5895cb9fc70d436fd5
  https://github.com/groonga/grntest/commit/9cb22bea37bca6386e7f4d5895cb9fc70d436fd5

  Message:
    Add log parser
    
    It supports multi-line message.

  Added files:
    lib/grntest/log-entry.rb
    lib/grntest/log-parser.rb
    test/test-log-parser.rb

  Added: lib/grntest/log-entry.rb (+19 -0) 100644
===================================================================
--- /dev/null
+++ lib/grntest/log-entry.rb    2014-09-28 12:34:19 +0900 (fe810ef)
@@ -0,0 +1,19 @@
+# Copyright (C) 2014  Kouhei Sutou <kou �� clear-code.com>
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+module Grntest
+  class LogEntry < Struct.new(:timestamp, :log_level, :message)
+  end
+end

  Added: lib/grntest/log-parser.rb (+44 -0) 100644
===================================================================
--- /dev/null
+++ lib/grntest/log-parser.rb    2014-09-28 12:34:19 +0900 (0aae288)
@@ -0,0 +1,44 @@
+# Copyright (C) 2014  Kouhei Sutou <kou �� clear-code.com>
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+require "grntest/log-entry"
+
+module Grntest
+  class LogParser
+    def parse(log)
+      timestamp = nil
+      log_level = nil
+      message = nil
+      emit_entry = lambda do
+        if timestamp
+          entry = LogEntry.new(timestamp, log_level, message.chomp)
+          yield(entry)
+        end
+      end
+      log.each_line do |line|
+        case line
+        when /\A(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d+)\|([a-zA-Z])\|\s*/
+          emit_entry.call
+          timestamp = $1
+          log_level = $2
+          message = $POSTMATCH
+        else
+          message << line
+        end
+      end
+      emit_entry.call
+    end
+  end
+end

  Added: test/test-log-parser.rb (+77 -0) 100644
===================================================================
--- /dev/null
+++ test/test-log-parser.rb    2014-09-28 12:34:19 +0900 (181e0a6)
@@ -0,0 +1,77 @@
+# Copyright (C) 2014  Kouhei Sutou <kou �� clear-code.com>
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+require "grntest/log-parser"
+
+class TestLogParser < Test::Unit::TestCase
+  def parse(log)
+    parser = Grntest::LogParser.new
+    entries = []
+    parser.parse(log) do |entry|
+      entries << entry.to_h
+    end
+    entries
+  end
+
+  sub_test_case("#parse") do
+    def test_one_line
+      log = "2014-09-27 17:31:53.046467|n| message"
+      assert_equal([
+                     {
+                       :timestamp => "2014-09-27 17:31:53.046467",
+                       :log_level => "n",
+                       :message   => "message",
+                     },
+                   ],
+                   parse(log))
+    end
+
+    def test_one_lines
+      log = <<-LOG
+2014-09-27 17:31:53.046467|n| notification message
+2014-09-27 17:31:54.046467|W| warning message
+      LOG
+      assert_equal([
+                     {
+                       :timestamp => "2014-09-27 17:31:53.046467",
+                       :log_level => "n",
+                       :message   => "notification message",
+                     },
+                     {
+                       :timestamp => "2014-09-27 17:31:54.046467",
+                       :log_level => "W",
+                       :message   => "warning message",
+                     },
+                   ],
+                   parse(log))
+    end
+
+
+    def test_multi_line
+      log = <<-LOG
+2014-09-27 17:31:53.046467|n| multi
+line message
+      LOG
+      assert_equal([
+                     {
+                       :timestamp => "2014-09-27 17:31:53.046467",
+                       :log_level => "n",
+                       :message   => "multi\nline message",
+                     },
+                   ],
+                   parse(log))
+    end
+  end
+end
-------------- next part --------------
HTML����������������������������...
Download 



More information about the Groonga-commit mailing list
Zurück zum Archiv-Index