Skip to content

Instantly share code, notes, and snippets.

@johnthethird
Created December 16, 2009 00:49
Show Gist options
  • Save johnthethird/257472 to your computer and use it in GitHub Desktop.
Save johnthethird/257472 to your computer and use it in GitHub Desktop.
# Read the log into an array.
@mylog = IO.readlines("path/to/log/file.log")
Now, if you want to get rid of the ANSI escape codes from the log (assuming you are accessing your application/database logs) you will have to use regular expressions. There are two ways of doing this. The first is to get rid of the codes after you read the log file, or do it as you display.
Here are the examples:
# Read the log into an array.
@mylog = IO.readlines("/path/to/log/file.log")
# Loop though all the log entries.
for i in 0..@mylog.size
# Remove the escape codes.
@mylog[i] = @mylog[i].gsub(/\e\[[0-9]{0,2}?[;]?[0-9]{0,2}?[;]?[0-9]{0,2}[A-Z]/, '')
end
Or, better to make it faster do it in your view. This might be faster, but it makes it more confusing. I don't think gsubs should be in the view. But its however you want to do it.
<% for entry in @mylog %>
<div class="log-line">
<%= entry.gsub(/\e\[[0-9]{0,2}?[;]?[0-9]{0,2}?[;]?[0-9]{0,2}[A-Z]/, '') %>
</div>
<% end %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment