- gracefully handle errors from log parsers

+ they can throw a LogStash::Log::LogParseError now
- handle JSON parse exceptions
This commit is contained in:
Pete Fritchman 2009-11-08 05:07:12 +00:00
parent e838dfaee4
commit fff7413057
3 changed files with 18 additions and 5 deletions

View file

@ -15,6 +15,8 @@ module LogStash
:pattern_dir]
attr_accessor :attrs
LogParseError = Class.new(StandardError)
def initialize(config)
check_hash_keys(config, REQUIRED_KEYS, OPTIONAL_KEYS)

View file

@ -23,8 +23,11 @@ module LogStash
end
def parse_entry(raw_entry)
# need to add @LINE
res = LogStash::Util::collapse(JSON.parse(raw_entry))
begin
res = LogStash::Util::collapse(JSON.parse(raw_entry))
rescue JSON::ParserError
raise LogParseError.new("Invalid JSON: #{$!}: #{raw_entry}")
end
res["@LINE"] = template(@line_format, res)
fix_date(res)

View file

@ -55,11 +55,19 @@ module LogStash; module Net; module Servers
end
log_type = request.log_type
entry = @config.logs[log_type].parse_entry(request.log_data)
entry = nil
reason = "unknown; parse_entry returned without an exception"
begin
entry = @config.logs[log_type].parse_entry(request.log_data)
rescue LogStash::Log::LogParseError
reason = $!
end
if !entry
@logger.warn "Failed parsing line: #{request.log_data}"
@logger.warn "Failed parsing line: #{reason}: #{request.log_data}"
response.code = 1
response.error = "Entry was #{entry.inspect} (log parsing failed)"
response.error = "Entry was #{entry.inspect} (log parsing " \
"failed: #{reason})"
entry = {
"@NEEDSPARSING" => 1,
"@LINE" => request.log_data