Log4j 2
来源:互联网 发布:mac原生游戏列表 编辑:程序博客网 时间:2024/06/14 16:41
echnologies used: Java SE 1.8 | Log4j 2.8.2 | Maven 3.3.9 | Jackson API 2.8.7 | Eclipse Neon.3
RollingFileAppender 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 RollingFileAppender
to backup and compress the old log files based on -
- Date and Time
- Size of log file
- Cron expression
Jar dependencies
Edit pom.xml
file and add log4j2 and Jackson API 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 to rollover the log file based on the date and time pattern used in <FilePattern/>
element as 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.
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.zipRoll the log files hourlyIf 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.zipRoll the log files dailyIf interval=1, rollovers will occur every day.
E.g. - 2017-07-26.log.zip, 2017-07-27.log.zip etc..
The following is the complete example of log4j2.xml
file for 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 define a 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 to rollover 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.xml
file for rolling 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..."); } }}
Rolling based on cron expression
You can use the CronTriggeringPolicy to rollover 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.xml
file, which trigger rollover after every 2nd minutes as specified in schedule
attribute of<CronTriggeringPolicy/>
element by 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 2
- log4j 2
- Log4j Study(2)
- log4j基本原理(2)
- log4j应用配置2
- log4j配置2
- log4j使用(2)
- 配置Log4J (2)
- Log4j用法-2
- Log4j 2架构
- Log4j 2 配置
- apache log4j(2)-Download
- log4j 2 配置
- Log4j学习(2)----实例
- Log4j 2 XML 配置
- Log4j 2使用教程
- Log4j 2 配置 简单
- Log4j 2使用教程
- c++强制类型转换:dynamic_cast、const_cast 、static_cast、reinterpret_cast
- 结合ZXing实现类似微信扫二维码放大摄像头
- wheelView自定义android日期时间选择器
- Aria2使用前端UI显示下载任务
- Verilog算法入门
- Log4j 2
- rem布局js设置
- cglib
- Android 应用国际化
- 日期转化
- CentOS7下安装配置python3
- andengine消除重力的影响
- postgresql查看用户连接以及杀死连接的会话
- 获取图片的原始宽高