Skip to content

Commit

Permalink
Add last_error method to FluentLogger. fix #11
Browse files Browse the repository at this point in the history
  • Loading branch information
repeatedly committed Nov 8, 2013
1 parent 17eabe7 commit e63a40e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
6 changes: 4 additions & 2 deletions README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ A structured event loger
require 'fluent-logger'

log = Fluent::Logger::FluentLogger.new(nil, :host=>'localhost', :port=>24224)
log.post("myapp.access", {"agent"=>"foo"})

unless log.post("myapp.access", {"agent"=>"foo"})
p log.last_error # You can get last error object via last_error method
end

# output: myapp.access {"agent":"foo"}

=== Singleton
Expand Down
19 changes: 13 additions & 6 deletions lib/fluent/logger/fluent_logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,15 +94,19 @@ def initialize(tag_prefix, *args)
end
end

@last_error = nil

begin
connect!
rescue
rescue => e
@last_error = e
@logger.error "Failed to connect fluentd: #{$!}"
@logger.error "Connection will be retried."
end
end

attr_accessor :limit, :logger, :log_reconnect_error_threshold
attr_reader :last_error

def post_with_time(tag, map, time)
@logger.debug { "event: #{tag} #{map.to_json}" rescue nil }
Expand All @@ -115,7 +119,8 @@ def close
if @pending
begin
send_data(@pending)
rescue
rescue => e
@last_error = e
@logger.error("FluentLogger: Can't send logs to #{@host}:#{@port}: #{$!}")
end
end
Expand Down Expand Up @@ -154,7 +159,8 @@ def suppress_sec
def write(msg)
begin
data = to_msgpack(msg)
rescue
rescue => e
@last_error = e
@logger.error("FluentLogger: Can't convert to msgpack: #{msg.inspect}: #{$!}")
return false
end
Expand All @@ -177,7 +183,8 @@ def write(msg)
send_data(@pending)
@pending = nil
true
rescue
rescue => e
@last_error = e
if @pending.bytesize > @limit
@logger.error("FluentLogger: Can't send logs to #{@host}:#{@port}: #{$!}")
@pending = nil
Expand Down Expand Up @@ -215,7 +222,7 @@ def connect!
@con.sync = true
@connect_error_history.clear
@logged_reconnect_error = false
rescue
rescue => e
@connect_error_history << Time.now.to_i
if @connect_error_history.size > RECONNECT_WAIT_MAX_COUNT
@connect_error_history.shift
Expand All @@ -226,7 +233,7 @@ def connect!
@logged_reconnect_error = true
end

raise
raise e
end

def log_reconnect_error
Expand Down

0 comments on commit e63a40e

Please sign in to comment.