您好,登錄后才能下訂單哦!
怎么利用logstash收集springboot日志?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。
<dependency> <groupId>net.logstash.logback</groupId> <artifactId>logstash-logback-encoder</artifactId> <version>5.1</version> </dependency>
logging: config: classpath: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>
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]" }
看完上述內容是否對您有幫助呢?如果還想對相關知識有進一步的了解或閱讀更多相關文章,請關注億速云行業資訊頻道,感謝您對億速云的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。