log_printer.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. from __future__ import absolute_import
  2. from __future__ import unicode_literals
  3. import sys
  4. from itertools import cycle
  5. from . import colors
  6. from .multiplexer import Multiplexer
  7. from compose import utils
  8. from compose.utils import split_buffer
  9. class LogPrinter(object):
  10. """Print logs from many containers to a single output stream."""
  11. def __init__(self, containers, output=sys.stdout, monochrome=False, cascade_stop=False):
  12. self.containers = containers
  13. self.output = utils.get_output_stream(output)
  14. self.monochrome = monochrome
  15. self.cascade_stop = cascade_stop
  16. def run(self):
  17. if not self.containers:
  18. return
  19. prefix_width = max_name_width(self.containers)
  20. generators = list(self._make_log_generators(self.monochrome, prefix_width))
  21. for line in Multiplexer(generators, cascade_stop=self.cascade_stop).loop():
  22. self.output.write(line)
  23. self.output.flush()
  24. def _make_log_generators(self, monochrome, prefix_width):
  25. def no_color(text):
  26. return text
  27. if monochrome:
  28. color_funcs = cycle([no_color])
  29. else:
  30. color_funcs = cycle(colors.rainbow())
  31. for color_func, container in zip(color_funcs, self.containers):
  32. generator_func = get_log_generator(container)
  33. prefix = color_func(build_log_prefix(container, prefix_width))
  34. yield generator_func(container, prefix, color_func)
  35. def build_log_prefix(container, prefix_width):
  36. return container.name_without_project.ljust(prefix_width) + ' | '
  37. def max_name_width(containers):
  38. """Calculate the maximum width of container names so we can make the log
  39. prefixes line up like so:
  40. db_1 | Listening
  41. web_1 | Listening
  42. """
  43. return max(len(container.name_without_project) for container in containers)
  44. def get_log_generator(container):
  45. if container.has_api_logs:
  46. return build_log_generator
  47. return build_no_log_generator
  48. def build_no_log_generator(container, prefix, color_func):
  49. """Return a generator that prints a warning about logs and waits for
  50. container to exit.
  51. """
  52. yield "{} WARNING: no logs are available with the '{}' log driver\n".format(
  53. prefix,
  54. container.log_driver)
  55. yield color_func(wait_on_exit(container))
  56. def build_log_generator(container, prefix, color_func):
  57. # if the container doesn't have a log_stream we need to attach to container
  58. # before log printer starts running
  59. if container.log_stream is None:
  60. stream = container.attach(stdout=True, stderr=True, stream=True, logs=True)
  61. line_generator = split_buffer(stream)
  62. else:
  63. line_generator = split_buffer(container.log_stream)
  64. for line in line_generator:
  65. yield prefix + line
  66. yield color_func(wait_on_exit(container))
  67. def wait_on_exit(container):
  68. exit_code = container.wait()
  69. return "%s exited with code %s\n" % (container.name, exit_code)