Kentaro Hayashi
null+****@clear*****
Tue Sep 26 16:36:30 JST 2017
Kentaro Hayashi 2017-09-26 16:36:30 +0900 (Tue, 26 Sep 2017) New Revision: 13f4733630ba1d2cc831246b1d92bace46693ae0 https://github.com/groonga/groonga-log/commit/13f4733630ba1d2cc831246b1d92bace46693ae0 Merged ab8830c: Merge pull request #4 from kenhys/extract-parser-from-f-p-g-l Message: Implement GroongaLog::Parser This implementation is extracted from fluent-plugin-groonga-log. Added files: lib/groonga-log/parser.rb Added: lib/groonga-log/parser.rb (+84 -0) 100644 =================================================================== --- /dev/null +++ lib/groonga-log/parser.rb 2017-09-26 16:36:30 +0900 (694acb8) @@ -0,0 +1,84 @@ +# Copyright (C) 2017 Yasuhiro Horimoto <horimoto �� clear-code.com> +# Copyright (C) 2017 Kentaro Hayashi <hayashi �� clear-code.com> +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2.1 of the License, or (at your option) any later version. +# +# This library 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 +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + +module GroongaLog + class Parser + PATTERN = + /\A(?<year>\d{4})-(?<month>\d\d)-(?<day>\d\d) (?<hour>\d\d):(?<minutes>\d\d):(?<seconds>\d\d)\.(?<micro_seconds>\d+)\|(?<log_level>.)\|(?<context_id>.+?)\|(?<message>.*)/ + + 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) + + year = Integer(m['year']) + month = Integer(m['month']) + day = Integer(m['day']) + hour = Integer(m['hour']) + minutes = Integer(m['minutes']) + seconds = Integer(m['seconds']) + micro_seconds = Integer(m['micro_seconds']) + log_level = log_level_to_symbol(m['log_level']) + context_id = m['context_id'] + message = m['message'] + timestamp = Time.local(year, month, day, + hour, minutes, seconds, micro_seconds) + + record = { + :timestamp => timestamp, + :year => year, + :month => month, + :day => day, + :hour => hour, + :minutes => minutes, + :seconds => seconds, + :micro_seconds => micro_seconds, + :log_level => log_level, + :context_id => context_id, + :message => message, + } + yield record + end + end + + private + def log_level_to_symbol(level_text) + case level_text + when "E" + :emergency + when "A" + :alert + when "C" + :critical + when "e" + :error + when "w" + :warning + when "n" + :notice + when "i" + :information + when "d" + :debug + when "-" + :dump + end + end + end +end -------------- next part -------------- HTML����������������������������... URL: https://lists.osdn.me/mailman/archives/groonga-commit/attachments/20170926/9e5add37/attachment-0001.htm