到logstash-2.0.0\bin下新建文件 logstash.conf
input { tcp { mode => "server" host => "0.0.0.0" port => 4560 codec => json_lines } } output { elasticsearch { hosts => "localhost:9200" index => "springboot-logstash-%{+YYYY.MM.dd}" } }
到logstash-2.0.0\bin下执行:logstash.bat agent -f logstash.conf
引入依赖:
<dependency> <groupId>net.logstash.logback</groupId> <artifactId>logstash-logback-encoder</artifactId> <version>5.1</version> </dependency>
在resources下新建logback-spring.xml
配置logback日志信息,注意这里配置的destination属性,输出的要和logstash配置的对应上,不然收集不上,内容如下:
<?xml version="1.0" encoding="UTF-8"?> <configuration> <include resource="org/springframework/boot/logging/logback/base.xml" /> <appender name="LOGSTASH" class="net.logstash.logback.appender.LogstashTcpSocketAppender"> <destination>127.0.0.1:4560</destination> <!--配置logStash 服务地址--> <!-- 日志输出编码 --> <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>
测试
private static final Logger logger = LoggerFactory.getLogger(GoodsController.class); @Autowired private GoodsDao goodsDao; //http://localhost:8888/getOne?id=1525417362754 @GetMapping("/getOne") public GoodsInfo getOne(long id){ logger.info("查询产品信息:id:"+id); GoodsInfo goodsInfo = goodsDao.findOne(id); return goodsInfo; }
访问kibana http://127.0.0.1:5601
删除原来的索引匹配"test",新建索引匹配:springboot-logstash-2019.10.11
点击Discover 搜索:查询产品信息
原文地址:https://www.cnblogs.com/brant/p/11680155.html
时间: 2024-10-03 23:27:22