需求: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忘古、配置日志環(huán)境
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>