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

Unify error logging #82

Merged
merged 2 commits into from
Jul 11, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 2 additions & 0 deletions lib/logstash-logger/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ def configure(&block)
class Configuration
attr_accessor :customize_event_block
attr_accessor :max_message_size
attr_accessor :default_error_logger

def initialize(*args)
@customize_event_block = nil
@default_error_logger = Logger.new($stderr)

yield self if block_given?
self
Expand Down
14 changes: 13 additions & 1 deletion lib/logstash-logger/device/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ module Device
class Base
attr_reader :io
attr_accessor :sync
attr_accessor :error_logger

def initialize(opts={})
@sync = opts[:sync]
@error_logger = opts.fetch(:error_logger, LogStashLogger.configuration.default_error_logger)
end

def to_io
Expand All @@ -23,10 +25,20 @@ def flush
def close
@io && @io.close
rescue => e
warn "#{self.class} - #{e.class} - #{e.message}"
log_error(e)
ensure
@io = nil
end

private

def log_error(e)
error_logger.error "[#{self.class}] #{e.class} - #{e.message}"
end

def log_warning(message)
error_logger.warn("[#{self.class}] #{message}")
end
end
end
end
2 changes: 1 addition & 1 deletion lib/logstash-logger/device/connectable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def with_connection(&block)
connect unless connected?
yield
rescue => e
warn "#{self.class} - #{e.class} - #{e.message}"
log_error(e)
@io = nil
raise
end
Expand Down
8 changes: 5 additions & 3 deletions lib/logstash-logger/device/kafka.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,14 @@ def with_connection
connect unless @io
yield
rescue ::Poseidon::Errors::ChecksumError, Poseidon::Errors::UnableToFetchMetadata => e
warn "#{self.class} - #{e.class} -> reconnect/retry"
log_error(e)
log_warning("reconnect/retry")
sleep backoff if backoff
reconnect
retry
rescue => e
warn "#{self.class} - #{e.class} - #{e.message} -> giving up"
log_error(e)
log_warning("giving up")
@io = nil
end

Expand All @@ -55,7 +57,7 @@ def close
buffer_flush(final: true)
@io && @io.close
rescue => e
warn "#{self.class} - #{e.class} - #{e.message}"
log_error(e)
ensure
@io = nil
end
Expand Down
4 changes: 2 additions & 2 deletions lib/logstash-logger/device/redis.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def with_connection
reconnect
retry
rescue => e
warn "#{self.class} - #{e.class} - #{e.message}"
log_error(e)
@io = nil
raise
end
Expand All @@ -47,7 +47,7 @@ def close
buffer_flush(final: true)
@io && @io.quit
rescue => e
warn "#{self.class} - #{e.class} - #{e.message}"
log_error(e)
ensure
@io = nil
end
Expand Down