Skip to content

Commit fff5e51

Browse files
committed
Make log messages line up with each other
1 parent a724aa5 commit fff5e51

1 file changed

Lines changed: 23 additions & 1 deletion

File tree

fig/cli/log_printer.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,27 @@ class LogPrinter(object):
1313
def __init__(self, containers, attach_params=None):
1414
self.containers = containers
1515
self.attach_params = attach_params or {}
16+
self.prefix_width = self._calculate_prefix_width(containers)
1617
self.generators = self._make_log_generators()
1718

1819
def run(self):
1920
mux = Multiplexer(self.generators)
2021
for line in mux.loop():
2122
sys.stdout.write(line.encode(sys.__stdout__.encoding or 'utf-8'))
2223

24+
def _calculate_prefix_width(self, containers):
25+
"""
26+
Calculate the maximum width of container names so we can make the log
27+
prefixes line up like so:
28+
29+
db_1 | Listening
30+
web_1 | Listening
31+
"""
32+
prefix_width = 0
33+
for container in containers:
34+
prefix_width = max(prefix_width, len(container.name_without_project))
35+
return prefix_width
36+
2337
def _make_log_generators(self):
2438
color_fns = cycle(colors.rainbow())
2539
generators = []
@@ -31,7 +45,7 @@ def _make_log_generators(self):
3145
return generators
3246

3347
def _make_log_generator(self, container, color_fn):
34-
prefix = color_fn(container.name_without_project + " | ")
48+
prefix = color_fn(self._generate_prefix(container))
3549
# Attach to container before log printer starts running
3650
line_generator = split_buffer(self._attach(container), '\n')
3751

@@ -42,6 +56,14 @@ def _make_log_generator(self, container, color_fn):
4256
yield color_fn("%s exited with code %s\n" % (container.name, exit_code))
4357
yield STOP
4458

59+
def _generate_prefix(self, container):
60+
"""
61+
Generate the prefix for a log line without colour
62+
"""
63+
name = container.name_without_project
64+
padding = ' ' * (self.prefix_width - len(name))
65+
return ''.join([name, padding, ' | '])
66+
4567
def _attach(self, container):
4668
params = {
4769
'stdout': True,

0 commit comments

Comments
 (0)