springboot集成elk 二:springboot + elk 採集日誌

到logstash-2.0.0\bin下新建文件 logstash.confspring

 

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.confjson

 

 

 

 

引入依賴:springboot

<dependency>

     <groupId>net.logstash.logback</groupId>

     <artifactId>logstash-logback-encoder</artifactId>

     <version>5.1</version>

</dependency>

 

在resources下新建logback-spring.xmlapp

配置logback日誌信息,注意這裏配置的destination屬性,輸出的要和logstash配置的對應上,否則收集不上,內容以下:elasticsearch

<?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>

 

測試tcp

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:5601ide

刪除原來的索引匹配"test",新建索引匹配:springboot-logstash-2019.10.11測試

點擊Discover 搜索:查詢產品信息編碼

 

相關文章
相關標籤/搜索