我尝试配置log4j.xml,文件将按文件大小滚动,滚动文件的名称为:"C:/ temp/test/test_log4j-%d {yyyy-MM-dd-HH_mm_ss} .log"我跟着这个讨论:http://web.archiveorange.com/archive/v/NUYyjJipzkDOS3reRiMz
最后,只有当我添加时,它才对我有用:
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
方法:
public boolean isTriggeringEvent(Appender appender, LoggingEvent event,
String filename, long fileLength)
Run Code Online (Sandbox Code Playgroud)
这使它工作.
问题是,是否有更好的方法使其发挥作用?因为这种方法多次调用并且减慢了我的程序.
这是代码:
package com.mypack.rolling;
import org.apache.log4j.rolling.RollingPolicy;
import org.apache.log4j.rolling.RolloverDescription;
import org.apache.log4j.rolling.TimeBasedRollingPolicy;
/**
* Same as org.apache.log4j.rolling.TimeBasedRollingPolicy but acts only as
* RollingPolicy and NOT as TriggeringPolicy.
*
* This allows us to combine this class with a size-based triggering policy
* (decision to roll based on size, name of rolled files …Run Code Online (Sandbox Code Playgroud)