Add grep's context option to show lines before and after a match

This commit is contained in:
Nick Hammond
2024-05-24 08:59:33 -07:00
parent 10b8c826d8
commit 89994c8b20
12 changed files with 121 additions and 21 deletions

View File

@@ -150,22 +150,24 @@ class Kamal::Cli::Accessory < Kamal::Cli::Base
option :lines, type: :numeric, aliases: "-n", desc: "Number of log lines to pull from each server"
option :grep, aliases: "-g", desc: "Show lines with grep match only (use this to fetch specific requests by id)"
option :follow, aliases: "-f", desc: "Follow logs on primary server (or specific host set by --hosts)"
option :context, aliases: "-C", desc: "Show number of lines leading and trailing a grep match (use with --grep)"
def logs(name)
with_accessory(name) do |accessory, hosts|
grep = options[:grep]
context = options[:context]
if options[:follow]
run_locally do
info "Following logs on #{hosts}..."
info accessory.follow_logs(grep: grep)
exec accessory.follow_logs(grep: grep)
info accessory.follow_logs(grep: grep, context: context)
exec accessory.follow_logs(grep: grep, context: context)
end
else
since = options[:since]
lines = options[:lines].presence || ((since || grep) ? nil : 100) # Default to 100 lines if since or grep isn't set
on(hosts) do
puts capture_with_info(*accessory.logs(since: since, lines: lines, grep: grep))
puts capture_with_info(*accessory.logs(since: since, lines: lines, grep: grep, context: context))
end
end
end

View File

@@ -167,11 +167,14 @@ class Kamal::Cli::App < Kamal::Cli::Base
option :lines, type: :numeric, aliases: "-n", desc: "Number of lines to show from each server"
option :grep, aliases: "-g", desc: "Show lines with grep match only (use this to fetch specific requests by id)"
option :follow, aliases: "-f", desc: "Follow log on primary server (or specific host set by --hosts)"
option :context, aliases: "-C", desc: "Show number of lines leading and trailing a grep match (use with --grep)"
def logs
# FIXME: Catch when app containers aren't running
grep = options[:grep]
context = options[:context]
since = options[:since]
if options[:follow]
lines = options[:lines].presence || ((since || grep) ? nil : 10) # Default to 10 lines if since or grep isn't set
@@ -182,8 +185,8 @@ class Kamal::Cli::App < Kamal::Cli::Base
role = KAMAL.roles_on(KAMAL.primary_host).first
app = KAMAL.app(role: role, host: host)
info app.follow_logs(host: KAMAL.primary_host, lines: lines, grep: grep)
exec app.follow_logs(host: KAMAL.primary_host, lines: lines, grep: grep)
info app.follow_logs(host: KAMAL.primary_host, lines: lines, grep: grep, context: context)
exec app.follow_logs(host: KAMAL.primary_host, lines: lines, grep: grep, context: context)
end
else
lines = options[:lines].presence || ((since || grep) ? nil : 100) # Default to 100 lines if since or grep isn't set
@@ -193,7 +196,7 @@ class Kamal::Cli::App < Kamal::Cli::Base
roles.each do |role|
begin
puts_by_host host, capture_with_info(*KAMAL.app(role: role, host: host).logs(since: since, lines: lines, grep: grep))
puts_by_host host, capture_with_info(*KAMAL.app(role: role, host: host).logs(since: since, lines: lines, grep: grep, context: context))
rescue SSHKit::Command::Failed
puts_by_host host, "Nothing found"
end

View File

