Module: LogParser

Defined in:
lib/production_log/parser.rb

Overview

LogParser parses a Syslog log file looking for lines logged by the ‘rails’ program. A typical log line looks like this:

Mar  7 00:00:20 online1 rails[59600]: Person Load (0.001884)   SELECT * FROM people WHERE id = 10519 LIMIT 1

LogParser does not work with Rails’ default logger because there is no way to group all the log output of a single request. You must use SyslogLogger.

Defined Under Namespace

Classes: LogEntry

Class Method Summary collapse

Class Method Details

.parse(stream) ⇒ Object

Parses IO stream stream, creating a LogEntry for each recognizable log entry.

Log entries are recognised as starting with Processing, continuing with the same process id through Completed.



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/production_log/parser.rb', line 118

def self.parse(stream) # :yields: log_entry
    buckets = Hash.new { |h,k| h[k] = [] }
    stream.each_line do |line|
        line =~ / ([^ ]+) ([^ ]+)\[(\d+)\]: (.*)/
        next if $2 != 'rails'
        bucket = "#{$1}-#{$3}"
        data = $4

        buckets[bucket] << data

        if data =~ /^Completed/ then
            entry = buckets.delete bucket
            if entry.first =~ /^Processing/ then
                yield LogEntry.new(entry)
            end
        end
    end

    buckets.each do |bucket, data|
        yield LogEntry.new(data)
    end
end