Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: try hard to log Java cause (chain) #62

Merged
merged 3 commits into from
Apr 15, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions lib/logstash/plugin_mixins/jdbc/jdbc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -122,16 +122,32 @@ def jdbc_connect
# rescue Java::JavaSql::SQLException, ::Sequel::Error => e
rescue ::Sequel::Error => e
if retry_attempts <= 0
@logger.error("Unable to connect to database. Tried #{@connection_retry_attempts} times", :error_message => e.message)
log_java_exception(e.cause)
@logger.error("Unable to connect to database. Tried #{@connection_retry_attempts} times", error_details(e, trace: true))
raise e
else
@logger.error("Unable to connect to database. Trying again", :error_message => e.message)
@logger.error("Unable to connect to database. Trying again", error_details(e, trace: false))
end
end
sleep(@connection_retry_attempts_wait_time)
end
end

def error_details(e, trace: false)
details = { :message => e.message, :exception => e.class }
details[:backtrace] = e.backtrace if trace || @logger.debug?
details[:cause] = e.cause if e.cause
details
end

def log_java_exception(e)
return unless e.is_a?(java.lang.Exception)
# @logger.name using the same convention as LS does
logger = self.class.name.gsub('::', '.').downcase
logger = org.apache.logging.log4j.LogManager.getLogger(logger)
logger.error('', e) # prints nested causes
end

def open_jdbc_connection
# at this point driver is already loaded
Sequel.application_timezone = @plugin_timezone.to_sym
Expand Down