compose icon indicating copy to clipboard operation
compose copied to clipboard

Allow colors for container log messages to be specified in docker-compose.yml

Open jwiegley opened this issue 9 years ago • 23 comments

When debugging a particular group of containers run with docker-compose, one becomes very familiar with looking for messages reported from a certain color. However, every once in a while these colors unpredictably change. It would be nice if the RGB code could be specified for each container in the docker-compose.yml file, to ensure the colors are always what I expect.

Including @nweston, whose brought this idea to my attention.

jwiegley avatar Dec 20 '16 22:12 jwiegley

I second this

mutchco avatar Feb 16 '17 15:02 mutchco

+1

In addition, 'red' should not be removed from the colours to be chosen, as it denotes something has gone wrong.

theskipster avatar Jun 23 '17 14:06 theskipster

+1

This would be incredibly helpful in particular during development phases where you up and down dockers a lot. Colors keep changing each time as of now.

mrglaser avatar Mar 08 '18 22:03 mrglaser

+1 It happens quite frequently that some of the most talkative containers get the same color (or pretty much the same), which makes differentiating their log outputs a bit tedious. And up/downing the whole service bundle just to switch the colors seems a bit of an overkill...

Greek64 avatar May 11 '18 17:05 Greek64

+1.

This would be very useful. Right now, it seems that the colors change randomly after each restart.

If colors are not made configurable, at least they should stay constant over time.

michaelfranzl avatar Sep 13 '18 08:09 michaelfranzl

+1

Where on the source colors are specified. Like to know how complicated pull request will be.

Rafalsky avatar Sep 26 '18 12:09 Rafalsky

+1

Would be great to config this in the docker-compose file :-)

Johanneke avatar Jan 14 '19 16:01 Johanneke

This would be a great feature.

@Rafalsky I think the relevant code starts here: https://github.com/docker/compose/blob/master/compose/cli/log_printer.py#L43-L44

def build_log_presenters(service_names, monochrome):
    ...
    for color_func in cycle([no_color] if monochrome else colors.rainbow()):
        yield LogPresenter(prefix_width, color_func)
    ...

colors.rainbow() seems to be defined here: https://github.com/docker/compose/blob/master/compose/cli/colors.py#L43-L49

def rainbow():
    cs = ['cyan', 'yellow', 'green', 'magenta', 'red', 'blue',
          'intense_cyan', 'intense_yellow', 'intense_green',
          'intense_magenta', 'intense_red', 'intense_blue']

    for c in cs:
        yield globals()[c]

I've looked at it once before, but can't exactly figure out the best way to implement this feature, but hope that helps someone!

michaelchadwick avatar Jan 14 '19 17:01 michaelchadwick

Thanks for the report!

It's unlikely that we'd add log output colours to the Compose format but it would definitely help if the colours were deterministic between runs.

chris-crone avatar Jan 16 '19 21:01 chris-crone

It's very annoying seeing red output to the console when nothing is going wrong -_-

ghost avatar Feb 04 '19 00:02 ghost

+1

I spawn test containers with a color themed naming convention, rather then a numeric one. i.e. "container-blue". It is initially confusing when log-color does not match the naming convention for new project-members.

markus-lundin-86 avatar Apr 12 '19 08:04 markus-lundin-86

I'm piping my output through colout as a workaround. Not pretty but works :) https://github.com/neuland/micro-frontends/tree/master/2-composition-universal

docker-compose up --build | colout "team_green_1" green | colout "team_blue_1" cyan | colout "team_red_1" red | colout "nginx_1" white"

naltatis avatar Apr 12 '19 09:04 naltatis

+1 It could be something like

version: '3.7'
services:
  container1:
    **labels: 
      color: red**
    container_name: container_red
  container2:
    **labels: 
      color: blue**
    container_name: container_blue

xinin avatar Apr 23 '19 10:04 xinin

@xinin I don't think the parameter setting is the problem here.

After having a look through the code, one can see that the function that sets the color to the log stream output is doing so in a carousel manner from an array of predefined colors (as many of us would have expected), but is totally independent of the rest of the code (in the sense that it has no information about the container the log stream comes from).

To make the log color container specific would mean to expand several functions with extra arguments and drag this information down to this level.

If I get some free time (which will be a bit tough before this summer) I will take a closer look at it.


For anyone interested, here's a jumpstart:

Function log_printer_from_project (/compose/cli/main.py) creates a new LogPrinter class (/compose/cli/log_printer.py) instance. It passes to the constructor an instance of a LogPresenter class (/compose/cli/log_printer.py) returned from build_log_presenters (/compose/cli/log_printer.py), which is the actual function that sets the colors (it returns LogPresenters with set colors). The LogPrinter than uses this array of LogPresenters to format the container output streams.

The build_log_presenters function knows the actual name of the container (since it does format it accordingly), but that's it. We now only have to relay to it also the color information per container.

Greek64 avatar Apr 23 '19 11:04 Greek64

If at least red could be excluded from container name, and kept for stderr. It's a bit disturbing to see red as container name.

bourdeau avatar Jun 03 '19 18:06 bourdeau

Yes. PLEASE.

Jamtastic808 avatar Jun 14 '19 14:06 Jamtastic808

This would be a great feature.

alp82 avatar Jul 23 '19 08:07 alp82

I have 9 containers starting and one of them has started to show up frequently with a black container name, which is really annoying on my black terminal background. The dark blue isn't much better.

derekrprice avatar Oct 01 '19 17:10 derekrprice

We're in the process of migrating a shed load of applications to containers and being able to define the colour a service is linked to would be super helpful for our team.

sparkacus avatar Oct 31 '19 16:10 sparkacus

@naltatis thanks for the shoutout for colout! It works like a charm.

michaelchadwick avatar Oct 31 '19 17:10 michaelchadwick