Wildfly json 未应用日志格式化程序动态配置

Wildfly json log formatter dynamic configuration not applied

Wildfly 20 与 Logstash 实例连接,监听 tcp 端口 5300:

logstash.conf:

input {
 tcp {
    codec => json
    port => "5300"
  }
}

output {
  stdout {}
}

利用其内置的 json 套接字连接日志记录功能,如 中所述,Wildfly 在 Wildfly CLI 上配置,输入以下语句序列(自动以 standalone.xml 结尾):

/subsystem=logging/json-formatter=LOG-STASH:add(key-overrides={timestamp=@timestamp,message=@message,logger-name=@source,host-name=@source_host}, exception-output-type=formatted)

/socket-binding-group=standard-sockets/remote-destination-outbound-socket-binding=log-stash:add(host=localhost, port=8000)
/subsystem=logging/socket-handler=LOGSTASH-SOCKET:add(named-formatter=LOG-STASH, outbound-socket-binding-ref=log-stash, level=DEBUG)

/subsystem=logging/async-handler=LOGSTASH-ASYNC:add(queue-length=512, subhandlers=[LOGSTASH-SOCKET])

/subsystem=logging/root-logger=ROOT:add-handler(name=LOGSTASH-ASYNC)

它在 logstash 节点的标准输出上生成日志语句,例如:

{
          "level" => "DEBUG",
           "host" => "gateway",
      "processId" => 14972,
       "sequence" => 34696,
       "@version" => "1",
        "@source" => "com.myapplication.TaskService",
   "@source_host" => "device-01",
     "threadName" => "EJB default - 6",
       "threadId" => 215,
"loggerClassName" => "org.slf4j.impl.Slf4jLogger",
            "mdc" => {},
            "ndc" => "",
           "port" => 64210,
    "processName" => "jboss-modules.jar",
     "@timestamp" => 2021-03-31T14:10:19.869Z,
       "@message" => "task execution successfull: MailDaemon"
}

这只是 目标的一半,需要另一组属性名称(个人 json 日志消息的)以适应我们的企业 logstash 实例. 特别是,尽管配置了“host-name”和“logger-name”,但都没有写;而是记录“@source_host”和@source。

日志格式器 LOG-STASH 的进一步适配部分成功。

1) /subsystem=logging/json-formatter=LOG-STASH:write-attribute(name="meta-data",value={service="myapplication-api", serviceversion="1.1.0", instanceId="myapplication-api-1.1.0"})
2) /subsystem=logging/json-formatter=LOG-STASH:write-attribute(name="key-overrides",value=[severity=level,timestamp=@timestamp,message=msg,logger-name=@source,host-name=@source_host])

进一步简化导致存储属性,但未应用:

3) /subsystem=logging/json-formatter=LOG-STASH:write-attribute(name="key-overrides",value={"level"="severity"})
4) /subsystem=logging/json-formatter=LOG-STASH:read-attribute(name="key-overrides")
  1. 添加了作品和元数据。 2. 和 3. 没有结果。 4. 打印出来像

     INFO  [org.jboss.as.cli.CommandContext] {
         "outcome" => "success",
         "result" => {"level" => "severity"}
     }
     {
         "outcome" => "success",
         "result" => {"level" => "severity"}
     }
    

通过以上设置,以下 Wildfly CLI 命令成功重命名了所需键的默认值:

/subsystem=logging/json-formatter=LOG-STASH:write-attribute(name="key-overrides",value={"level"="severity","sequence"="trace","thread-id"="pid","logger-class-name"="class","thread-name"="thread"})

这些设置最终位于 standalone.xmllogging.properties 磁盘上的同一文件夹中。 在我工作期间,两个文件中配置的密钥之间存在差异。

请注意,像 threadId 这样的驼峰式键名会产生配置错误。您必须改用 thread-id。我通过检查 JBoss 日志库发现了这一点,即查看 Java 源代码。

生成的日志输出例如

{
               "pid" => 212,
           "message" => "Synchronizaing finished in 0ms",
          "@version" => "1",
        "loggerName" => "com.myapp.Cache",
        "@timestamp" => 2021-04-08T13:49:00.178Z,
              "port" => 59182,
       "processName" => "jboss-modules.jar",
             "trace" => 4245,
              "host" => "gateway",
          "severity" => "DEBUG",
         "processId" => 10536,
               "mdc" => {},
          "hostName" => "host-alpha",
         "timestamp" => "2021-04-08T15:49:00.176+02:00",
             "class" => "org.slf4j.impl.Slf4jLogger",
               "ndc" => "",
            "thread" => "EJB default - 7"
}

更好的是,让字段 mdcndc 被剥夺输出。