Class: GroongaLog::Parser
- Inherits:
-
Object
- Object
- GroongaLog::Parser
- Defined in:
- lib/groonga-log/parser.rb
Constant Summary collapse
- 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>.)\| (?:(?<pid>\d+)[|:])? (?:(?<thread_id>[\da-fA-F]+)[|:])? \ ?(?<message>[^\r\n]*)/x
- PATH_TIMESTAMP_PATTERN =
/(\d{4})-(\d{2})-(\d{2})- (\d{2})-(\d{2})-(\d{2})-(\d{6}) (?:\.(?:gz|zip))?\z/xi
Instance Attribute Summary collapse
-
#current_path ⇒ Object
readonly
Returns the value of attribute current_path.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize ⇒ Parser
constructor
A new instance of Parser.
- #parse(input) ⇒ Object
- #parse_paths(paths, &block) ⇒ Object
Constructor Details
#initialize ⇒ Parser
Returns a new instance of Parser.
60 61 62 |
# File 'lib/groonga-log/parser.rb', line 60 def initialize @current_path = nil end |
Instance Attribute Details
#current_path ⇒ Object (readonly)
Returns the value of attribute current_path.
59 60 61 |
# File 'lib/groonga-log/parser.rb', line 59 def current_path @current_path end |
Class Method Details
.sort_paths(paths) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 |
# 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) ⇒ Boolean
36 37 38 39 40 41 42 43 44 |
# 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 |
Instance Method Details
#parse(input) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# 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. = 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. = match_data[:message] yield entry end end |
#parse_paths(paths, &block) ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# 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 |