mirror of
https://github.com/docker/compose.git
synced 2025-07-22 13:14:29 +02:00
Make log messages line up with each other
This commit is contained in:
parent
a724aa5717
commit
fff5e51426
@ -13,6 +13,7 @@ class LogPrinter(object):
|
|||||||
def __init__(self, containers, attach_params=None):
|
def __init__(self, containers, attach_params=None):
|
||||||
self.containers = containers
|
self.containers = containers
|
||||||
self.attach_params = attach_params or {}
|
self.attach_params = attach_params or {}
|
||||||
|
self.prefix_width = self._calculate_prefix_width(containers)
|
||||||
self.generators = self._make_log_generators()
|
self.generators = self._make_log_generators()
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
@ -20,6 +21,19 @@ class LogPrinter(object):
|
|||||||
for line in mux.loop():
|
for line in mux.loop():
|
||||||
sys.stdout.write(line.encode(sys.__stdout__.encoding or 'utf-8'))
|
sys.stdout.write(line.encode(sys.__stdout__.encoding or 'utf-8'))
|
||||||
|
|
||||||
|
def _calculate_prefix_width(self, containers):
|
||||||
|
"""
|
||||||
|
Calculate the maximum width of container names so we can make the log
|
||||||
|
prefixes line up like so:
|
||||||
|
|
||||||
|
db_1 | Listening
|
||||||
|
web_1 | Listening
|
||||||
|
"""
|
||||||
|
prefix_width = 0
|
||||||
|
for container in containers:
|
||||||
|
prefix_width = max(prefix_width, len(container.name_without_project))
|
||||||
|
return prefix_width
|
||||||
|
|
||||||
def _make_log_generators(self):
|
def _make_log_generators(self):
|
||||||
color_fns = cycle(colors.rainbow())
|
color_fns = cycle(colors.rainbow())
|
||||||
generators = []
|
generators = []
|
||||||
@ -31,7 +45,7 @@ class LogPrinter(object):
|
|||||||
return generators
|
return generators
|
||||||
|
|
||||||
def _make_log_generator(self, container, color_fn):
|
def _make_log_generator(self, container, color_fn):
|
||||||
prefix = color_fn(container.name_without_project + " | ")
|
prefix = color_fn(self._generate_prefix(container))
|
||||||
# Attach to container before log printer starts running
|
# Attach to container before log printer starts running
|
||||||
line_generator = split_buffer(self._attach(container), '\n')
|
line_generator = split_buffer(self._attach(container), '\n')
|
||||||
|
|
||||||
@ -42,6 +56,14 @@ class LogPrinter(object):
|
|||||||
yield color_fn("%s exited with code %s\n" % (container.name, exit_code))
|
yield color_fn("%s exited with code %s\n" % (container.name, exit_code))
|
||||||
yield STOP
|
yield STOP
|
||||||
|
|
||||||
|
def _generate_prefix(self, container):
|
||||||
|
"""
|
||||||
|
Generate the prefix for a log line without colour
|
||||||
|
"""
|
||||||
|
name = container.name_without_project
|
||||||
|
padding = ' ' * (self.prefix_width - len(name))
|
||||||
|
return ''.join([name, padding, ' | '])
|
||||||
|
|
||||||
def _attach(self, container):
|
def _attach(self, container):
|
||||||
params = {
|
params = {
|
||||||
'stdout': True,
|
'stdout': True,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user