Add -t, --timestamps flag as option on logs.

Signed-off-by: Stéphane Seguin <stephseguin93@gmail.com>
This commit is contained in:
Stéphane Seguin 2016-02-28 22:00:12 +01:00
parent 82632098a3
commit d9b4286f91
4 changed files with 28 additions and 11 deletions

View File

@ -13,13 +13,19 @@ from compose.utils import split_buffer
class LogPrinter(object):
"""Print logs from many containers to a single output stream."""
def __init__(self, containers, output=sys.stdout, monochrome=False,
cascade_stop=False, follow=False):
def __init__(self,
containers,
output=sys.stdout,
monochrome=False,
cascade_stop=False,
follow=False,
timestamps=False):
self.containers = containers
self.output = utils.get_output_stream(output)
self.monochrome = monochrome
self.cascade_stop = cascade_stop
self.follow = follow
self.timestamps = timestamps
def run(self):
if not self.containers:
@ -43,7 +49,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)
yield generator_func(container, prefix, color_func, self.follow, self.timestamps)
def build_log_prefix(container, prefix_width):
@ -66,7 +72,7 @@ def get_log_generator(container):
return build_no_log_generator
def build_no_log_generator(container, prefix, color_func, follow):
def build_no_log_generator(container, prefix, color_func, follow, timestamps):
"""Return a generator that prints a warning about logs and waits for
container to exit.
"""
@ -77,12 +83,12 @@ def build_no_log_generator(container, prefix, color_func, follow):
yield color_func(wait_on_exit(container))
def build_log_generator(container, prefix, color_func, follow):
def build_log_generator(container, prefix, color_func, follow, timestamps):
# 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)
follow=follow, timestamps=timestamps)
line_generator = split_buffer(stream)
else:
line_generator = split_buffer(container.log_stream)

View File

@ -328,15 +328,17 @@ class TopLevelCommand(DocoptCommand):
Usage: logs [options] [SERVICE...]
Options:
--no-color Produce monochrome output.
-f, --follow Follow log output
--no-color Produce monochrome output.
-f, --follow Follow log output
-t, --timestamps Show timestamps
"""
containers = project.containers(service_names=options['SERVICE'], stopped=True)
monochrome = options['--no-color']
follow = options['--follow']
timestamps = options['--timestamps']
print("Attaching to", list_containers(containers))
LogPrinter(containers, monochrome=monochrome, follow=follow).run()
LogPrinter(containers, monochrome=monochrome, follow=follow, timestamps=timestamps).run()
def pause(self, project, options):
"""

View File

@ -15,8 +15,9 @@ parent = "smn_compose_cli"
Usage: logs [options] [SERVICE...]
Options:
--no-color Produce monochrome output.
-f, --follow Follow log output
--no-color Produce monochrome output.
-f, --follow Follow log output
-t, --timestamps Show timestamps
```
Displays log output from services.

View File

@ -1163,6 +1163,14 @@ class CLITestCase(DockerClientTestCase):
assert result.stdout.count('\n') >= 1
assert 'exited with code 0' not in result.stdout
def test_logs_timestamps(self):
self.base_dir = 'tests/fixtures/echo-services'
self.dispatch(['up', '-d'], None)
result = self.dispatch(['logs', '-f', '-t'], None)
self.assertRegexpMatches(result.stdout, '(\d{4})-(\d{2})-(\d{2})T(\d{2})\:(\d{2})\:(\d{2})')
def test_kill(self):
self.dispatch(['up', '-d'], None)
service = self.project.get_service('simple')