class GroongaLog::Parser

Constants

PATH_TIMESTAMP_PATTERN
PATTERN

Attributes

current_path[R]

Public Class Methods

new() click to toggle source
# File lib/groonga-log/parser.rb, line 60
def initialize
  @current_path = nil
end
sort_paths(paths) click to toggle source
# File lib/groonga-log/parser.rb, line 46
def sort_paths(paths)
  paths.sort_by do |path|
    match_data = PATH_TIMESTAMP_PATTERN.match(File.basename(path))
    if match_data
      values = match_data.to_a[1..-1].collect(&:to_i)
      Time.local(*values)
    else
      Time.now
    end
  end
end
target_line?(line) click to toggle source
# File lib/groonga-log/parser.rb, line 36
def target_line?(line)
  if line.respond_to?(:valid_encoding?)
    return false unless line.valid_encoding?
  end

  return false unless PATTERN.match(line)

  true
end

Public Instance Methods

parse(input) { |entry| ... } click to toggle source
# File lib/groonga-log/parser.rb, line 64
def parse(input)
  return to_enum(:parse, input) unless block_given?

  input.each_line do |line|
    if line.respond_to?(:valid_encoding?)
      next unless line.valid_encoding?
    end

    match_data = PATTERN.match(line)
    next if match_data.nil?

    entry = Entry.new

    year = Integer(match_data[:year], 10)
    month = Integer(match_data[:month], 10)
    day = Integer(match_data[:day], 10)
    hour = Integer(match_data[:hour], 10)
    minute = Integer(match_data[:minute], 10)
    second = Integer(match_data[:second], 10)
    micro_second = Integer(match_data[:micro_second], 10)
    entry.timestamp = Time.local(year, month, day,
                                 hour, minute, second, micro_second)
    entry.log_level = log_level_to_symbol(match_data[:log_level])
    pid = match_data[:pid]
    entry.pid = Integer(pid, 10) if pid
    entry.thread_id = match_data[:thread_id]
    entry.message = match_data[:message]
    yield entry
  end
end
parse_paths(paths, &block) click to toggle source
# File lib/groonga-log/parser.rb, line 95
def parse_paths(paths, &block)
  return to_enum(__method__, paths) unless block_given?

  target_paths = self.class.sort_paths(paths)
  target_paths.each do |path|
    Input.open(path) do |log|
      @current_path = path
      begin
        parse(log, &block)
      ensure
        @current_path = nil
      end
    end
  end
end

Private Instance Methods

log_level_to_symbol(level_text) click to toggle source
# File lib/groonga-log/parser.rb, line 112
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