From a342b565e868767880f3395f88f23c15dcd3d5f9 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Tue, 17 Jan 2023 14:11:27 +0100 Subject: [PATCH] Add grep and line configuration to logs --- lib/mrsk/cli/app.rb | 12 +++++++++++- lib/mrsk/commands/app.rb | 6 ++++-- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/lib/mrsk/cli/app.rb b/lib/mrsk/cli/app.rb index ba2341b2..10b0c50e 100644 --- a/lib/mrsk/cli/app.rb +++ b/lib/mrsk/cli/app.rb @@ -79,9 +79,19 @@ class Mrsk::Cli::App < Mrsk::Cli::Base end desc "logs", "Show last 100 log lines from app on servers" + option :lines, type: :numeric, default: 100, desc: "Number of log lines to pull from each server" + option :grep, desc: "Show lines with grep match only (use this to fetch specific requests by id)" def logs # FIXME: Catch when app containers aren't running - on(MRSK.config.hosts) { |host| puts "App Host: #{host}\n" + capture(*MRSK.app.logs) + "\n\n" } + lines = options[:lines] + grep = options[:grep] + on(MRSK.config.hosts) do |host| + begin + puts "App Host: #{host}\n" + capture(*MRSK.app.logs(lines: lines, grep: grep), verbosity: Logger::INFO) + "\n\n" + rescue SSHKit::Command::Failed + puts "App Host: #{host}\nNothing found\n\n" + end + end end desc "remove", "Remove app containers and images from servers" diff --git a/lib/mrsk/commands/app.rb b/lib/mrsk/commands/app.rb index 8da324f5..406699f4 100644 --- a/lib/mrsk/commands/app.rb +++ b/lib/mrsk/commands/app.rb @@ -27,8 +27,10 @@ class Mrsk::Commands::App < Mrsk::Commands::Base docker :ps, *service_filter end - def logs - [ "docker ps -q #{service_filter.join(" ")} | xargs docker logs -n 100 -t" ] + def logs(lines: 100, grep: nil) + [ "docker ps -q #{service_filter.join(" ")} | xargs docker logs -n #{lines} -t" ].tap do |command| + command.first << " | grep #{grep}" if grep + end end def exec(*command, interactive: false)