Log4j 2 - RollingFileAppender example 日志分割
2017-10-11 15:21
603 查看
echnologies used: Java SE 1.8 | Log4j 2.8.2 | Maven 3.3.9 | Jackson API 2.8.7 | Eclipse Neon.3RollingFileAppender is a file appender which rolls over the log files once it has reached a certain size limit or date/time pattern no longer applies.In this post, I will show you how to use the
The following is the complete example of
RollingFileAppendertobackup and compress the old log files based on - Date and TimeSize of log fileCron expression
Jar dependencies
Editpom.xmlfile and add log4j2 and JacksonAPI dependencies in it.
<dependencies> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>2.8.2</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-api</artifactId> <version>2.8.2</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.8.7</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.8.7</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.8.7</version> </dependency> </dependencies>
Rolling based on Date and Time
You can use the TimeBasedTriggeringPolicy torollover the log file based on the date and time pattern used in<FilePattern/>elementas follows.
<RollingFile name="RollingFile"> <FileName>C:/log/mylog.log</FileName> <FilePattern>C:/log/time-based-logs/%d{yyyy-MM-dd-hh-mm}.log.zip</FilePattern> <PatternLayout> <Pattern>%d{yyyy-MMM-dd HH:mm:ss a} [%t] %-5level %logger{36} - %msg%n</Pattern> </PatternLayout> <Policies> <TimeBasedTriggeringPolicy interval="2" modulate="true" /> </Policies> <DefaultRolloverStrategy max="5" /> </RollingFile>Here are sample date/time patterns for rolling of files base on date/time.
DATE/TIME Pattern | Description | Intervale Attribute Example |
---|---|---|
%d{yyyy-MM-dd-hh-mm}.log.zip | Roll the log files every minutes | If interval=2, rollovers will occur every 2nd minutes.E.g. - 2017-07-26-09-57.log.zip, 2017-07-26-09-59.log.zip, 2017-07-26-10-01.log.zip, 2017-07-26-10-03.log.zip etc.. |
%d{yyyy-MM-dd-hh}.log.zip | Roll the log files hourly | If interval=4, rollovers will occur every 4 hours.E.g. - 2017-07-26-09.log.zip, 2017-07-26-10.log.zip, 2017-07-26-11.log.zip etc.. |
%d{yyyy-MM-dd}.log.zip | Roll the log files daily | If interval=1, rollovers will occur every day.E.g. - 2017-07-26.log.zip, 2017-07-27.log.zip etc.. |
log4j2.xmlfilefor rolling files every 2nd minutes.log4j2.xml
<?xml version="1.0" encoding="UTF-8"?><Configuration><Appenders><!-- Console Appender --><Console name="Console" target="SYSTEM_OUT"><PatternLayout pattern="%d{yyyy-MMM-dd HH:mm:ss a} [%t] %-5level %logger{36} - %msg%n" /></Console><!-- Rolling File Appender --><RollingFile name="RollingFile"> <FileName>C:/log/mylog.log</FileName> <FilePattern>C:/log/time-based-logs/%d{yyyy-MM-dd-hh-mm}.log.zip</FilePattern> <PatternLayout> <Pattern>%d{yyyy-MMM-dd HH:mm:ss a} [%t] %-5level %logger{36} - %msg%n</Pattern> </PatternLayout> <Policies> <TimeBasedTriggeringPolicy interval="2" modulate="true" /> </Policies> <DefaultRolloverStrategy max="5" /> </RollingFile></Appenders><Loggers><Logger name="com.boraji.tutorial.log4j2" level="debug" additivity="false"><AppenderRef ref="RollingFile" /><AppenderRef ref="Console" /></Logger><Root level="trace"><AppenderRef ref="Console" /></Root></Loggers></Configuration>The
<DefaultRolloverStrategy>element definea rollover strategy that will keep up to 5 files before removing them.A simple Java program to test the above log4j 2 configuration.MainApp.java
package com.boraji.tutorial.log4j2; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; public class MainApp { private static final Logger logger = LogManager.getLogger(MainApp.class); public static void main(String[] args) { for (int i = 0; i < 10000; i++) { logger.info("Rolling file appender example..."); try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } } } }
Rolling based on size of file
You can use the SizeBasedTriggeringPolicy torollover the log file based on the size of file as follows.<RollingFile name="RollingFile"> <FileName>C:/log/mylog.log</FileName> <FilePattern>C:/log/size-based-logs/%d{yyyy-MM-dd-hh}-%i.log.zip</FilePattern> <PatternLayout> <Pattern>%d{yyyy-MMM-dd HH:mm:ss a} [%t] %-5level %logger{36} - %msg%n</Pattern> </PatternLayout> <Policies> <SizeBasedTriggeringPolicy size="10 KB"/> </Policies> <DefaultRolloverStrategy max="5" /> </RollingFile>You can specify the size of file in bytes, with the suffix KB, MB or GB.Here is the complete
log4j2.xmlfile forrolling files based on the specified size in
<SizeBasedTriggeringPolicy/>element.log4j2.xml
<?xml version="1.0" encoding="UTF-8"?> <Configuration> <Appenders> <!-- Console Appender --> <Console name="Console" target="SYSTEM_OUT"> <PatternLayout pattern="%d{yyyy-MMM-dd HH:mm:ss a} [%t] %-5level %logger{36} - %msg%n" /> </Console> <!-- Rolling File Appender --> <RollingFile name="RollingFile"> <FileName>C:/log/mylog.log</FileName> <FilePattern>C:/log/size-based-logs/%d{yyyy-MM-dd-hh}-%i.log.zip</FilePattern> <PatternLayout> <Pattern>%d{yyyy-MMM-dd HH:mm:ss a} [%t] %-5level %logger{36} - %msg%n</Pattern> </PatternLayout> <Policies> <SizeBasedTriggeringPolicy size="10 KB" /> </Policies> <DefaultRolloverStrategy max="5" /> </RollingFile> </Appenders> <Loggers> <Logger name="com.boraji.tutorial.log4j2" level="debug" additivity="false"> <AppenderRef ref="RollingFile" /> <AppenderRef ref="Console" /> </Logger> <Root level="trace"> <AppenderRef ref="Console" /> </Root> </Loggers> </Configuration>The following is a simple java program to test the above log4j2.xml configuration.
package com.boraji.tutorial.log4j2; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; public class MainApp { private static final Logger logger = LogManager.getLogger(MainApp.class); public static void main(String[] args) { for (int i = 0; i < 50000; i++) { logger.info("Rolling file appender example..."); } } }
Rollingbased on cron expression
You can use the CronTriggeringPolicy torollover the log file based on the specified cron expression as follows.<RollingFile name="RollingFile"> <FileName>C:/log/mylog.log</FileName> <FilePattern>C:/log/cron-based-logs/%d{yyyy-MM-dd-hh-mm}-%i.log.zip</FilePattern> <PatternLayout> <Pattern>%d{yyyy-MMM-dd HH:mm:ss a} [%t] %-5level %logger{36} - %msg%n</Pattern> </PatternLayout> <Policies> <CronTriggeringPolicy schedule="0 0/2 * 1/1 * ? *" /> </Policies> <DefaultRolloverStrategy max="5" /> </RollingFile>The following is the complete
log4j2.xmlfile,which trigger rollover after every 2nd minutes as specified in
scheduleattribute of
<CronTriggeringPolicy/>elementby cron expression.log4j2.xml
<?xml version="1.0" encoding="UTF-8"?><Configuration><Appenders><!-- Console Appender --><Console name="Console" target="SYSTEM_OUT"><PatternLayout pattern="%d{yyyy-MMM-dd HH:mm:ss a} [%t] %-5level %logger{36} - %msg%n" /></Console><!-- Rolling File Appender --><RollingFile name="RollingFile"> <FileName>C:/log/mylog.log</FileName> <FilePattern>C:/log/cron-based-logs/%d{yyyy-MM-dd-hh-mm}-%i.log.zip</FilePattern> <PatternLayout> <Pattern>%d{yyyy-MMM-dd HH:mm:ss a} [%t] %-5level %logger{36} - %msg%n</Pattern> </PatternLayout> <Policies> <CronTriggeringPolicy schedule="0 0/2 * 1/1 * ? *" /> </Policies> <DefaultRolloverStrategy max="5" /> </RollingFile></Appenders><Loggers><Logger name="com.boraji.tutorial.log4j2" level="debug" additivity="false"><AppenderRef ref="RollingFile" /><AppenderRef ref="Console" /></Logger><Root level="trace"><AppenderRef ref="Console" /></Root></Loggers></Configuration>The following is a simple java program to test the above log4j2.xml configuration.
package com.boraji.tutorial.log4j2; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; public class MainApp { private static final Logger logger = LogManager.getLogger(MainApp.class); public static void main(String[] args) { for (int i = 0; i < 1000; i++) { logger.info("Rolling file appender example..."); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } }
https://www.boraji.com/log4j-2-rollingfileappender-example
相关文章推荐
- 使用log4j的DailyRollingFileAppender时只有一个日志文件
- Log4j的扩展-支持设置最大日志数量MaxFileSize的DailyRollingFileAppender
- 扩展log4j系列[一]DailyRollingFileAppender的按分钟生成日志修改为按n分钟生成日志
- 扩展log4j系列[一]DailyRollingFileAppender的按分钟生成日志修改为按n分钟生成日志
- Log4j RollingFileAppender 备份日志文件名 修改
- 使用log4j扩展包的RollingFileAppender生成带时间戳的日志文件
- log4j配置日志文件log4j.appender.R.File相对路径方法
- log4cpp的RollingFileAppender异常情况下日志文件不拆分的一种可能解决方法
- log4j使用DailyRollingFileAppender
- log4j使用DailyRollingFileAppender
- 扩展DailyRollingFileAppender使其支持限定日志文件的数量
- 13.log4j.properties例子:org.apache.log4j.RollingFileAppender
- log4j使用DailyRollingFileAppender
- Log4J学习【二十二】常用的Appender之DailyRollingFileAppender
- Log4j 重写 RollingFileAppender 实现按日期生成文件夹文件名
- log4j使用DailyRollingFileAppender
- 发现一个log4j的日志输出器(DatedFileAppender)
- 扩展log4j系列[一]为DailyRollingFileAppender加上maxBackupIndex属性 - 玮哥也是哥 - ITeye技术网站
- log4j配置日志文件log4j.appender.R.File相对路径方法
- logback系列之七:继承RollingFileAppender,保存自定义文件名的日志