我正在使用log4j2来配置我的代码中的日志记录。 我正在使用基于时间的触发器策略。 但是删除对我不起作用,我收到以下错误
016-08-05 14:44:22,635 main ERROR appender RollingFile has no parameter that matches element Delete
2016-08-05 14:44:22.686 [WARN ] [main] Class1 - new cycle
2016-08-05 14:44:22.691 [DEBUG] [main] Class1 - Hello this is a debug message
2016-08-05 14:44:22.691 [INFO ] [main] Class1 - Hello this is an info message
2016-08-05 14:44:22.692 [FATAL] [main] Class1 - Beaware This is a Fatal message
2016-08-05 14:44:22.692 [ERROR] [main] Class1 - This is an error message
2016-08-05 14:44:22.692 [INFO ] [main] Class2 - In constructor of class2
2016-08-05 14:44:22.692 [INFO ] [main] Class1 - Repeating cycle
2016-08-05 14:44:22.692 [DEBUG] [main] Class1 - Hello this is a debug message
2016-08-05 14:44:22.692 [INFO ] [main] Class1 - Hello this is an info message
2016-08-05 14:44:22.693 [FATAL] [main] Class1 - Beaware This is a Fatal message
2016-08-05 14:44:22.693 [ERROR] [main] Class1 - This is an error message
以下是我的log4j2配置文件
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn">
<Properties>
<Property name="log-path">D:/logs</Property>
</Properties>
<Appenders>
<Console name="console-log" target="SYSTEM_OUT">
<PatternLayout
pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%-5level] [%t] %c{1} - %msg%n" />
</Console>
<RollingFile name="file-log" fileName="${log-path}/custom-log.log"
filePattern="${log-path}/customelog-%d{yyyy-MM-dd HH-mm}.log">
<PatternLayout>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%-5level] [%t] %c{1} - %msg%n
</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true" />
</Policies>
<Delete basePath="${log-path}" maxDepth="2">
<IfFileName glob="customelog-*.log" />
<IfLastModified age="1m" />
</Delete>
</RollingFile>
</Appenders>
<Loggers>
<Logger name="test.Class1" level="debug" additivity="false">
<appender-ref ref="file-log" level="debug" />
<appender-ref ref="console-log" level="debug" />
</Logger>
<Root level="all" additivity="false">
<AppenderRef ref="console-log" />
</Root>
</Loggers>
</Configuration>
我的测试代码在
之下class1.java:
package test;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class Class1 {
public static Logger logger = LogManager.getLogger(Class1.class);
public static void main(String[] args){
logger.warn("new cycle");
logger.debug("Hello this is a debug message");
logger.info("Hello this is an info message");
logger.fatal("Beaware This is a Fatal message");
logger.error("This is an error message");
Class2 obj = new Class2();
logger.info("Repeating cycle");
logger.debug("Hello this is a debug message");
logger.info("Hello this is an info message");
logger.fatal("Beaware This is a Fatal message");
logger.error("This is an error message");
}
}
Class2.java
package test;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class Class2 {
public static Logger logger = LogManager.getLogger(Class2.class);
public Class2(){
Class2.logger.info("In constructor of class2");
}
}
问题是上面提到的错误以及如果年龄超过1分钟文件没有被删除的事实
您能否告诉我我正在制作的错误,因为我的删除功能无效?
我在Log4j2 - Configure RolloverStrategy to delete old log files
使用了以下示例谢谢,
维卡斯
答案 0 :(得分:2)
您的Delete
标记应该包含在DefaultRolloverStrategy
e.g。
<RollingFile name="RollingFile" fileName="D:/app.log"
filePattern="D:/app-%d{yyyy-MM-dd-HH-mm-ss}.log">
<PatternLayout>
<Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="60" modulate="true"/>
<!--<SizeBasedTriggeringPolicy size="250 MB"/>-->
</Policies>
<DefaultRolloverStrategy>
<Delete basePath="D:" maxDepth="1">
<IfFileName glob="app-*.log" />
<IfLastModified age="3m" />
</Delete>
</DefaultRolloverStrategy>
</RollingFile>