mirror of
https://github.com/docker/compose.git
synced 2025-07-23 21:54:40 +02:00
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,
|
output=sys.stdout,
|
||||||
monochrome=False,
|
monochrome=False,
|
||||||
cascade_stop=False,
|
cascade_stop=False,
|
||||||
follow=False,
|
log_args=None):
|
||||||
timestamps=False,
|
log_args = log_args or {}
|
||||||
tail="all"):
|
|
||||||
|
|
||||||
self.containers = containers
|
self.containers = containers
|
||||||
self.output = utils.get_output_stream(output)
|
self.output = utils.get_output_stream(output)
|
||||||
self.monochrome = monochrome
|
self.monochrome = monochrome
|
||||||
self.cascade_stop = cascade_stop
|
self.cascade_stop = cascade_stop
|
||||||
self.follow = follow
|
self.log_args = log_args
|
||||||
self.timestamps = timestamps
|
|
||||||
self.tail = tail
|
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
if not self.containers:
|
if not self.containers:
|
||||||
@ -52,7 +48,7 @@ class LogPrinter(object):
|
|||||||
for color_func, container in zip(color_funcs, self.containers):
|
for color_func, container in zip(color_funcs, self.containers):
|
||||||
generator_func = get_log_generator(container)
|
generator_func = get_log_generator(container)
|
||||||
prefix = color_func(build_log_prefix(container, prefix_width))
|
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):
|
def build_log_prefix(container, prefix_width):
|
||||||
@ -75,30 +71,29 @@ def get_log_generator(container):
|
|||||||
return build_no_log_generator
|
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
|
"""Return a generator that prints a warning about logs and waits for
|
||||||
container to exit.
|
container to exit.
|
||||||
"""
|
"""
|
||||||
yield "{} WARNING: no logs are available with the '{}' log driver\n".format(
|
yield "{} WARNING: no logs are available with the '{}' log driver\n".format(
|
||||||
prefix,
|
prefix,
|
||||||
container.log_driver)
|
container.log_driver)
|
||||||
if follow:
|
if log_args.get('follow'):
|
||||||
yield color_func(wait_on_exit(container))
|
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
|
# if the container doesn't have a log_stream we need to attach to container
|
||||||
# before log printer starts running
|
# before log printer starts running
|
||||||
if container.log_stream is None:
|
if container.log_stream is None:
|
||||||
stream = container.logs(stdout=True, stderr=True, stream=True,
|
stream = container.logs(stdout=True, stderr=True, stream=True, **log_args)
|
||||||
follow=follow, timestamps=timestamps, tail=tail)
|
|
||||||
line_generator = split_buffer(stream)
|
line_generator = split_buffer(stream)
|
||||||
else:
|
else:
|
||||||
line_generator = split_buffer(container.log_stream)
|
line_generator = split_buffer(container.log_stream)
|
||||||
|
|
||||||
for line in line_generator:
|
for line in line_generator:
|
||||||
yield prefix + line
|
yield prefix + line
|
||||||
if follow:
|
if log_args.get('follow'):
|
||||||
yield color_func(wait_on_exit(container))
|
yield color_func(wait_on_exit(container))
|
||||||
|
|
||||||
|
|
||||||
|
@ -337,16 +337,19 @@ class TopLevelCommand(DocoptCommand):
|
|||||||
containers = project.containers(service_names=options['SERVICE'], stopped=True)
|
containers = project.containers(service_names=options['SERVICE'], stopped=True)
|
||||||
|
|
||||||
monochrome = options['--no-color']
|
monochrome = options['--no-color']
|
||||||
follow = options['--follow']
|
|
||||||
timestamps = options['--timestamps']
|
|
||||||
tail = options['--tail']
|
tail = options['--tail']
|
||||||
if tail is not None:
|
if tail is not None:
|
||||||
if tail.isdigit():
|
if tail.isdigit():
|
||||||
tail = int(tail)
|
tail = int(tail)
|
||||||
elif tail != 'all':
|
elif tail != 'all':
|
||||||
raise UserError("tail flag must be all or a number")
|
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))
|
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):
|
def pause(self, project, options):
|
||||||
"""
|
"""
|
||||||
@ -672,8 +675,8 @@ class TopLevelCommand(DocoptCommand):
|
|||||||
|
|
||||||
if detached:
|
if detached:
|
||||||
return
|
return
|
||||||
log_printer = build_log_printer(to_attach, service_names, monochrome, cascade_stop,
|
log_args = {'follow': True}
|
||||||
follow=True)
|
log_printer = build_log_printer(to_attach, service_names, monochrome, cascade_stop, log_args)
|
||||||
print("Attaching to", list_containers(log_printer.containers))
|
print("Attaching to", list_containers(log_printer.containers))
|
||||||
log_printer.run()
|
log_printer.run()
|
||||||
|
|
||||||
@ -771,13 +774,13 @@ def run_one_off_container(container_options, project, service, options):
|
|||||||
sys.exit(exit_code)
|
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:
|
if service_names:
|
||||||
containers = [
|
containers = [
|
||||||
container
|
container
|
||||||
for container in containers if container.service in service_names
|
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
|
@contextlib.contextmanager
|
||||||
|
@ -39,7 +39,7 @@ def mock_container():
|
|||||||
class TestLogPrinter(object):
|
class TestLogPrinter(object):
|
||||||
|
|
||||||
def test_single_container(self, output_stream, mock_container):
|
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()
|
output = output_stream.getvalue()
|
||||||
assert 'hello' in output
|
assert 'hello' in output
|
||||||
@ -48,7 +48,7 @@ class TestLogPrinter(object):
|
|||||||
assert output_stream.flush.call_count == 3
|
assert output_stream.flush.call_count == 3
|
||||||
|
|
||||||
def test_single_container_without_stream(self, output_stream, mock_container):
|
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()
|
output = output_stream.getvalue()
|
||||||
assert 'hello' in output
|
assert 'hello' in output
|
||||||
|
@ -33,7 +33,7 @@ class CLIMainTestCase(unittest.TestCase):
|
|||||||
mock_container('another', 1),
|
mock_container('another', 1),
|
||||||
]
|
]
|
||||||
service_names = ['web', 'db']
|
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])
|
self.assertEqual(log_printer.containers, containers[:3])
|
||||||
|
|
||||||
def test_build_log_printer_all_services(self):
|
def test_build_log_printer_all_services(self):
|
||||||
@ -43,7 +43,7 @@ class CLIMainTestCase(unittest.TestCase):
|
|||||||
mock_container('other', 1),
|
mock_container('other', 1),
|
||||||
]
|
]
|
||||||
service_names = []
|
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)
|
self.assertEqual(log_printer.containers, containers)
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user