Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

changes default logging behavior to not tail logs #807

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions compose/cli/log_printer.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,17 @@


class LogPrinter(object):
def __init__(self, containers, attach_params=None, output=sys.stdout, monochrome=False):
def __init__(self, containers, attach_params=None, output=sys.stdout, monochrome=False, tail=False):
self.containers = containers
self.attach_params = attach_params or {}
self.prefix_width = self._calculate_prefix_width(containers)
self.generators = self._make_log_generators(monochrome)
self.tail = tail
self.output = output

def run(self):
mux = Multiplexer(self.generators)
for line in mux.loop():
for line in mux.loop(self.tail):
self.output.write(line)

def _calculate_prefix_width(self, containers):
Expand Down
4 changes: 3 additions & 1 deletion compose/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,12 +151,14 @@ def logs(self, project, options):

Options:
--no-color Produce monochrome output.
--tail Prints logs to stdout and returns.
"""
containers = project.containers(service_names=options['SERVICE'], stopped=True)

monochrome = options['--no-color']
tail = options['--tail']
print("Attaching to", list_containers(containers))
LogPrinter(containers, attach_params={'logs': True}, monochrome=monochrome).run()
LogPrinter(containers, attach_params={'logs': True}, monochrome=monochrome, tail=tail).run()

def port(self, project, options):
"""
Expand Down
5 changes: 3 additions & 2 deletions compose/cli/multiplexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def __init__(self, generators):
self.generators = generators
self.queue = Queue()

def loop(self):
def loop(self, tail=True):
self._init_readers()

while True:
Expand All @@ -28,7 +28,8 @@ def loop(self):
else:
yield item
except Empty:
pass
if not tail:
return

def _init_readers(self):
for generator in self.generators:
Expand Down
24 changes: 22 additions & 2 deletions tests/unit/log_printer_test.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import unicode_literals
from __future__ import absolute_import
import os
import time

from compose.cli.log_printer import LogPrinter
from .. import unittest
Expand Down Expand Up @@ -40,11 +41,30 @@ def reader(*args, **kwargs):

self.assertIn(glyph, output)

def test_notail(self):
def reader(*args, **kwargs):
for count in xrange(3):
yield "keep going\n"
time.sleep(0.2)

container = MockContainer(reader)
output = run_log_printer([container], monochrome=False, tail=False)
self.assertEqual(output.count('keep going'), 1)

def run_log_printer(containers, monochrome=False):
def test_tail(self):
def reader(*args, **kwargs):
for count in xrange(3):
yield "keep going\n"
time.sleep(0.2)

container = MockContainer(reader)
output = run_log_printer([container], monochrome=False, tail=True)
self.assertEqual(output.count('keep going'), 3)

def run_log_printer(containers, monochrome=False, tail=True):
r, w = os.pipe()
reader, writer = os.fdopen(r, 'r'), os.fdopen(w, 'w')
printer = LogPrinter(containers, output=writer, monochrome=monochrome)
printer = LogPrinter(containers, output=writer, monochrome=monochrome, tail=tail)
printer.run()
writer.close()
return reader.read()
Expand Down