springboot集成elk 二:springboot + elk 采集日志

Posted 陈小兵

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了springboot集成elk 二:springboot + elk 采集日志相关的知识,希望对你有一定的参考价值。

到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 搜索:查询产品信息

 

 

以上是关于springboot集成elk 二:springboot + elk 采集日志的主要内容,如果未能解决你的问题,请参考以下文章

springboot 2.0 集成elk 7.6.2

springboot 2.0 集成 kafka 2.6.2(集群) + elk 7.6.2(集群)

ELK Springboot集成ElasticSearch7.6.2 SpringData

springboot集成elk 一: springboot + Elasticsearch

springboot集成elk 四:springboot + Elasticsearch+Jest

springboot集成elk 三:springboot + Elasticsearch Rest-Client