gpt4 book ai didi

logging - 将日志元数据写入 rsyslog

转载 作者:行者123 更新时间:2023-12-02 18:23:53 24 4
gpt4 key购买 nike

我有一个设置,其中 docker 容器使用 journald日志驱动程序来写他们的日志。当前日志中的日志行被转发到主机上运行的 rsyslog,但 syslog 行上的应用程序名称显示为 dockerd .

作为一种解决方法,我想写 CONTAINER_NAME字段将日志元数据形成到出现在 syslog 中的行中,因此我可以识别在主机的 syslog 已发送到 syslog 聚合服务器后哪个容器写了哪一行。

有什么建议么?

最佳答案

在解析来自 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
  • 关于logging - 将日志元数据写入 rsyslog,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39237801/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com