2021-04-29

logstash收集springboot日志

logstash收集springboot日志

maven依赖

<dependency> <groupId>net.logstash.logback</groupId> <artifactId>logstash-logback-encoder</artifactId> <version>5.1</version></dependency>

springboot 配置文件

logging: config: classpath:logback.

logstash

springboot-log.conf

# 文档 # https://www.elastic.co/guide/en/logstash/5.6/input-plugins.html# https://www.elastic.co/guide/en/logstash/6.1/input-plugins.htmlinput{	tcp {  mode => "server"  host => "0.0.0.0"  port => 4567  codec => json_lines	}}output{ # 为了模拟测试就先不放es了,在控制台输出测试看看	#elasticsearch{	# hosts=>["127.0.0.1:9200"]	# index => "springboot-elk-%{+YYYY.MM.dd}" #}	stdout{		codec => rubydebug	}}

测试接口

import java.time.LocalDateTime;import java.time.format.DateTimeFormatter;import javax.servlet.http.HttpServletRequest;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RestController;import org.springframework.web.context.request.RequestContextHolder;import org.springframework.web.context.request.ServletRequestAttributes;@RestControllerpublic class ApiTests {	protected final static Logger log = LoggerFactory.getLogger(ApiTests.class);		@GetMapping("/api/log")	public Object log() {		ServletRequestAttributes requestAttributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();		HttpServletRequest request = requestAttributes.getRequest();		String ip = request.getRemoteAddr();		String uri = request.getRequestURI();		String logStr = String.format("IP=[%s] send request URI = [%s]", ip, uri);		log.debug("[debug] "+logStr);		log.info("[info] "+logStr);		log.warn("[warn] "+logStr);		log.error("[error] "+logStr);		return "ok : "+ LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));	}	}

测试效果

先运行logstash

# linux: bin/logstash -f config/springboot-log.conf# windows: bin\logstash.bat -f config/springboot-log.conf

在运行springboot项目,然后调用测试接口:

观察logstash控制台输出:

{  "@timestamp" => 2021-04-28T15:40:43.182Z,   "level" => "DEBUG",   "port" => 57778, "thread_name" => "http-nio-8080-exec-1", "level_value" => 10000,  "@version" => "1",   "host" => "127.0.0.1", "logger_name" => "com.bart.elk.ApiTests",  "message" => "[debug] IP=[127.0.0.1] send request URI = [/api/log]"}{  "@timestamp" => 2021-04-28T15:40:43.182Z,   "level" => "INFO",   "port" => 57778, "thread_name" => "http-nio-8080-exec-1", "level_value" => 20000,  "@version" => "1",   "host" => "127.0.0.1", "logger_name" => "com.bart.elk.ApiTests",  "message" => "[info] IP=[127.0.0.1] send request URI = [/api/log]"}{  "@timestamp" => 2021-04-28T15:40:43.183Z,   "level" => "WARN",   "port" => 57778, "thread_name" => "http-nio-8080-exec-1", "level_value" => 30000,  "@version" => "1",   "host" => "127.0.0.1", "logger_name" => "com.bart.elk.ApiTests",  "message" => "[warn] IP=[127.0.0.1] send request URI = [/api/log]"}{  "@timestamp" => 2021-04-28T15:40:43.183Z,   "level" => "ERROR",   "port" => 57778, "thread_name" => "http-nio-8080-exec-1", "level_value" => 40000,  "@version" => "1",   "host" => "127.0.0.1", "logger_name" => "com.bart.elk.ApiTests",  "message" => "[error] IP=[127.0.0.1] send request URI = [/api/log]"}

说明运行成功!

参考说明:

logstash-logback-encoder

参考博客-1

参考博客-2

logstash 文档









原文转载:http://www.shaoqun.com/a/712748.html

跨境电商:https://www.ikjzd.com/

巨鲸:https://www.ikjzd.com/w/1986

海带宝:https://www.ikjzd.com/w/1548


logstash收集springboot日志maven依赖<dependency><groupId>net.logstash.logback</groupId><artifactId>logstash-logback-encoder</artifactId><version>5.1</version></de
商标抢注:https://www.ikjzd.com/w/1053
淘粉吧返利:https://www.ikjzd.com/w/1725
gtin:https://www.ikjzd.com/w/136
Wish也开始收税了?但不是所有人都要交……:https://www.ikjzd.com/home/22685
好友老公真的很绅士 口述我和好友老公的婚外情生活:http://lady.shaoqun.com/m/a/275232.html
老公出轨 骂刚出生的女儿是"烧钱罐:http://www.30bags.com/m/a/249476.html

No comments:

Post a Comment