mirror of
https://github.com/elastic/logstash.git
synced 2025-04-24 06:37:19 -04:00
- gracefully handle errors from log parsers
+ they can throw a LogStash::Log::LogParseError now - handle JSON parse exceptions
This commit is contained in:
parent
e838dfaee4
commit
fff7413057
3 changed files with 18 additions and 5 deletions
|
@ -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)
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue