Skip to content

Instantly share code, notes, and snippets.

@movstox
Last active November 15, 2019 21:38
Show Gist options
  • Save movstox/a1325051779c59fe7dc616b21575438d to your computer and use it in GitHub Desktop.
Save movstox/a1325051779c59fe7dc616b21575438d to your computer and use it in GitHub Desktop.
Naive log watcher for Rails
# ruby tail_rails_log.rb log/development.log
require 'bundler/inline'
gemfile do
source 'https://rubygems.org'
gem 'file-tail'
end
puts 'Watching for errors: ' + ARGV[0]
lines_to_catch = 0
start_lines = []
File.open(ARGV[0]) do |log|
log.extend(File::Tail)
log.backward(1).tail do |log_line|
if lines_to_catch.positive?
puts log_line
lines_to_catch -= 1
puts '--------------<<<<<<<' if lines_to_catch.zero?
end
if log_line =~ /^Started/
start_lines = []
start_lines << log_line
end
if log_line =~ /^Processing/
start_lines << log_line
end
if log_line =~ /Completed 500/ || log_line =~/Error/
lines_to_catch = 15
puts '>>>>>>>--------------'
start_lines.each { |l| puts l }
puts '---------------------'
puts log_line
puts '>-------------------<'
end
end
end
# >>>>>>>--------------
# Started GET "/asfd" for 127.0.0.1 at 2019-11-15 23:36:50 +0200
# ---------------------
# ActionController::RoutingError (No route matches [GET] "/asfd"):
# >-------------------<
# actionpack (5.2.2) lib/action_dispatch/middleware/debug_exceptions.rb:65:in `call'
# rack-contrib (2.1.0) lib/rack/contrib/response_headers.rb:17:in `call'
# meta_request (0.6.0) lib/meta_request/middlewares/headers.rb:16:in `call'
# web-console (3.7.0) lib/web_console/middleware.rb:135:in `call_app'
# web-console (3.7.0) lib/web_console/middleware.rb:30:in `block in call'
# web-console (3.7.0) lib/web_console/middleware.rb:20:in `catch'
# web-console (3.7.0) lib/web_console/middleware.rb:20:in `call'
# actionpack (5.2.2) lib/action_dispatch/middleware/show_exceptions.rb:33:in `call'
# railties (5.2.2) lib/rails/rack/logger.rb:38:in `call_app'
# railties (5.2.2) lib/rails/rack/logger.rb:26:in `block in call'
# activesupport (5.2.2) lib/active_support/tagged_logging.rb:71:in `block in tagged'
# activesupport (5.2.2) lib/active_support/tagged_logging.rb:28:in `tagged'
# activesupport (5.2.2) lib/active_support/tagged_logging.rb:71:in `tagged'
# railties (5.2.2) lib/rails/rack/logger.rb:26:in `call'
# --------------<<<<<<<
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment