From 61c7a1307e2c31267c6678d361d570db64cde2a1 Mon Sep 17 00:00:00 2001 From: Karl Southern Date: Thu, 7 Jul 2016 08:50:58 +0100 Subject: [PATCH] Provisionally address issue 46 --- lib/logstash/outputs/jdbc.rb | 44 ++++++++++++++++++++---------------- 1 file changed, 25 insertions(+), 19 deletions(-) diff --git a/lib/logstash/outputs/jdbc.rb b/lib/logstash/outputs/jdbc.rb index f811c51..34a5c11 100644 --- a/lib/logstash/outputs/jdbc.rb +++ b/lib/logstash/outputs/jdbc.rb @@ -286,10 +286,18 @@ class LogStash::Outputs::Jdbc < LogStash::Outputs::Base def add_statement_event_params(statement, event) @statement[1..-1].each_with_index do |i, idx| - case event[i] + value = event[i] + + if value.nil? and i.to_s =~ /%{/ + value = event.sprintf(i) + else + value = i + end + + case value when Time # See LogStash::Timestamp, below, for the why behind strftime. - statement.setString(idx + 1, event[i].strftime(STRFTIME_FMT)) + statement.setString(idx + 1, value.strftime(STRFTIME_FMT)) when LogStash::Timestamp # XXX: Using setString as opposed to setTimestamp, because setTimestamp # doesn't behave correctly in some drivers (Known: sqlite) @@ -298,23 +306,17 @@ class LogStash::Outputs::Jdbc < LogStash::Outputs::Base # choke on the 'T' in the string (Known: Derby). # # strftime appears to be the most reliable across drivers. - statement.setString(idx + 1, event[i].time.strftime(STRFTIME_FMT)) + statement.setString(idx + 1, value.time.strftime(STRFTIME_FMT)) when Fixnum, Integer - statement.setInt(idx + 1, event[i]) + statement.setInt(idx + 1, value) when Float - statement.setFloat(idx + 1, event[i]) + statement.setFloat(idx + 1, value) when String - statement.setString(idx + 1, event[i]) - when true - statement.setBoolean(idx + 1, true) - when false - statement.setBoolean(idx + 1, false) + statement.setString(idx + 1, value) + when true, false + statement.setBoolean(idx + 1, value) else - if event[i].nil? and i =~ /%\{/ - statement.setString(idx + 1, event.sprintf(i)) - else - statement.setString(idx + 1, nil) - end + statement.setString(idx + 1, nil) end end @@ -323,13 +325,17 @@ class LogStash::Outputs::Jdbc < LogStash::Outputs::Base def log_jdbc_exception(exception, retrying) current_exception = exception + log_text = 'JDBC Exception. ' + (retrying ? 'Retrying' : 'No retry') + '.' + loop do - if retrying - @logger.error("JDBC Exception. Retrying.", :exception => current_exception) + @logger.error(log_text, :exception => current_exception, :backtrace => current_exception.backtrace) + + if current_exception.respond_to? 'getNextException' + current_exception = current_exception.getNextException() else - @logger.error("JDBC Exception. No retry.", :exception => current_exception) + current_exception = nil end - current_exception = current_exception.getNextException() + break if current_exception == nil end end