Created
September 28, 2011 03:03
-
-
Save zev/1246879 to your computer and use it in GitHub Desktop.
Buffered logging to handle encoding conversion failures
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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