在开发过程中我们需要将有用的日志存入ELK,方便以后的分析。因此需要在SpringMVC中集成logstash:
1.在pom中添加依赖
<!-- 添加ELK中的logstash -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.13</version>
</dependency>
<dependency>
<groupId>net.logstash.logback</groupId>
<artifactId>logstash-logback-encoder</artifactId>
<version>5.2</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>net.logstash.log4j</groupId>
<artifactId>jsonevent-layout</artifactId>
<version>1.7</version>
</dependency>
2.资源文件夹中添加logback.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<property name="springAppName" value="your-app-name" />
<!-- Example for logging into the build folder of your project -->
<property name="LOG_FILE" value="logs/${springAppName}.log"/>
<!-- Appender to log to console -->
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<!-- Minimum logging level to be presented in the console logs-->
<level>DEBUG</level>
</filter>
<encoder>
<pattern>
{
"level": "%level",
"service": "${springAppName:-}",
"pid": "${PID:-}",
"thread": "%thread",
"class": "%logger{40}",
"message": "%message"
}
</pattern>
<charset>UTF-8</charset>
</encoder>
</appender>
<!-- Appender to log to file -->
<appender name="flatfile" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_FILE}</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${LOG_FILE}.%d{yyyy-MM-dd}.gz</fileNamePattern>
<maxHistory>7</maxHistory>
</rollingPolicy>
<encoder>
<pattern>
{
"level": "%level",
"service": "${springAppName:-}",
"pid": "${PID:-}",
"thread": "%thread",
"class": "%logger{40}",
"message": "%message"
}
</pattern>
<charset>UTF-8</charset>
</encoder>
</appender>
<!-- Appender to log to file in a JSON format -->
<appender name="logstash" class="net.logstash.logback.appender.LogstashTcpSocketAppender">
<destination>xxx.xxx.xxx.xxx:yyyy</destination>
<encoder class="net.logstash.logback.encoder.LoggingEventCompositeJsonEncoder">
<providers>
<timestamp>
<timeZone>UTC</timeZone>
</timestamp>
<pattern>
<pattern>
{
"level": "%level",
"service": "${springAppName:-}",
"pid": "${PID:-}",
"thread": "%thread",
"class": "%logger{40}",
"message": "%message"
}
</pattern>
</pattern>
</providers>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="console"/>
<appender-ref ref="logstash"/>
<appender-ref ref="flatfile"/>
</root>
</configuration>
将<property name="springAppName" value="your-app-name" />中的your-app-name给我你的app名称。
<destination>xxx.xxx.xxx.xxx:yyyy</destination>中的地址改为自己logstash服务器地址和端口。
3.将logger日志改为slf4j
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
………………………………………………………………
……………………………………………………
………………………………………………………………
private static final Logger logger = LoggerFactory.getLogger(yourClass.class);
……………………………………………………
logger.info("logstash调测!");
完成