依赖:
1、springboot集成logstash时,注意 spring-boot-starter-parent和logstash的版本号。
2、logback中logstash的配置:注意 <destination>127.0.0.1:4560</destination> 配置。
XML
<!-- 输出到logstash的相关日志 appender-->
<appender name="LOGSTASH" class="net.logstash.logback.appender.LogstashTcpSocketAppender">
<!-- 配置可以访问的logstash日志收集端口 -->
<destination>127.0.0.1:4560</destination>
<!-- <encoder charset="UTF-8" class="net.logstash.logback.encoder.LogstashEncoder">-->
<!-- <!– 在elasticsearch的index中追加applicationName字段 –>-->
<!-- <customFields>{"applicationName":"logstash-test"}</customFields>-->
<!-- </encoder>-->
<encoder charset="UTF-8" class="net.logstash.logback.encoder.LoggingEventCompositeJsonEncoder">
<providers>
<timestamp>
<timeZone>Asia/Shanghai</timeZone>
</timestamp>
<!--自定义日志输出格式-->
<pattern>
<pattern>
{
"project": "wxpublic",
"level": "%level",
"service": "${APP_NAME:-}",
"pid": "${PID:-}",
"thread": "%thread",
"class": "%logger",
"message": "%message",
"stack_trace": "%exception{20}"
}
</pattern>
</pattern>
</providers>
</encoder>
</appender>
<!-- 号码相关信息日志,输出到logstash-->
<logger name="logStashTest01Log" level="INFO" additivity="true">
<appender-ref ref="LOGSTASH"/>
</logger>
3、本地安装Logstash、Elasticsearch、Kibana,注意版本号要保持一致。
4、Logstash的启动配置:
在Logstash的config目录下创建20230424-test05.conf配置文件,指定logstash的输入与输出,input中的端口要和logback-spring.xml配置文件中的配置保持一致,output中指定了日志输出的目的地Elasticsearch的地址、索引。内容如下:
XML
input {
tcp {
mode => "server"
host => "0.0.0.0"
port => 4560
codec => json_lines
}
}
output{
elasticsearch {
hosts => ["127.0.0.1:9200"]
index => "logstash-test-%{+YYYY.MM.dd}"
}
}
指定配置启动命令:
在bin目录下,打开控制台,输入以下命令启动Logstash:
XML
logstash -f ../config/20230424-test05.conf --config.reload.automatic
linux 系统命令:(需加 ./)
XML
./logstash -f ../config/20230424-test05.conf --config.reload.automatic
若命令失败,则在往前一级目录:(可不加 --config.reload.automatic)
XML
logstash -f ../xxx/config/20230424-test05.conf
filter
filter{
grok{
match => {"message" => "%{COMBINEDAPACHELOG}" }
}
mutate{
# 重命名字段
rename => {"clientip" => "cip"}
}
mutate{
# 移出特定字段
remove_field => ["timestamp","agent"]
}
geoip{
# 由于上面将clientip修改为了cip,故此处配置cip,如果没有rename字段则用clientip
source => "cip"
}
}
filter {
ruby {
code => "
array1 = event.get('message').split(';,;')
array1.each do |temp1|
if temp1.nil? then
next
end
array2 = temp1.split('=')
key = array2[0]
value = array2[1]
if key.nil? then
next
end
event.set(key, value)
end
"
remove_field => [ "message" ]
}
}