Docker Compose Up-在日志输出中删除服务名称

时间:2019-10-19 11:42:32

标签: docker logging docker-compose

是否可以在docker-compose up的日志输出中删除服务名称?

我的日志当前看起来像这样:

serviceA        | log1
serviceB        | log2
serviceB        | log3
serviceA        | log4

但是我希望他们看起来像这样:

log1
log2
log3
log4

documentation指出,您可以为以下服务配置日志记录:

version: "3.7"
services:
  some-service:
    image: some-service
    logging:
      driver: "json-file"
      options:
        max-size: "200k"
        max-file: "10"

但是在运行docker-compose up时,我找不到从日志中删除服务名称的任何选项。

更新

根据EchoMike444的建议,我尝试了以下命令:

docker-compose up --no-color 2>&1 | sed 's/^[^ ]*  | //'

具有以下docker compose文件:

version: "3"
services:
  consumer:
    image: debian:stretch-slim
    command:
      [
        "bash",
        "-c",
        "while ( true ) ; do echo $$(date) $$( hostname ) ; sleep $$(( $$RANDOM %4 )) ; done ",
      ]
  ideas:
    image: debian:stretch-slim
    restart: always
    command:
      [
        "bash",
        "-c",
        "while ( true ) ; do echo $$(date) $$( hostname ) ; sleep $$(( $$RANDOM %4 )) ; done ",
      ]

输出看起来像这样

ideas_1     | Sun Oct 20 05:56:23 UTC 2019 99295bdcbf2c
ideas_1     | Sun Oct 20 05:56:25 UTC 2019 99295bdcbf2c
Sun Oct 20 05:56:25 UTC 2019 dd45c5900159
ideas_1     | Sun Oct 20 05:56:27 UTC 2019 99295bdcbf2c
ideas_1     | Sun Oct 20 05:56:27 UTC 2019 99295bdcbf2c
Sun Oct 20 05:56:27 UTC 2019 dd45c5900159
Sun Oct 20 05:56:29 UTC 2019 dd45c5900159
ideas_1     | Sun Oct 20 05:56:30 UTC 2019 99295bdcbf2c

因此它适用于服务consumer,但不适用于ideas

2 个答案:

答案 0 :(得分:1)

当前您无法删除前缀,我在源代码中没有看到任何选项。

docker-compose python 编写,可以随时进行修补。

https://github.com/docker/compose/blob/master/compose/cli/log_printer.py

一种解决方法:

 docker-compose up --no-color 2>&1 | sed 's/^[^ ]*  *| //' 

 docker-compose up -d
 docker-compose logs --no-color -f 2>&1 | sed 's/^[^ ]*  *| //'

我使用很小的docker-compose.yaml

测试了MacOS和Linux
version: '3.7'
services:
  serviceA:
    image: debian:stretch-slim
    command: ["bash","-c","while ( true ) ; do echo $$(date) $$( hostname ) ; sleep $$(( $$RANDOM %4 )) ; done "]
  serviceB:
    image: debian:stretch-slim
    restart: always
    command: ["bash","-c","while ( true ) ; do echo $$(date) $$( hostname ) ; sleep $$(( $$RANDOM %4 )) ; done "]

答案 1 :(得分:0)

看来你现在可以了,这个 PR 已经合并了:

https://github.com/docker/compose/pull/7435

例如:docker-compose up --no-log-prefix