El Ruso
El Ruso

Reputation: 15881

How can I remove prefix with service name from logs?

docker-compose inserts prefixes like service_1 | in the beginning of every line of output. I use this container for testing and this kind of improvement (very useful in other cases) mess my debugging logs and I want to remove it for this service. Documentation have no information about this question. Any ideas?
my docker-compose.yml:

version: '3'
services:
  rds:
    image: postgres:9.4
    ports:
      - ${POSTGRES_PORT}:5432
    environment:
      POSTGRES_USER: ${POSTGRES_USER}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
  dynamo:
    image: iarruss/dynamo-local-admin:latest
    ports:
      - ${DYNAMODB_PORT}:8000
  python:
    container_name: python
    image: iarruss/docker-python2:latest
    depends_on:
      - rds
      - dynamo
    environment:
      POSTGRES_HOST: rds
      POSTGRES_USER: ${POSTGRES_USER}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
      DYNAMODB_HOST: dynamo

Edit: Clarify expected result

Current output:

python             |
python             | collected 511 items
python             |
python             | tests/test_access.py
python             |

Expected output:

collected 511 items

test_access.py                                                     

Upvotes: 17

Views: 4710

Answers (2)

davnicwil
davnicwil

Reputation: 30997

docker compose logs has a --no-log-prefix flag that removes the prefix.

For example

# start all services in background
docker compose up -d

# show logs for all services, without prefix (-f means follow the logs)
docker compose logs -f --no-log-prefix

# or, for a single service called foo
docker compose up foo -d
docker compose logs foo -f --no-log-prefix

See the documentation here

Upvotes: 12

Kadu Diógenes
Kadu Diógenes

Reputation: 507

In an unix shell you can use pipes (|):

docker-compose up python | sed -u 's/^[^|]*[^ ]* //'

The [^|]* means match everything that is not a |, so in your example it will match python and all spaces until the first |.

The [^ ]* is similar to the above, except that it matches everything that is not a space. This is necessary because there isn't only a space after the |, docker-compose adds some control characters to colorize output.

After this there is a space to actually remove the space after |.

The first slash of // determines the end of the first regular expression. Inside theses slash you can put some content to replace what was matched, in this case, it's empty.

The -u option is necessary when you use some other tools to manage your process, like foreman.

Upvotes: 6

Related Questions