From 0d709a3fdb929d914a612b4ad70fc336a2b5e579 Mon Sep 17 00:00:00 2001 From: Ahmed Al Hafoudh Date: Mon, 8 Jan 2024 09:34:38 +0100 Subject: [PATCH] Allow lines option to be configured when following app logs --- lib/kamal/cli/app.rb | 9 +++++---- lib/kamal/commands/app/logging.rb | 4 ++-- test/commands/app_test.rb | 8 ++++++++ 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/lib/kamal/cli/app.rb b/lib/kamal/cli/app.rb index fae607fc..390b2ae9 100644 --- a/lib/kamal/cli/app.rb +++ b/lib/kamal/cli/app.rb @@ -202,19 +202,20 @@ class Kamal::Cli::App < Kamal::Cli::Base # FIXME: Catch when app containers aren't running grep = options[:grep] - + 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 + run_locally do info "Following logs on #{KAMAL.primary_host}..." KAMAL.specific_roles ||= ["web"] role = KAMAL.roles_on(KAMAL.primary_host).first - info KAMAL.app(role: role).follow_logs(host: KAMAL.primary_host, grep: grep) - exec KAMAL.app(role: role).follow_logs(host: KAMAL.primary_host, grep: grep) + info KAMAL.app(role: role).follow_logs(host: KAMAL.primary_host, lines: lines, grep: grep) + exec KAMAL.app(role: role).follow_logs(host: KAMAL.primary_host, lines: lines, grep: grep) 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.hosts) do |host| diff --git a/lib/kamal/commands/app/logging.rb b/lib/kamal/commands/app/logging.rb index d20eda2c..7e760512 100644 --- a/lib/kamal/commands/app/logging.rb +++ b/lib/kamal/commands/app/logging.rb @@ -6,11 +6,11 @@ module Kamal::Commands::App::Logging ("grep '#{grep}'" if grep) end - def follow_logs(host:, grep: nil) + def follow_logs(host:, lines: nil, grep: nil) run_over_ssh \ pipe( current_running_container_id, - "xargs docker logs --timestamps --tail 10 --follow 2>&1", + "xargs docker logs --timestamps#{" --tail #{lines}" if lines} --follow 2>&1", (%(grep "#{grep}") if grep) ), host: host diff --git a/test/commands/app_test.rb b/test/commands/app_test.rb index c2e335a3..450f1326 100644 --- a/test/commands/app_test.rb +++ b/test/commands/app_test.rb @@ -151,6 +151,14 @@ class CommandsAppTest < ActiveSupport::TestCase assert_match \ "docker ps --quiet --filter label=service=app --filter label=role=web --filter status=running --filter status=restarting --latest | xargs docker logs --timestamps --tail 10 --follow 2>&1 | grep \"Completed\"", new_command.follow_logs(host: "app-1", grep: "Completed") + + assert_match \ + "docker ps --quiet --filter label=service=app --filter label=role=web --filter status=running --filter status=restarting --latest | xargs docker logs --timestamps --tail 123 --follow 2>&1", + new_command.follow_logs(host: "app-1", lines: 123) + + assert_match \ + "docker ps --quiet --filter label=service=app --filter label=role=web --filter status=running --filter status=restarting --latest | xargs docker logs --timestamps --follow 2>&1 | grep \"Completed\"", + new_command.follow_logs(host: "app-1", lines: 123, grep: "Completed") end