Add --tail flag as option on logs.

Closes #265
Signed-off-by: Stéphane Seguin <stephseguin93@gmail.com>
This commit is contained in:
Stéphane Seguin 2016-02-28 22:00:53 +01:00
commit 9b36dc5c54
6 changed files with 33 additions and 9 deletions

View file

@ -19,13 +19,16 @@ class LogPrinter(object):
monochrome=False,
cascade_stop=False,
follow=False,
timestamps=False):
timestamps=False,
tail="all"):
self.containers = containers
self.output = utils.get_output_stream(output)
self.monochrome = monochrome
self.cascade_stop = cascade_stop
self.follow = follow
self.timestamps = timestamps
self.tail = tail
def run(self):
if not self.containers:
@ -49,7 +52,7 @@ class LogPrinter(object):
for color_func, container in zip(color_funcs, self.containers):
generator_func = get_log_generator(container)
prefix = color_func(build_log_prefix(container, prefix_width))
yield generator_func(container, prefix, color_func, self.follow, self.timestamps)
yield generator_func(container, prefix, color_func, self.follow, self.timestamps, self.tail)
def build_log_prefix(container, prefix_width):
@ -72,7 +75,7 @@ def get_log_generator(container):
return build_no_log_generator
def build_no_log_generator(container, prefix, color_func, follow, timestamps):
def build_no_log_generator(container, prefix, color_func, follow, timestamps, tail):
"""Return a generator that prints a warning about logs and waits for
container to exit.
"""
@ -83,12 +86,12 @@ def build_no_log_generator(container, prefix, color_func, follow, timestamps):
yield color_func(wait_on_exit(container))
def build_log_generator(container, prefix, color_func, follow, timestamps):
def build_log_generator(container, prefix, color_func, follow, timestamps, tail):
# if the container doesn't have a log_stream we need to attach to container
# before log printer starts running
if container.log_stream is None:
stream = container.logs(stdout=True, stderr=True, stream=True,
follow=follow, timestamps=timestamps)
follow=follow, timestamps=timestamps, tail=tail)
line_generator = split_buffer(stream)
else:
line_generator = split_buffer(container.log_stream)

View file

@ -329,16 +329,24 @@ class TopLevelCommand(DocoptCommand):
Options:
--no-color Produce monochrome output.
-f, --follow Follow log output
-t, --timestamps Show timestamps
-f, --follow Follow log output.
-t, --timestamps Show timestamps.
--tail="all" Number of lines to show from the end of the logs
for each container.
"""
containers = project.containers(service_names=options['SERVICE'], stopped=True)
monochrome = options['--no-color']
follow = options['--follow']
timestamps = options['--timestamps']
tail = options['--tail']
if tail is not None:
if tail.isdigit():
tail = int(tail)
elif tail != 'all':
raise UserError("tail flag must be all or a number")
print("Attaching to", list_containers(containers))
LogPrinter(containers, monochrome=monochrome, follow=follow, timestamps=timestamps).run()
LogPrinter(containers, monochrome=monochrome, follow=follow, timestamps=timestamps, tail=tail).run()
def pause(self, project, options):
"""