[b]Tomcat6下配置log4j[/b]
log4j配置到tomcat6可以看这里, [url=http://joedanny.iteye.com/blog/154521]tomcat6下配置log4j[/url]
[b]引起log4j:ERROR Failed to rename的原因[/b]
引起log4j:ERROR Failed to rename的原因大致为
[quote][color=red] [url=http://www.javaworld.com.tw/jute/post/print?bid=11&id=129070]因為他是採用renameTo檔名的方式,所以,有可能是因為有process咬住檔案(之前遇到的問題),或是對檔案讀寫的權限不足[/url][/color][/quote]
关于使用renameTo需要注意的事项, 请看 [url=http://xiaoych.iteye.com/blog/149328]请慎用java的File#renameTo(File)方法[/url] , [url=http://www.javayou.com/html/diary/showlog.vm?sid=2&log_id=1494]File.renameTo方法在linux下的问题[/url]
[b]引起log4j:ERROR Failed to rename的条件[/b]
通常都是在配置文件采用
[b]和异常相关的log4j源代码[/b]
查看log4j的源代码,和[color=red]log4j:ERROR Failed to rename[/color]异常相关的代码为
重点在这里
[b]解决办法[/b]
解决办法为修改log4j的源代码, 修改
为
然后再添加copy()方法.
此方法借鉴自 [url=http://www.java2s.com/Code/Java/File-Input-Output/FileCopyinJava.htm]File Copy in Java[/url] 和 [url=http://www.cnitblog.com/Justin/archive/2005/08/18/2184.aspx]Java File Copy[/url]
[b]后面的话[/b]
修改后的代码可以正常运行在tomcat6下, log文件替换正常. 没有高负载和集群环境下测试. 如有问题,还请大家积极反馈. [color=red]如果你有更好的办法请告诉我. [/color] :)
[b]附件[/b] : 包括修改后的[color=darkred]org.apache.log4j.DailyRollingFileAppender[/color]类的源代码和已编译好的文件.
请用DailyRollingFileAppender.class替换log4j-1.2.15.jar包里相应的类.
[b]参考资料 : [/b]
[url=http://logging.apache.org/]http://logging.apache.org/[/url]
[url=http://joedanny.iteye.com/blog/154521]tomcat6下配置log4j[/url]
[url=http://www.javaworld.com.tw/jute/post/print?bid=11&id=129070]Log4J DailyRollingFileAppender 問題[/url]
[url=http://xiaoych.iteye.com/blog/149328]请慎用java的File#renameTo(File)方法[/url]
[url=http://www.javayou.com/html/diary/showlog.vm?sid=2&log_id=1494]File.renameTo方法在linux下的问题[/url]
[url=http://www.java2s.com/Code/Java/File-Input-Output/FileCopyinJava.htm]File Copy in Java[/url]
[url=http://www.cnitblog.com/Justin/archive/2005/08/18/2184.aspx]Java File Copy[/url]
[url=http://log4j.jaxmao.org/log4j/docs/manual.html]log4j 简明手册[/url]
log4j配置到tomcat6可以看这里, [url=http://joedanny.iteye.com/blog/154521]tomcat6下配置log4j[/url]
[b]引起log4j:ERROR Failed to rename的原因[/b]
引起log4j:ERROR Failed to rename的原因大致为
[quote][color=red] [url=http://www.javaworld.com.tw/jute/post/print?bid=11&id=129070]因為他是採用renameTo檔名的方式,所以,有可能是因為有process咬住檔案(之前遇到的問題),或是對檔案讀寫的權限不足[/url][/color][/quote]
关于使用renameTo需要注意的事项, 请看 [url=http://xiaoych.iteye.com/blog/149328]请慎用java的File#renameTo(File)方法[/url] , [url=http://www.javayou.com/html/diary/showlog.vm?sid=2&log_id=1494]File.renameTo方法在linux下的问题[/url]
[b]引起log4j:ERROR Failed to rename的条件[/b]
通常都是在配置文件采用
log4j.appender.A1=org.apache.log4j.DailyRollingFileAppender
或者log4j.appender.A1=org.apache.log4j.RollingFileAppender
的情况下遇到"异常"提示.[b]和异常相关的log4j源代码[/b]
查看log4j的源代码,和[color=red]log4j:ERROR Failed to rename[/color]异常相关的代码为
/**
Rollover the current file to a new file.
*/
void rollOver() throws IOException {
/* Compute filename, but only if datePattern is specified */
if (datePattern == null) {
errorHandler.error("Missing DatePattern option in rollOver().");
return;
}
String datedFilename = fileName+sdf.format(now);
// It is too early to roll over because we are still within the
// bounds of the current interval. Rollover will occur once the
// next interval is reached.
if (scheduledFilename.equals(datedFilename)) {
return;
}
// close current file, and rename it to datedFilename
this.closeFile();
File target = new File(scheduledFilename);
if (target.exists()) {
target.delete();
}
File file = new File(fileName);
boolean result = file.renameTo(target);
if(result) {
LogLog.debug(fileName +" -> "+ scheduledFilename);
} else {
LogLog.error("Failed to rename ["+fileName+"] to ["+scheduledFilename+"].");
}
try {
// This will also close the file. This is OK since multiple
// close operations are safe.
this.setFile(fileName, false, this.bufferedIO, this.bufferSize);
}
catch(IOException e) {
errorHandler.error("setFile("+fileName+", false) call failed.");
}
scheduledFilename = datedFilename;
}
/**
* This method differentiates DailyRollingFileAppender from its
* super class.
*
* <p>Before actually logging, this method will check whether it is
* time to do a rollover. If it is, it will schedule the next
* rollover time and then rollover.
* */
protected void subAppend(LoggingEvent event) {
long n = System.currentTimeMillis();
if (n >= nextCheck) {
now.setTime(n);
nextCheck = rc.getNextCheckMillis(now);
try {
rollOver();
}
catch(IOException ioe) {
LogLog.error("rollOver() failed.", ioe);
}
}
super.subAppend(event);
}
}
重点在这里
File file = new File(fileName);
boolean result = file.renameTo(target);
if(result) {
LogLog.debug(fileName +" -> "+ scheduledFilename);
} else {
LogLog.error("Failed to rename ["+fileName+"] to ["+scheduledFilename+"].");
}
[b]解决办法[/b]
解决办法为修改log4j的源代码, 修改
boolean result = file.renameTo(target);
为
boolean result = copy(file, target);
然后再添加copy()方法.
/**
* Copies src file to dst file. If the dst file does not exist, it is
* created.8KB cache
*
* @param src
* @param dst
* @throws IOException
*/
boolean copy(File src, File dst) throws IOException {
try {
InputStream in = new FileInputStream(src);
OutputStream out = new FileOutputStream(dst);
// Transfer bytes from in to out
byte[] buf = new byte[8192];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.close();
return true;
} catch (FileNotFoundException e) {
LogLog.error("源文件不存在,或者目标文件无法被识别." );
return false;
} catch (IOException e) {
LogLog.error("文件读写错误.");
return false;
}
}
此方法借鉴自 [url=http://www.java2s.com/Code/Java/File-Input-Output/FileCopyinJava.htm]File Copy in Java[/url] 和 [url=http://www.cnitblog.com/Justin/archive/2005/08/18/2184.aspx]Java File Copy[/url]
[b]后面的话[/b]
修改后的代码可以正常运行在tomcat6下, log文件替换正常. 没有高负载和集群环境下测试. 如有问题,还请大家积极反馈. [color=red]如果你有更好的办法请告诉我. [/color] :)
[b]附件[/b] : 包括修改后的[color=darkred]org.apache.log4j.DailyRollingFileAppender[/color]类的源代码和已编译好的文件.
请用DailyRollingFileAppender.class替换log4j-1.2.15.jar包里相应的类.
[b]参考资料 : [/b]
[url=http://logging.apache.org/]http://logging.apache.org/[/url]
[url=http://joedanny.iteye.com/blog/154521]tomcat6下配置log4j[/url]
[url=http://www.javaworld.com.tw/jute/post/print?bid=11&id=129070]Log4J DailyRollingFileAppender 問題[/url]
[url=http://xiaoych.iteye.com/blog/149328]请慎用java的File#renameTo(File)方法[/url]
[url=http://www.javayou.com/html/diary/showlog.vm?sid=2&log_id=1494]File.renameTo方法在linux下的问题[/url]
[url=http://www.java2s.com/Code/Java/File-Input-Output/FileCopyinJava.htm]File Copy in Java[/url]
[url=http://www.cnitblog.com/Justin/archive/2005/08/18/2184.aspx]Java File Copy[/url]
[url=http://log4j.jaxmao.org/log4j/docs/manual.html]log4j 简明手册[/url]