应用日志系统
- 在所有spring-boot的应用中,都是slf4j做日志调用接口,使用logback实现做日志记录
- 本地开发开发环境使用logback-local.xml,测试环境使用logback-dev.xml,生产环境使用logback-prod.xml日志文件。
所有项目maven依赖写法:
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.1.6</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.1.6</version>
</dependency>
在java代码中使用方法
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* 微信端账单相关接口实现类
* @author wangkun23 on 2017/12/3.
*/
@Service
public class RepaymentWechatServiceApiImpl implements RepaymentWechatServiceApi {
//必须定义成final
final Logger logger = LoggerFactory.getLogger(getClass());
.....如下写法
logger.info("还款计划的金额:{},页面上的金额:{}",totalAmount , amount);
}
开发环境配置文件logback-local.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<timestamp key="LOG_TIME" datePattern="yyyy-MM-dd"/>
<property name="APP_NAME" value="app-manager" />
<property name="LOG_PATTEN" value="[%d{yyyy-MM-dd HH:mm:ss.SSS}][%thread][%level][%logger][%L]%msg%n" />
<contextName>${APP_NAME}</contextName>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<charset>UTF-8</charset>
<pattern>${LOG_PATTEN}</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="STDOUT"/>
</root>
</configuration>
测试环境配置文件logback-dev.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<timestamp key="LOG_TIME" datePattern="yyyy-MM-dd"/>
<property name="APP_NAME" value="app-manager" />
<property name="LOG_PATH" value="/data/log/java/dev/${APP_NAME}/${LOG_TIME}" />
<property name="LOG_PATTEN" value="[%d{yyyy-MM-dd HH:mm:ss.SSS}][%thread][%level][%logger][%L]%msg%n" />
<contextName>${APP_NAME}</contextName>
<appender name="SYS_INFO" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_PATH}/system-info.log</file>
<append>true</append>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>INFO</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${LOG_PATH}/system-info.%d{yyyy-MM-dd}.%i.log.gz</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>100MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<maxHistory>100</maxHistory>
</rollingPolicy>
<encoder charset="UTF-8">
<pattern>${LOG_PATTEN}</pattern>
</encoder>
</appender>
<appender name="SYS_ERROR" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_PATH}/system-error.log</file>
<append>true</append>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>ERROR</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${LOG_PATH}/system-error.%d{yyyy-MM-dd}.%i.log.gz</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>100MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<maxHistory>100</maxHistory>
</rollingPolicy>
<encoder charset="UTF-8">
<pattern>${LOG_PATTEN}</pattern>
</encoder>
</appender>
<appender name="APP_INFO" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_PATH}/app-info.log</file>
<append>true</append>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>INFO</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${LOG_PATH}/app-info.%d{yyyy-MM-dd}.%i.log.gz</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>100MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<maxHistory>100</maxHistory>
</rollingPolicy>
<encoder charset="UTF-8">
<pattern>${LOG_PATTEN}</pattern>
</encoder>
</appender>
<appender name="APP_ERROR" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_PATH}/app-error.log</file>
<append>true</append>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>ERROR</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${LOG_PATH}/app-error.%d{yyyy-MM-dd}.%i.log.gz</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>100MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<maxHistory>100</maxHistory>
</rollingPolicy>
<encoder charset="UTF-8">
<pattern>${LOG_PATTEN}</pattern>
</encoder>
</appender>
<logger name="com.demo.app.manager" level="INFO" additivity="false">
<appender-ref ref="APP_INFO"/>
<appender-ref ref="APP_ERROR"/>
</logger>
<root level="INFO">
<appender-ref ref="SYS_INFO"/>
<appender-ref ref="SYS_ERROR"/>
</root>
</configuration>
生产环境配置文件logback-prod.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<timestamp key="LOG_TIME" datePattern="yyyy-MM-dd"/>
<property name="APP_NAME" value="app-manager" />
<property name="LOG_PATH" value="/data/log/java/prod/${APP_NAME}/${LOG_TIME}" />
<property name="LOG_PATTEN" value="[%d{yyyy-MM-dd HH:mm:ss.SSS}][%thread][%level][%logger][%L]%msg%n" />
<contextName>${APP_NAME}</contextName>
<appender name="SYS_INFO" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_PATH}/system-info.log</file>
<append>true</append>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>INFO</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${LOG_PATH}/system-info.%d{yyyy-MM-dd}.%i.log.gz</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>100MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<maxHistory>100</maxHistory>
</rollingPolicy>
<encoder charset="UTF-8">
<pattern>${LOG_PATTEN}</pattern>
</encoder>
</appender>
<appender name="SYS_ERROR" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_PATH}/system-error.log</file>
<append>true</append>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>ERROR</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${LOG_PATH}/system-error.%d{yyyy-MM-dd}.%i.log.gz</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>100MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<maxHistory>100</maxHistory>
</rollingPolicy>
<encoder charset="UTF-8">
<pattern>${LOG_PATTEN}</pattern>
</encoder>
</appender>
<appender name="APP_INFO" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_PATH}/app-info.log</file>
<append>true</append>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>INFO</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${LOG_PATH}/app-info.%d{yyyy-MM-dd}.%i.log.gz</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>100MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<maxHistory>100</maxHistory>
</rollingPolicy>
<encoder charset="UTF-8">
<pattern>${LOG_PATTEN}</pattern>
</encoder>
</appender>
<appender name="APP_ERROR" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_PATH}/app-error.log</file>
<append>true</append>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>ERROR</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${LOG_PATH}/app-error.%d{yyyy-MM-dd}.%i.log.gz</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>100MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<maxHistory>100</maxHistory>
</rollingPolicy>
<encoder charset="UTF-8">
<pattern>${LOG_PATTEN}</pattern>
</encoder>
</appender>
<logger name="com.demo.app.manager" level="INFO" additivity="false">
<appender-ref ref="APP_INFO"/>
<appender-ref ref="APP_ERROR"/>
</logger>
<root level="INFO">
<appender-ref ref="SYS_INFO"/>
<appender-ref ref="SYS_ERROR"/>
</root>
</configuration>