如何从Docker控制流利的日志标签 [英] How to control fluentd log tag from Docker
问题描述
当前设置正在使用fluentd
驱动程序运行Docker容器:
Current setup is running Docker containers with the fluentd
driver:
docker run --log-driver=fluentd my-container
这很容易...将标准输出发送到主机上本地运行的fluentd系统.
That works quite easily...sends stdout to the locally running fluentd system on the host.
我想使用容器上设置的一些标签来控制流畅的tag
.对于此示例,假定一些标签(docker inspect
代码段):
I'd like to control the fluentd tag
using some of the labels which are set on the container. For this example, assume some labels (docker inspect
snippet):
"Config": {
"Labels": {
"com.amazonaws.ecs.container-name": "web",
"com.amazonaws.ecs.task-definition-version": "3"
}
}
如何通过提取一个或多个Labels
来设置fluentd
tag
?
How can I set the fluentd
tag
by extracting out one or more of the Labels
?
此Docker文档列出了当前支持的标签: https://docs .docker.com/engine/admin/logging/log_tags/
The Docker docs on this list out the currently supported tags: https://docs.docker.com/engine/admin/logging/log_tags/
那些文档记录了ExtraAttributes
...但是,我可以弄清楚如何使用它来查找一个或多个标签.阅读Docker代码对我来说还不清楚: https: //github.com/docker/docker/blob/master/daemon/logger/context.go#L29
Those docs make note of ExtraAttributes
...however I can figure out how to use that to look up one or more labels. Reading through the Docker code it's not clear to me: https://github.com/docker/docker/blob/master/daemon/logger/context.go#L29
这与docker inspect
无关紧要:
docker inspect --format '{{index .ContainerLabels "com.amazonaws.ecs.container-name"}}'
我想提取相同的信息并将其放入log-opt tag=?
I'd like to grep out the same information and get it into log-opt tag=?
推荐答案
ContainerLabels
地图是 fluentd驱动程序支持ParseLogTag ,因此您可以使用go模板格式.
The ContainerLabels
map is one of the items available in dockers logging Context and the fluentd driver supports ParseLogTag so you can use go template formatting.
在直接泊坞窗中,这看起来像:
In straight docker this would look like:
docker run \
--label alabel=1value \
--log-driver=fluentd \
--log-opt tag="{{ .ContainerLabels.alabel }}" \
busybox \
echo "$(date) test log"
因此,如果您可以按以下方式将--log-opt
注入到ECS启动中:
So if you can inject a --log-opt
into your ECS startup as follows:
--log-opt tag='{{ index .ContainerLabels "com.amazonaws.ecs.container-name" }}'
您应该获得标签.
index
具有非字母数字字符的键
index
is required here to access map keys with non alpha numeric characters
这篇关于如何从Docker控制流利的日志标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!