Skip to content

Commit 42f62d3

Browse files
Display a specified number of log lines on console based on tail option (#8233)
1 parent f9ee215 commit 42f62d3

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

localstack/cli/localstack.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,15 @@ def cmd_stop():
193193
help="Block the terminal and follow the log output",
194194
default=False,
195195
)
196+
@click.option(
197+
"-n",
198+
"--tail",
199+
type=int,
200+
help="Print only the last N lines of the log output",
201+
default=None,
202+
)
196203
@publish_invocation
197-
def cmd_logs(follow: bool):
204+
def cmd_logs(follow: bool, tail: int):
198205
from localstack.utils.bootstrap import LocalstackContainer
199206
from localstack.utils.docker_utils import DOCKER_CLIENT
200207

@@ -211,10 +218,18 @@ def cmd_logs(follow: bool):
211218
sys.exit(1)
212219

213220
if follow:
221+
num_lines = 0
214222
for line in DOCKER_CLIENT.stream_container_logs(container_name):
215223
print(line.decode("utf-8").rstrip("\r\n"))
224+
num_lines += 1
225+
if tail is not None and num_lines >= tail:
226+
break
227+
216228
else:
217-
print(DOCKER_CLIENT.get_container_logs(container_name))
229+
logs = DOCKER_CLIENT.get_container_logs(container_name)
230+
if tail is not None:
231+
logs = "\n".join(logs.split("\n")[-tail:])
232+
print(logs)
218233

219234

220235
@localstack.command(name="wait", help="Wait on the LocalStack container to start")

tests/bootstrap/test_cli.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ def test_logs(self, runner, container_client):
8181
runner.invoke(cli, ["start", "-d"])
8282
runner.invoke(cli, ["wait", "-t", "60"])
8383

84-
result = runner.invoke(cli, ["logs"])
84+
result = runner.invoke(cli, ["logs", "--tail", "3"])
85+
assert result.output.count("\n") == 3
8586
assert constants.READY_MARKER_OUTPUT in result.output
8687

8788
def test_status_services(self, runner):

0 commit comments

Comments
 (0)