将 journald 元数据写入 rsyslog
Write journald metadata to rsyslog
我有一个设置,其中 docker 容器使用 journald
日志驱动程序来写入它们的日志。目前日志中的日志行被转发到主机上的 rsyslog 运行,但 syslog 行上的应用程序名称显示为 dockerd
.
作为解决方法,我想将日志元数据中的 CONTAINER_NAME
字段写入出现在 syslog 中的行中,这样我就可以确定主机的 syslog 发送后哪个容器写了哪一行到系统日志聚合服务器。
有什么建议吗?
我想最接近的你可以得到图像名称。您可以添加日志标签以在日志中显示图像名称。此功能已在 v1.11.0 中添加。例如:
docker run --log-driver=journald --log-opt tag="{{.ImageName}}
也看看 log tag docs。希望这有帮助。
我通过将其放入 rsyslog.conf
中设法使其正常工作
if ( $!CONTAINER_TAG == "mycontainer" ) then {
action(type="omfile" file="/var/log/mycontainer.log")
stop
}
我可以测试它是否适用于此
docker run --log-driver=journald --log-opt tag="mycontainer" centos:latest echo Pierre7
我在文件 /var/log/mycontainer 和日志中都得到了我的日志,我可以通过
找到它们
journalctl --unit docker CONTAINER_TAG=mycontainer
很难找到该信息,似乎在 redhat 文档中记录的唯一地方:https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/7/html/System_Administrators_Guide/s1-structured_logging_with_rsyslog.html
我的用例是,我希望获得该期刊的所有好处,但支持人员要求拥有普通文件。我们还将使用 splunkforwarder 来使用文件。
我能够通过在解析来自 journald 的结构化日志后定义一个模板来实现这一点。为了找出可用的属性,我 运行 journalctl -o verbose -n 10
rsyslog 有多种不同的方法来进行相同的配置,这是我在 CentOS 7 机器上的配置:
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
我有一个设置,其中 docker 容器使用 journald
日志驱动程序来写入它们的日志。目前日志中的日志行被转发到主机上的 rsyslog 运行,但 syslog 行上的应用程序名称显示为 dockerd
.
作为解决方法,我想将日志元数据中的 CONTAINER_NAME
字段写入出现在 syslog 中的行中,这样我就可以确定主机的 syslog 发送后哪个容器写了哪一行到系统日志聚合服务器。
有什么建议吗?
我想最接近的你可以得到图像名称。您可以添加日志标签以在日志中显示图像名称。此功能已在 v1.11.0 中添加。例如:
docker run --log-driver=journald --log-opt tag="{{.ImageName}}
也看看 log tag docs。希望这有帮助。
我通过将其放入 rsyslog.conf
中设法使其正常工作
if ( $!CONTAINER_TAG == "mycontainer" ) then {
action(type="omfile" file="/var/log/mycontainer.log")
stop
}
我可以测试它是否适用于此
docker run --log-driver=journald --log-opt tag="mycontainer" centos:latest echo Pierre7
我在文件 /var/log/mycontainer 和日志中都得到了我的日志,我可以通过
journalctl --unit docker CONTAINER_TAG=mycontainer
很难找到该信息,似乎在 redhat 文档中记录的唯一地方:https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/7/html/System_Administrators_Guide/s1-structured_logging_with_rsyslog.html
我的用例是,我希望获得该期刊的所有好处,但支持人员要求拥有普通文件。我们还将使用 splunkforwarder 来使用文件。
我能够通过在解析来自 journald 的结构化日志后定义一个模板来实现这一点。为了找出可用的属性,我 运行 journalctl -o verbose -n 10
rsyslog 有多种不同的方法来进行相同的配置,这是我在 CentOS 7 机器上的配置:
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