@@ -70,21 +70,23 @@ class Kamal::Cli::Traefik < Kamal::Cli::Base
option :lines, type: :numeric, aliases: "-n", desc: "Number of log lines to pull from each server"
option :grep, aliases: "-g", desc: "Show lines with grep match only (use this to fetch specific requests by id)"
option :follow, aliases: "-f", desc: "Follow logs on primary server (or specific host set by --hosts)"
option :context, aliases: "-C", desc: "Show number of lines leading and trailing a grep match (use with --grep)"
def logs
grep = options[:grep]
context = options[:context]
if options[:follow]
run_locally do
info "Following logs on #{KAMAL.primary_host}..."
info KAMAL.traefik.follow_logs(host: KAMAL.primary_host, grep: grep)
exec KAMAL.traefik.follow_logs(host: KAMAL.primary_host, grep: grep)
info KAMAL.traefik.follow_logs(host: KAMAL.primary_host, grep: grep, context: context)
exec KAMAL.traefik.follow_logs(host: KAMAL.primary_host, grep: grep, context: context)
end
else
since = options[:since]
lines = options[:lines].presence || ((since || grep) ? nil : 100) # Default to 100 lines if since or grep isn't set
on(KAMAL.traefik_hosts) do |host|
puts_by_host host, capture(*KAMAL.traefik.logs(since: since, lines: lines, grep: grep)), type: "Traefik"
puts_by_host host, capture(*KAMAL.traefik.logs(since: since, lines: lines, grep: grep, context: context)), type: "Traefik"
end
end
end

View File

@@ -36,17 +36,17 @@ class Kamal::Commands::Accessory < Kamal::Commands::Base
end
def logs(since: nil, lines: nil, grep: nil)
def logs(since: nil, lines: nil, grep: nil, context: nil)
pipe \
docker(:logs, service_name, (" --since #{since}" if since), (" --tail #{lines}" if lines), "--timestamps", "2>&1"),
("grep '#{grep}'" if grep)
("grep '#{grep}'#{" -C #{context}" if context}" if grep)
end
def follow_logs(grep: nil)
def follow_logs(grep: nil, context: nil)
run_over_ssh \
pipe \
docker(:logs, service_name, "--timestamps", "--tail", "10", "--follow", "2>&1"),
(%(grep "#{grep}") if grep)
(%(grep "#{grep}"#{" -C #{context}" if context}) if grep)
end

View File

@@ -1,17 +1,17 @@
module Kamal::Commands::App::Logging
def logs(version: nil, since: nil, lines: nil, grep: nil)
def logs(version: nil, since: nil, lines: nil, grep: nil, context: nil)
pipe \
version ? container_id_for_version(version) : current_running_container_id,
"xargs docker logs#{" --since #{since}" if since}#{" --tail #{lines}" if lines} 2>&1",
("grep '#{grep}'" if grep)
("grep '#{grep}'#{" -C #{context}" if context}" if grep)
end
def follow_logs(host:, lines: nil, grep: nil)
def follow_logs(host:, lines: nil, grep: nil, context: nil)
run_over_ssh \
pipe(
current_running_container_id,
"xargs docker logs --timestamps#{" --tail #{lines}" if lines} --follow 2>&1",
(%(grep "#{grep}") if grep)
(%(grep "#{grep}"#{" -C #{context}" if context}) if grep)
),
host: host
end

View File

@@ -46,16 +46,16 @@ class Kamal::Commands::Traefik < Kamal::Commands::Base
docker :ps, "--filter", "name=^traefik$"
end
def logs(since: nil, lines: nil, grep: nil)
def logs(since: nil, lines: nil, grep: nil, context: nil)
pipe \
docker(:logs, "traefik", (" --since #{since}" if since), (" --tail #{lines}" if lines), "--timestamps", "2>&1"),
("grep '#{grep}'" if grep)
("grep '#{grep}'#{" -C #{context}" if context}" if grep)
end
def follow_logs(host:, grep: nil)
def follow_logs(host:, grep: nil, context: nil)
run_over_ssh pipe(
docker(:logs, "traefik", "--timestamps", "--tail", "10", "--follow", "2>&1"),
(%(grep "#{grep}") if grep)
(%(grep "#{grep}"#{" -C #{context}" if context}) if grep)
).join(" "), host: host
end