需求:springcloud项目中需要对行为日志进行收集与上报、分析
背景:项目开始阶段日志框架使用的是spring-boot-starter-log4j2,后因与logstash集成不够友好,将日志框架替换为spring-boot-starter-logging。spring-boot-starter-logging日志底层使用的是logback日志框架。
1、日志配置依赖如下:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
<version>2.1.7.RELEASE</version>
</dependency>
<dependency>
<groupId>net.logstash.logback</groupId>
<artifactId>logstash-logback-encoder</artifactId>
<version>5.2</version>
</dependency>
2、日志文件输出位置,日志文件名称从项目配置中获取,故进行配置
log:
path: ./log
spring:
application:
name: xxxx
3、logstash地址配置
logstash:
server-addr: 192.168.135.99:4560
4、配置日志环境
logging:
config: classpath:logback-busi-dev.xml
5、新建logback.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<Configuration name="logback-dev-config" debug="true" scan="true" scanPeriod="60 seconds">
<property name="log_pattern" value="%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level [%thread]%logger -%msg%n"/>
<springProperty scope="context" name="logstash.server-addr" source="logstash.server-addr"/>
<springProperty scope="context" name="log.path" source="log.path"/>
<springProperty scope="context" name="log.name" source="spring.application.name"/>
<!--控制台-->
<appender name="Console" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>${log_pattern}</Pattern>
</encoder>
</appender>
<!-- 输出到Controller文件 -->
<appender name="sysLogFile" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${log.path}/${log.name}-sys.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${log.path}/${log.name}-sys-%d{yyyy-MM-dd}-%i.log</fileNamePattern>
<maxHistory>60</maxHistory>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>10MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
<encoder>
<Pattern>${log_pattern}</Pattern>
</encoder>
</appender>
<!-- 输出到Controller文件 -->
<appender name="controllerLogFile" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${log.path}/${log.name}-controller.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${log.path}/${log.name}-controller-%d{yyyy-MM-dd}-%i.log</fileNamePattern>
<maxHistory>60</maxHistory>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>10MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
<encoder>
<Pattern>${log_pattern}</Pattern>
</encoder>
</appender>
<!-- 输出到Service文件 -->
<appender name="serviceLogFile" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${log.path}/${log.name}-service.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${log.path}/${log.name}-service-%d{yyyy-MM-dd}-%i.log</fileNamePattern>
<maxHistory>60</maxHistory>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>10MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
<encoder>
<Pattern>${log_pattern}</Pattern>
</encoder>
</appender>
<appender name="stashLog" class="net.logstash.logback.appender.LogstashTcpSocketAppender">
<destination>${logstash.server-addr}</destination>
<encoder class="net.logstash.logback.encoder.LoggingEventCompositeJsonEncoder">
<jsonFactoryDecorator class="net.logstash.logback.decorate.CharacterEscapesJsonFactoryDecorator">
<escape>
<targetCharacterCode>10</targetCharacterCode>
<escapeSequence>\u2028</escapeSequence>
</escape>
</jsonFactoryDecorator>
<providers>
<pattern>
<pattern>
{
"serviceName":"xx-businessx",
"timestamp":"%date{ISO8601}",
"message":"%msg%n"
}
</pattern>
</pattern>
</providers>
</encoder>
<keepAliveDuration>5 minutes</keepAliveDuration>
</appender>
<Logger name="com.xxx.business.controller" level="info" additivity="false">
<appender-ref ref="Console"/>
<appender-ref ref="controllerLogFile"/>
<appender-ref ref="stashLog"/>
</Logger>
<Logger name="com.xxx.business.service" level="info" additivity="false">
<appender-ref ref="Console"/>
<appender-ref ref="serviceLogFile"/>
</Logger>
<root level="info">
<appender-ref ref="Console"/>
<appender-ref ref="sysLogFile"/>
</root>
</Configuration>