我在 SpringBoot 应用程序中使用 Logback 和 Logstash。
在 logback.xml 我有一个带有服务名称的属性,就像:
<configuration>
<include resource="org/springframework/boot/logging/logback/defaults.xml" />
<include resource="org/springframework/boot/logging/logback/console-appender.xml" />
<property name="spring.application.name" calue="service"/>
<appender name="stash" class="net.logstash.logback.appender.LogstashTcpSocketAppender">
<destination>localhost:9600</destination>
<encoder class="net.logstash.logback.encoder.LogstashEncoder"/>
</appender>
<root level="INFO">
<appender-ref ref="CONSOLE" />
<appender-ref ref="stash" />
</root>
</configuration>
Logstash conf 文件如下所示:
input{ tcp{
port=> 9600
host=>logstash
}
}
filter {
grok {
match => {
"message" =>
"^%{TIMESTAMP_ISO8601:timestamp}\s+%{LOGLEVEL:level}\s+%{NUMBER:pid}\s+---\s+\[\s*%{USERNAME:thread}\s*\]\s+%{JAVAFILE:class}\s*:\s*%{DATA:themessage}(?:\n+(?<stacktrace>(?:.|\r|\n)+))?$"
}
}
date {
match => [ "timestamp" , "yyyy-MM-dd HH:mm:ss.SSS" ]
}
mutate {
remove_field => ["@version"]
add_field => {
"appid" => "%{[path]}"
}
add_field => {
"levell" => "level"
}
add_field => {
"mensage" => "message"
}
}
}
output{
elasticsearch {
hosts => ["elasticsearch"]
index => "indice"
}
stdout{}
}
如何将 logback 文件中的应用程序名称属性添加为字段?