没有去百度,所以下次补全Logback及Logstash相关基础概念,如有想法自行百度一下
SpringBoot 2.0.6、JDK 1.8、logback 1.2.3、logstash-logback-encoder 5.3
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>net.logstash.logback</groupId>
<artifactId>logstash-logback-encoder</artifactId>
<version>5.3</version>
</dependency>
<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="false">
<property name="LOG_HOME" value="./logs" />
<!-- 根据需要自行配置 -->
<property name="APP_NAME" value="app_name" />
<!-- 控制台输出 -->
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
</encoder>
</appender>
<appender name="LOGSTASH" class="ch.qos.logback.core.rolling.RollingFileAppender">
<encoder charset="UTF-8" class="net.logstash.logback.encoder.LogstashEncoder"/>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${LOG_HOME}/${APP_NAME}-%d{yyyy-MM-dd}.log</fileNamePattern>
</rollingPolicy>
</appender>
<!-- 日志输出级别 -->
<root level="INFO">
<appender-ref ref="STDOUT" />
<appender-ref ref="LOGSTASH" />
</root>
</configuration>
{"@timestamp":"2019-05-23T22:26:33.447+08:00","@version":"1","message":"Mapped \"{[/actuator],methods=[GET],produces=[application/vnd.spring-boot.actuator.v2+json || application/json]}\" onto protected java.util.Map<java.lang.String, java.util.Map<java.lang.String, org.springframework.boot.actuate.endpoint.web.Link>> org.springframework.boot.actuate.endpoint.web.servlet.WebMvcEndpointHandlerMapping.links(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)","logger_name":"org.springframework.boot.actuate.endpoint.web.servlet.WebMvcEndpointHandlerMapping","thread_name":"main","level":"INFO","level_value":20000}
{"@timestamp":"2019-05-23T22:26:33.620+08:00","@version":"1","message":"Registering beans for JMX exposure on startup","logger_name":"org.springframework.jmx.export.annotation.AnnotationMBeanExporter","thread_name":"main","level":"INFO","level_value":20000}
{"@timestamp":"2019-05-23T22:26:33.622+08:00","@version":"1","message":"Bean with name 'dataSource' has been autodetected for JMX exposure","logger_name":"org.springframework.jmx.export.annotation.AnnotationMBeanExporter","thread_name":"main","level":"INFO","level_value":20000}
{"@timestamp":"2019-05-23T22:26:33.631+08:00","@version":"1","message":"Located MBean 'dataSource': registering with JMX server as MBean [com.zaxxer.hikari:name=dataSource,type=HikariDataSource]","logger_name":"org.springframework.jmx.export.annotation.AnnotationMBeanExporter","thread_name":"main","level":"INFO","level_value":20000}
以上为输出到文件的Logstash配置方式,当然也只支持直接输出到Logstash,如下配置:
<appender name="TCP_LOGSTASH" class="net.logstash.logback.appender.LogstashTcpSocketAppender">
<destination>IP地址:端口号</destination>
<encoder charset="UTF-8" class="net.logstash.logback.encoder.LogstashEncoder"/>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${LOG_HOME}/${APP_NAME}--info-%d{yyyy-MM-dd}.log</fileNamePattern>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>INFO</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
</rollingPolicy>
</appender>
这里就不讲Logstash安装和使用(我也不是那么熟<_>!),可参考官网,附上下载地址,Logstash的使用可参考Logstash 最佳实践