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]"
}
說明執行成功!
參考說明: