Skip to content

Instantly share code, notes, and snippets.

@ddrscott
Last active March 21, 2016 11:54
Show Gist options
  • Save ddrscott/0f0f102625bdb04e5fb2 to your computer and use it in GitHub Desktop.
Save ddrscott/0f0f102625bdb04e5fb2 to your computer and use it in GitHub Desktop.
Simple script to handle multi-line logs
#!/usr/bin/env ruby
# # Simple script to handle multi-line logs.
# Adjust the global vars as needed.
#
# # Usage
#
# > chmod +x log_pipe.rb
# > tail -f log/development.log | ./log_pipe.rb
#
# TODO read some options from command line.
# Pattern used to detect the beginning of a log group.
$group_pattern = /^\d{4}.*\|/
# Ignore lines that match this pattern.
# This one will filter out DEBUG logs with timing metics,
# which happens to filter out SQL logs.
# A little more surgical would be to look for SQL keywords.
$skip_pattern = /DEBUG\|[^\(]+\(\d+\.?\d*?ms\)/
# Hate ANSI colors? This pattern kills them.
# Set to `false` if you don't hate colors.
$ansi_escape_pattern = /\e\[[^m]+m/
$redirect = $stdout
def grouped(lines)
first = lines.first
return if first.nil? || first =~ $skip_pattern
# strip ANSI escapes
if $ansi_escape_pattern
lines = lines.map { |l| l.gsub($ansi_escape_pattern, '') }
end
$redirect.puts lines
end
line_group = []
ARGF.each_line do |line|
if line =~ $group_pattern
grouped(line_group)
line_group = [line]
else
line_group << line
end
end
grouped(line_group)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment