log_printer.py 2.8 KB

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