栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringBoot:通过logstash统一收集日志

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringBoot:通过logstash统一收集日志

1.下载Logstash 7.3.1,解压,在config目录创建springboot-log.conf,

input{
	tcp {
        mode => "server"
        host => "0.0.0.0"
        port => 8888
        codec => json_lines
	}
}

output{
	#elasticsearch{
	#    hosts=>["127.0.0.1:9200"]
	#    index => "springboot"
    #}
	stdout{
		codec => rubydebug
	}
}

2.启动Logstash,

binlogstash.bat -f config/springboot-log.conf

Linux环境:

bin/logstash -f config/springboot-log.conf

3.创建spring boot应用,并在resources中创建logback-spring.xml



    
    
        
            %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n
        
    



    
        localhost:8888
        
    

    
        
        
    


4.在pom.xml中增加依赖,

        
            net.logstash.logback
            logstash-logback-encoder
            7.0
        

5.添加controller,并在其中增加接口,

package cn.edu.tju.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@RestController
public class TestController {
    protected final static Logger log = LoggerFactory.getLogger(TestController.class);
    @RequestMapping("/test")
    public String test() {
        log.info("this is a log from spring boot!");
        return  new Date().toLocaleString();
    }
}

6.启动应用,并访问http://localhost:8085/test,
在Logstash的输出可以看到日志输出:

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/710004.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号