mirror of https://github.com/docker/compose.git
Logs args of LogPrinter as a dictionary
Signed-off-by: Stéphane Seguin <stephseguin93@gmail.com>
This commit is contained in:
parent
9b36dc5c54
commit
038da4eea3
|
@ -18,17 +18,13 @@ class LogPrinter(object):
|
|||
output=sys.stdout,
|
||||
monochrome=False,
|
||||
cascade_stop=False,
|
||||
follow=False,
|
||||
timestamps=False,
|
||||
tail="all"):
|
||||
|
||||
log_args=None):
|
||||
log_args = log_args or {}
|
||||
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
|
||||
self.log_args = log_args
|
||||
|
||||
def run(self):
|
||||
if not self.containers:
|
||||
|
@ -52,7 +48,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, self.tail)
|
||||
yield generator_func(container, prefix, color_func, self.log_args)
|
||||
|
||||
|
||||
def build_log_prefix(container, prefix_width):
|
||||
|
@ -75,30 +71,29 @@ def get_log_generator(container):
|
|||
return build_no_log_generator
|
||||
|
||||
|
||||
def build_no_log_generator(container, prefix, color_func, follow, timestamps, tail):
|
||||
def build_no_log_generator(container, prefix, color_func, log_args):
|
||||
"""Return a generator that prints a warning about logs and waits for
|
||||
container to exit.
|
||||
"""
|
||||
yield "{} WARNING: no logs are available with the '{}' log driver\n".format(
|
||||
prefix,
|
||||
container.log_driver)
|
||||
if follow:
|
||||
if log_args.get('follow'):
|
||||
yield color_func(wait_on_exit(container))
|
||||
|
||||
|
||||
def build_log_generator(container, prefix, color_func, follow, timestamps, tail):
|
||||
def build_log_generator(container, prefix, color_func, log_args):
|
||||
# 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, tail=tail)
|
||||
stream = container.logs(stdout=True, stderr=True, stream=True, **log_args)
|
||||
line_generator = split_buffer(stream)
|
||||
else:
|
||||
line_generator = split_buffer(container.log_stream)
|
||||
|
||||
for line in line_generator:
|
||||
yield prefix + line
|
||||
if follow:
|
||||
if log_args.get('follow'):
|
||||
yield color_func(wait_on_exit(container))
|
||||
|
||||
|
||||
|
|
|
@ -337,16 +337,19 @@ class TopLevelCommand(DocoptCommand):
|
|||
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")
|
||||
log_args = {
|
||||
'follow': options['--follow'],
|
||||
'tail': tail,
|
||||
'timestamps': options['--timestamps']
|
||||
}
|
||||
print("Attaching to", list_containers(containers))
|
||||
LogPrinter(containers, monochrome=monochrome, follow=follow, timestamps=timestamps, tail=tail).run()
|
||||
LogPrinter(containers, monochrome=monochrome, log_args=log_args).run()
|
||||
|
||||
def pause(self, project, options):
|
||||
"""
|
||||
|
@ -672,8 +675,8 @@ class TopLevelCommand(DocoptCommand):
|
|||
|
||||
if detached:
|
||||
return
|
||||
log_printer = build_log_printer(to_attach, service_names, monochrome, cascade_stop,
|
||||
follow=True)
|
||||
log_args = {'follow': True}
|
||||
log_printer = build_log_printer(to_attach, service_names, monochrome, cascade_stop, log_args)
|
||||
print("Attaching to", list_containers(log_printer.containers))
|
||||
log_printer.run()
|
||||
|
||||
|
@ -771,13 +774,13 @@ def run_one_off_container(container_options, project, service, options):
|
|||
sys.exit(exit_code)
|
||||
|
||||
|
||||
def build_log_printer(containers, service_names, monochrome, cascade_stop, follow):
|
||||
def build_log_printer(containers, service_names, monochrome, cascade_stop, log_args):
|
||||
if service_names:
|
||||
containers = [
|
||||
container
|
||||
for container in containers if container.service in service_names
|
||||
]
|
||||
return LogPrinter(containers, monochrome=monochrome, cascade_stop=cascade_stop, follow=follow)
|
||||
return LogPrinter(containers, monochrome=monochrome, cascade_stop=cascade_stop, log_args=log_args)
|
||||
|
||||
|
||||
@contextlib.contextmanager
|
||||
|
|
|
@ -39,7 +39,7 @@ def mock_container():
|
|||
class TestLogPrinter(object):
|
||||
|
||||
def test_single_container(self, output_stream, mock_container):
|
||||
LogPrinter([mock_container], output=output_stream, follow=True).run()
|
||||
LogPrinter([mock_container], output=output_stream, log_args={'follow': True}).run()
|
||||
|
||||
output = output_stream.getvalue()
|
||||
assert 'hello' in output
|
||||
|
@ -48,7 +48,7 @@ class TestLogPrinter(object):
|
|||
assert output_stream.flush.call_count == 3
|
||||
|
||||
def test_single_container_without_stream(self, output_stream, mock_container):
|
||||
LogPrinter([mock_container], output=output_stream, follow=False).run()
|
||||
LogPrinter([mock_container], output=output_stream).run()
|
||||
|
||||
output = output_stream.getvalue()
|
||||
assert 'hello' in output
|
||||
|
|
|
@ -33,7 +33,7 @@ class CLIMainTestCase(unittest.TestCase):
|
|||
mock_container('another', 1),
|
||||
]
|
||||
service_names = ['web', 'db']
|
||||
log_printer = build_log_printer(containers, service_names, True, False, True)
|
||||
log_printer = build_log_printer(containers, service_names, True, False, {'follow': True})
|
||||
self.assertEqual(log_printer.containers, containers[:3])
|
||||
|
||||
def test_build_log_printer_all_services(self):
|
||||
|
@ -43,7 +43,7 @@ class CLIMainTestCase(unittest.TestCase):
|
|||
mock_container('other', 1),
|
||||
]
|
||||
service_names = []
|
||||
log_printer = build_log_printer(containers, service_names, True, False, True)
|
||||
log_printer = build_log_printer(containers, service_names, True, False, {'follow': True})
|
||||
self.assertEqual(log_printer.containers, containers)
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue