将日志元数据写入rsyslog
我有一个设置泊坞窗容器使用日志logging驱动程序写他们的日志。 目前,来自日志的日志行被转发到主机上运行的dockerd
,但syslog行上的应用程序名称显示为dockerd
。
作为一种解决方法,我希望将日记元数据中的CONTAINER_NAME
字段写入syslog中出现的行中,以便在主机的syslog被传送到syslog聚合服务器之后,我可以识别哪个容器写了什么行。
有什么build议么?
我能够通过在从journaldparsing结构化日志之后定义模板来实现此目的。 为了搞清楚哪些属性可用,我运行了journalctl -o verbose -n 10
rsyslog有多种不同的方式来做同样的configuration,这里是我从CentOS 7机器上的configuration:
module(load="imjournal" StateFile="imjournal.state") # Load imjournal module module(load="mmjsonparse") # Load mmjsonparse module for structured logs action(type="mmjsonparse") # Attempt to parse JSON template(name="ContainerTemplate" type="list") { property(name="timestamp" dateFormat="rfc3339") constant(value=" ") property(name="$!CONTAINER_NAME") constant(value=" ") property(name="$!CONTAINER_ID") constant(value=" ") property(name="$!MESSAGE") constant(value="\n") # Separate logs with a newline } if ($!CONTAINER_NAME != "") then { action(type="omfile" file="/var/log/messages" template="ContainerFormat") } else { *.info;mail.none;news.none;authpriv.none;cron.none action(type="omfile" file="/var/log/messages") }
参考文件:
- https://docs.docker.com/engine/admin/logging/journald/
- http://www.rsyslog.com/doc/v8-stable/configuration/modules/imjournal.html
- http://www.rsyslog.com/doc/master/configuration/modules/mmjsonparse.html
- http://www.rsyslog.com/using-rsyslog-and-elasticsearch-to-handle-different-types-of-json-logs/
- http://www.rsyslog.com/doc/v8-stable/configuration/templates.html
- http://www.rsyslog.com/doc/v8-stable/rainerscript/control_structures.html#if-else-if-else
我觉得最接近你可以得到图像的名称。 您可以添加日志标记以在日志中显示图像名称。 此function已添加到v1.11.0。 例如:
docker run --log-driver=journald --log-opt tag="{{.ImageName}}
看看日志标签文档了。 希望这可以帮助。
我设法通过把这个在rsyslog.conf中得到这个工作
if ( $!CONTAINER_TAG == "mycontainer" ) then { action(type="omfile" file="/var/log/mycontainer.log") stop }
我可以testing它与这个docker run --log-driver=journald --log-opt tag="mycontainer" centos:latest echo Pierre7
我在/ var / log / mycontainer和日志中find了我的日志
journalctl --unit docker CONTAINER_TAG=mycontainer
这是很难find的信息,唯一的地方,它似乎logging在红帽文档: https : //access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/7/html /System_Administrators_Guide/s1-structured_logging_with_rsyslog.html
我的使用案例是,我希望期刊能够提供所有的好处,但支持人员要求拥有普通文件。 我们也将使用splunkforwarder来使用这些文件。