一、Logstash简介
Logstash是一个接收,处理,转发日志的工具。支持系统日志,webserver日志,错误日志,应用日志,包括所有可以抛出来的日志类型。
二、Maven依赖
<!--logstash-->
<dependency>
<groupId>net.logstash.logback</groupId>
<artifactId>logstash-logback-encoder</artifactId>
<version>5.3</version>
</dependency>
三、日志配置
- 创建文件:logback-spring.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/base.xml" />
<!--配置LOGSTASH-->
<appender name="LOGSTASH" class="net.logstash.logback.appender.LogstashTcpSocketAppender">
<!--logstash IP地址-->
<destination>192.168.77.132:4560</destination>
<!-- 日志输出编码 -->
<encoder charset="UTF-8"
class="net.logstash.logback.encoder.LoggingEventCompositeJsonEncoder">
<providers>
<timestamp>
<timeZone>UTC</timeZone>
</timestamp>
<pattern>
<!--日志格式-->
<pattern>
{
"logLevel": "%level",
"serviceName": "${springAppName:-}",
"pid": "${PID:-}",
"thread": "%thread",
"class": "%logger{40}",
"rest": "%message"
}
</pattern>
</pattern>
</providers>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="LOGSTASH" />
<appender-ref ref="CONSOLE" />
</root>
</configuration>
- 添加测试控制器,并输出日志
@RestController
@RequestMapping("goods")
@Slf4j
public class GoodsController {
@GetMapping
public String getUser(){
log.info("Logstash日志采集测试");
return "admin";
}
}
四、Logstash配置
logstash配置如下,输入要与刚刚配置的对应,输出为本地es:
input {
tcp {
mode => "server"
host => "0.0.0.0"
port => 4560
codec => json_lines
}
}
output {
elasticsearch {
hosts => "192.168.77.132:9200"
index => "springboot-logstash-%{+YYYY.MM.dd}"
user => elastic
password => xW9dqAxThD5U4ShQV1JT
}
}
五、测试结果
-
查看索引管理
-
添加索引模式
-
查询结果
六、常见问题
- Logback configuration error detected
检查配置文件是否配置了es的用户与密码。
user => elastic
password => xW9dqAxThD5U4ShQV1JT