Class: SelectRailsLog::Scanner
- Inherits:
-
Object
- Object
- SelectRailsLog::Scanner
- Includes:
- Constants
- Defined in:
- lib/select_rails_log/scanner.rb
Constant Summary collapse
- LOG_REGEXP =
/\A., \[(?<time>\S+) #(?<pid>\d+)\] *(?<severity>\S+) -- :(?: #{reqid_regexp})? (?<message>.*)/- ANSI_ESCAPE_SEQ_REGEXP =
/\e\[(?:\d{1,2}(?:;\d{1,2})?)?[mK]/
Constants included from Constants
Constants::ACTION, Constants::CLIENT, Constants::COMPLETED, Constants::CONTROLLER, Constants::DEBUG, Constants::DEFAULT_OUTPUT, Constants::DURATION, Constants::HTTP_METHOD, Constants::HTTP_STATUS, Constants::ID, Constants::INTERVAL, Constants::LOGS, Constants::MESSAGE, Constants::PARAMETERS, Constants::PATH, Constants::PERFORMANCE, Constants::PERFORMANCE_ACTIVE_RECORD, Constants::PERFORMANCE_ALLOCATIONS, Constants::PERFORMANCE_VIEWS, Constants::PID, Constants::RAW_LOGS, Constants::REQUEST_ID, Constants::SEVERITY, Constants::STARTED, Constants::TIME
Instance Method Summary collapse
-
#initialize(io) ⇒ Scanner
constructor
A new instance of Scanner.
- #select(selector, keep_raw: true) ⇒ Object
Constructor Details
#initialize(io) ⇒ Scanner
Returns a new instance of Scanner.
17 18 19 |
# File 'lib/select_rails_log/scanner.rb', line 17 def initialize(io) @io = io end |
Instance Method Details
#select(selector, keep_raw: true) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 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 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/select_rails_log/scanner.rb', line 21 def select(selector, keep_raw: true) buff = {} prev_time = nil prev_data = nil found = false stop_iteration = false @io.each_line do |line| m = LOG_REGEXP.match(line) unless m if prev_data && prev_data[LOGS].any? prev_data[LOGS].last[MESSAGE] << "\n" << line.chomp.gsub(ANSI_ESCAPE_SEQ_REGEXP, "") prev_data[RAW_LOGS].last << line if keep_raw end next end begin time = Time.strptime(m[:time], DATETIME_FORMAT) rescue ArgumentError # ignore invalid time format end pid = m[:pid] reqid = m[:reqid] = m[:message] log = { TIME => time, MESSAGE => , SEVERITY => m[:severity] } ident = reqid || pid data = prev_data = buff[ident] if buff.key?(ident) if /\A(?:\[.*\] )?Started (?<http_method>\S+) "(?<path>[^"]*)" for (?<client>\S+)/ =~ buff.delete(ident) if stop_iteration prev_data = nil buff.empty? ? break : next end log[INTERVAL] = 0.0 prev_time = time data = { ID => reqid || time.strftime("%Y%m%d-%H%M%S-%6N-#{pid}"), STARTED => time, PID => pid, REQUEST_ID => reqid, HTTP_METHOD => http_method, PATH => path, CLIENT => client, LOGS => [log], REQUEST_FILTER_APPLIED => false } data[RAW_LOGS] = [line] if keep_raw buff[ident] = data next end unless data prev_data = nil next end .gsub!(ANSI_ESCAPE_SEQ_REGEXP, "") log[INTERVAL] = (time && prev_time) ? time - prev_time : 0.0 prev_time = time if /\A(?:\[.*\] )?Processing by (?<controller>[^\s#]+)#(?<action>\S+)/ =~ data[CONTROLLER] = controller data[ACTION] = action data[LOGS] << log data[RAW_LOGS] << line if keep_raw data.delete(REQUEST_FILTER_APPLIED) begin reqf_result = selector.run_request_filters(data) rescue StopIteration stop_iteration = true end if !reqf_result || stop_iteration buff.delete(ident) prev_data = nil end elsif /\A(?:\[.*\] )? Parameters: (?<params>.*)/ =~ data[PARAMETERS] = params data[LOGS] << log data[RAW_LOGS] << line if keep_raw elsif /\A(?:\[.*\] )?Completed (?<http_status>\d+) .* in (?<duration>\d+)ms \((?<durations>.*)\)/ =~ data[HTTP_STATUS] = http_status data[DURATION] = duration.to_i data[PERFORMANCE] = durations.scan(/(\S+): (\d+(\.\d+)?)/) .to_h { |type, dur, dur_f| [type, dur_f ? dur.to_f : dur.to_i] } data[COMPLETED] = time data[LOGS] << log data[RAW_LOGS] << line if keep_raw if data.key?(REQUEST_FILTER_APPLIED) data.delete(REQUEST_FILTER_APPLIED) reqf_result = selector.run_request_filters(data) else reqf_result = true end reqf_result && selector.run_line_filters(data) do |i| yield(i) found = true end buff.delete(ident) prev_data = nil else data[LOGS] << log data[RAW_LOGS] << line if keep_raw end end found end |