一文秒懂logstash收集springboot日志的方法
maven依賴
<dependency> <groupId>net.logstash.logback</groupId> <artifactId>logstash-logback-encoder</artifactId> <version>5.1</version> </dependency>
springboot 配置文件
logging: config: classpath:logback.xml
logback.xml
<?xml version="1.0" encoding="UTF-8"?> <configuration> <include resource="org/springframework/boot/logging/logback/base.xml" /> <!--======================================= 本地變量 ======================================== --> <!--在沒有定義${LOG_HOME}系統變量的時候,可以設置此本地變量。提交測試、上線時,要將其註釋掉,使用系統變量。 --> <property name="LOG_HOME" value="logs/spring.log" /> <!-- 應用名稱:和統一配置中的項目代碼保持一致(小寫) --> <property name="APP_NAME" value="log" /> <!--日志文件保留天數 --> <property name="LOG_MAX_HISTORY" value="30" /> <!--定義日志文件的存儲地址 勿在 LogBack 的配置中使用相對路徑 --> <!--應用日志文件保存路徑 --> <property name="LOG_APP_HOME" value="${APP_NAME}/%d{yyyy-MM-dd}" /> <!--=========================== 按照每天生成日志文件:默認配置=================================== --> <!-- 控制臺輸出 --> <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> <!--格式化輸出:%d表示日期,%thread表示線程名,%-5level:級別從左顯示5個字符寬度%msg:日志消息,%n是換行符 --> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern> </encoder> </appender> <!-- 按照每天生成日志文件:主項目日志 --> <appender name="APP" class="ch.qos.logback.core.rolling.RollingFileAppender"> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <!--日志文件輸出的文件名 --> <FileNamePattern>${LOG_APP_HOME}/base.%d{yyyy-MM-dd}.log </FileNamePattern> <!--日志文件保留天數 --> <MaxHistory>${LOG_MAX_HISTORY}</MaxHistory> </rollingPolicy> <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> <!--格式化輸出:%d表示日期,%thread表示線程名,%-5level:級別從左顯示5個字符寬度%msg:日志消息,%n是換行符 --> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{500} - %msg%n</pattern> </encoder> </appender> <!--=============================== 日志輸出: 默認主業務日志 ====================================== --> <logger name="org.springframework"> <level value="WARN" /> </logger> <logger name="org.apache.shiro"> <level value="WARN" /> </logger> <logger name="freemarker"> <level value="WARN" /> </logger> <logger name="org.hibernate"> <level value="WARN" /> </logger> <logger name="org.hibernate.SQL"> <level value="DEBUG" /> </logger> <logger name="org.apache.ibatis"> <level value="DEBUG" /> </logger> <logger name="org.mybatis.spring"> <level value="DEBUG" /> </logger> <logger name="com.alibaba.druid"> <level value="DEBUG,INFO" /> </logger> <!-- 這裡是你的業務的包名 --> <logger name="com.bart.elk"> <level value="DEBUG" /> </logger> <!-- logstash ip和暴露的端口,我目前理解就是通過這個地址把日志發送過去 --> <appender name="LOGSTASH" class="net.logstash.logback.appender.LogstashTcpSocketAppender"> <!-- 和logstash 的input 配置的端口保持一致 --> <destination>localhost:4567</destination> <encoder charset="UTF-8" class="net.logstash.logback.encoder.LogstashEncoder" /> </appender> <root level="warn,info,debug"> <appender-ref ref="APP" /> <appender-ref ref="STDOUT" /> <appender-ref ref="LOGSTASH" /> <appender-ref ref="CONSOLE" /> </root> </configuration>
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.html input{ 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; @RestController public 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項目,然後調用測試接口:http://localhost:8080/api/log
觀察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 文檔
到此這篇關於一文秒懂logstash收集springboot日志的方法的文章就介紹到這瞭,更多相關logstash收集springboot日志內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!
推薦閱讀:
- springboot如何去除debug日志
- Spring Boot console log 格式自定義方式
- springboot logback如何從apollo配置中心讀取變量
- 使用Logback設置日志級別
- Springboot允許logger.debug輸出日志方式