Skip to content

Instantly share code, notes, and snippets.

@zev
Created September 28, 2011 03:03
Show Gist options
  • Save zev/1246879 to your computer and use it in GitHub Desktop.
Save zev/1246879 to your computer and use it in GitHub Desktop.
Buffered logging to handle encoding conversion failures
def flush
@guard.synchronize do
unless buffer.empty?
old_buffer = buffer
begin
@log.write(old_buffer.join)
rescue Encoding::UndefinedConversionError
puts "Enc err"
@log.write(old_buffer.map { |b| b.force_encoding(Encoding.default_internal) }.join)
puts "Enc Done"
end
end
# Important to do this even if buffer was empty or else @buffer will
# accumulate empty arrays for each request where nothing was logged.
clear_buffer
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment