1.引入maven依赖:
<!-- slf4j的依赖 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.30</version>
</dependency>
<!-- log4j2的依赖 -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.13.3</version>
</dependency>
<!-- log4j2的依赖 -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.13.3</version>
</dependency>
<!-- log4j-slf4j的依赖 -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>2.13.3</version>
</dependency>
2.引入配置文件log4j2.xml
<?xml version="1.0" encoding="UTF-8"?> <Configuration> <Properties> <property name="LOG_HOME">D:/logs</property> <!-- 定义日志格式 --> <Property name="log.pattern">%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36}%n%msg%n</Property> <!-- 定义文件名变量 --> <Property name="file.err.filename">${LOG_HOME}/log_error.log</Property> <Property name="file.err.pattern">${LOG_HOME}/error_$${date:yyyy-MM}/log_error-%d{yyyy-MM-dd HH-mm}-%i.log.gz</Property> <!-- 定义文件名变量 --> <Property name="file.info.filename">${LOG_HOME}/log_info.log</Property> <Property name="file.info.pattern">${LOG_HOME}/info_$${date:yyyy-MM}/log_info-%d{yyyy-MM-dd HH-mm}-%i.log.gz</Property> </Properties> <!-- 定义Appender,即目的地 --> <Appenders> <!-- 定义输出到屏幕 --> <Console name="console" target="SYSTEM_OUT"> <!-- 日志格式引用上面定义的log.pattern --> <PatternLayout pattern="${log.pattern}" /> </Console> <!-- 定义输出到文件,文件名引用上面定义的file.err.filename --> <RollingFile name="info" bufferedIO="true" fileName="${file.info.filename}" filePattern="${file.info.pattern}"> <PatternLayout pattern="${log.pattern}" /> <Policies> <!-- 根据文件大小自动切割日志 --> <SizeBasedTriggeringPolicy size="10 MB" /> </Policies> <!-- 保留最近10份 --> <DefaultRolloverStrategy max="20" /> </RollingFile> <!-- 定义输出到文件,文件名引用上面定义的file.err.filename --> <RollingFile name="err" bufferedIO="true" fileName="${file.err.filename}" filePattern="${file.err.pattern}"> <PatternLayout pattern="${log.pattern}" /> <Policies> <!-- 根据文件大小自动切割日志 --> <SizeBasedTriggeringPolicy size="10 MB" /> </Policies> <!-- 保留最近10份 --> <DefaultRolloverStrategy max="20" /> </RollingFile> </Appenders> <Loggers> <Root level="info"> <!-- 对info级别的日志,输出到console --> <AppenderRef ref="console" level="info" /> <!-- 对error级别的日志,输出到err,即上面定义的RollingFile --> <AppenderRef ref="err" level="error" /> <!-- 对error级别的日志,输出到err,即上面定义的RollingFile --> <AppenderRef ref="info" level="info" /> </Root> </Loggers> </Configuration>
3.测试类
public class Test { public static void main(String[] args) { Logger logger = LoggerFactory.getLogger(Test.class); for (int i =0 ;i<32000;i++){ logger.trace("traceaaaaaaaaaaaaaaaa"); logger.debug("debugaaaaaaaaaaaa"); logger.info("infoaaaaaaaaaaaa"); logger.warn("warnaaaaaaaaaa"); logger.error("erroraaaaaaaaaaaaa"); } } }
说明:
PatternLayout自定义日志布局:
二:springboot整合log4j2:
1.pom.xml文件
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions>
<!-- 去掉springboot默认的日志配置 --> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </exclusion> </exclusions> </dependency> <dependency> <!-- 引入log4j2依赖 --> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-log4j2</artifactId> </dependency>
2.引入配置文件log4j2.xml
<?xml version="1.0" encoding="UTF-8"?> <Configuration> <Properties> <property name="LOG_HOME">D:/logs</property> <!-- 定义日志格式 --> <Property name="log.pattern">%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36}%n%msg%n</Property> <!-- 定义文件名变量 --> <Property name="file.err.filename">${LOG_HOME}/log_error.log</Property> <Property name="file.err.pattern">${LOG_HOME}/error_$${date:yyyy-MM}/log_error-%d{yyyy-MM-dd HH-mm}-%i.log.gz</Property> <!-- 定义文件名变量 --> <Property name="file.info.filename">${LOG_HOME}/log_info.log</Property> <Property name="file.info.pattern">${LOG_HOME}/info_$${date:yyyy-MM}/log_info-%d{yyyy-MM-dd HH-mm}-%i.log.gz</Property> </Properties> <!-- 定义Appender,即目的地 --> <Appenders> <!-- 定义输出到屏幕 --> <Console name="console" target="SYSTEM_OUT"> <!-- 日志格式引用上面定义的log.pattern --> <PatternLayout pattern="${log.pattern}" /> </Console> <!-- 定义输出到文件,文件名引用上面定义的file.err.filename --> <RollingFile name="info" bufferedIO="true" fileName="${file.info.filename}" filePattern="${file.info.pattern}"> <PatternLayout pattern="${log.pattern}" /> <Policies> <!-- 根据文件大小自动切割日志 --> <SizeBasedTriggeringPolicy size="10 MB" /> </Policies> <!-- 保留最近10份 --> <DefaultRolloverStrategy max="20" /> </RollingFile> <!-- 定义输出到文件,文件名引用上面定义的file.err.filename --> <RollingFile name="err" bufferedIO="true" fileName="${file.err.filename}" filePattern="${file.err.pattern}"> <PatternLayout pattern="${log.pattern}" /> <Policies> <!-- 根据文件大小自动切割日志 --> <SizeBasedTriggeringPolicy size="10 MB" /> </Policies> <!-- 保留最近10份 --> <DefaultRolloverStrategy max="20" /> </RollingFile> </Appenders> <Loggers> <Root level="info"> <!-- 对info级别的日志,输出到console --> <AppenderRef ref="console" level="info" /> <!-- 对error级别的日志,输出到err,即上面定义的RollingFile --> <AppenderRef ref="err" level="error" /> <!-- 对error级别的日志,输出到err,即上面定义的RollingFile --> <AppenderRef ref="info" level="info" /> </Root> </Loggers> </Configuration>
3.修改application.yml文件
logging: config: classpath:log4j2.xml