| 1 |
# Copyright (C) 2017 Yasuhiro Horimoto <[email protected]> |
| 2 |
# Copyright (C) 2017 Kentaro Hayashi <[email protected]> |
| 3 |
# |
| 4 |
# This library is free software; you can redistribute it and/or |
| 5 |
# modify it under the terms of the GNU Lesser General Public |
| 6 |
# License as published by the Free Software Foundation; either |
| 7 |
# version 2.1 of the License, or (at your option) any later version. |
| 8 |
# |
| 9 |
# This library is distributed in the hope that it will be useful, |
| 10 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 |
# Lesser General Public License for more details. |
| 13 |
# |
| 14 |
# You should have received a copy of the GNU Lesser General Public |
| 15 |
# License along with this library; if not, write to the Free Software |
| 16 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 17 |
|
| 18 |
require "groonga-log/statistic" |
| 19 |
|
| 20 |
module GroongaLog |
| 21 |
class Parser |
| 22 |
PATTERN = |
| 23 |
/\A(?<year>\d{4})-(?<month>\d\d)-(?<day>\d\d) |
| 24 |
\ (?<hour>\d\d):(?<minute>\d\d):(?<second>\d\d)\.(?<micro_second>\d+) |
| 25 |
\|(?<log_level>.) |
| 26 |
\|(?:(?<pid>\d+):)? |
| 27 |
\ (?<message>.*)/x |
| 28 |
|
| 29 |
def parse(input) |
| 30 |
return to_enum(:parse, input) unless block_given? |
| 31 |
|
| 32 |
input.each_line do |line| |
| 33 |
if line.respond_to?(:valid_encoding?) |
| 34 |
next unless line.valid_encoding? |
| 35 |
end |
| 36 |
|
| 37 |
m = PATTERN.match(line) |
| 38 |
|
| 39 |
statistic = Statistic.new |
| 40 |
|
| 41 |
year = m["year"].to_i |
| 42 |
month = m["month"].to_i |
| 43 |
day = m["day"].to_i |
| 44 |
hour = m["hour"].to_i |
| 45 |
minute = m["minute"].to_i |
| 46 |
second = m["second"].to_i |
| 47 |
micro_second = m["micro_second"].to_i |
| 48 |
statistic.timestamp = Time.local(year, month, day, |
| 49 |
hour, minute, second, micro_second) |
| 50 |
statistic.log_level = log_level_to_symbol(m["log_level"]) |
| 51 |
statistic.pid = m["pid"].to_i if m["pid"] |
| 52 |
statistic.message = m["message"] |
| 53 |
yield statistic |
| 54 |
end |
| 55 |
end |
| 56 |
|
| 57 |
private |
| 58 |
def log_level_to_symbol(level_text) |
| 59 |
case level_text |
| 60 |
when "E" |
| 61 |
:emergency |
| 62 |
when "A" |
| 63 |
:alert |
| 64 |
when "C" |
| 65 |
:critical |
| 66 |
when "e" |
| 67 |
:error |
| 68 |
when "w" |
| 69 |
:warning |
| 70 |
when "n" |
| 71 |
:notice |
| 72 |
when "i" |
| 73 |
:information |
| 74 |
when "d" |
| 75 |
:debug |
| 76 |
when "-" |
| 77 |
:dump |
| 78 |
end |
| 79 |
end |
| 80 |
end |
| 81 |
end |