`
wxw850227
  • 浏览: 71225 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

Tomcat6下Log4j的log4j:ERROR Failed to rename错误解决办法

    博客分类:
  • java
阅读更多
Tomcat6下配置log4j

log4j配置到tomcat6可以看这里, tomcat6下配置log4j

引起log4j:ERROR Failed to rename的原因

引起log4j:ERROR Failed to rename的原因大致为

引用
因為他是採用renameTo檔名的方式,所以,有可能是因為有process咬住檔案(之前遇到的問題),或是對檔案讀寫的權限不足


关于使用renameTo需要注意的事项, 请看 请慎用java的File#renameTo(File)方法 , File.renameTo方法在linux下的问题

引起log4j:ERROR Failed to rename的条件

通常都是在配置文件采用Java代码
log4j.appender.A1=org.apache.log4j.DailyRollingFileAppender 

log4j.appender.A1=org.apache.log4j.DailyRollingFileAppender或者
Java代码
log4j.appender.A1=org.apache.log4j.RollingFileAppender 

log4j.appender.A1=org.apache.log4j.RollingFileAppender的情况下遇到"异常"提示.

和异常相关的log4j源代码

查看log4j的源代码,和log4j:ERROR Failed to rename异常相关的代码为
Java代码
/** 
     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);  
   }  


/**
     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);
   }
}
重点在这里
Java代码
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+"].");  
    } 

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+"].");
    }

解决办法

解决办法为修改log4j的源代码, 修改
Java代码
boolean result = file.renameTo(target); 

boolean result = file.renameTo(target);

Java代码
boolean result = copy(file, target); 

boolean result = copy(file, target);
然后再添加copy()方法.
Java代码
/** 
     * 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;  
        }  
    } 

/**
* 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;
}
}
此方法借鉴自 File Copy in Java 和 Java File Copy

后面的话
修改后的代码可以正常运行在tomcat6下, log文件替换正常. 没有高负载和集群环境下测试. 如有问题,还请大家积极反馈. 如果你有更好的办法请告诉我.  

附件 : 包括修改后的org.apache.log4j.DailyRollingFileAppender类的源代码和已编译好的文件.
请用DailyRollingFileAppender.class替换log4j-1.2.15.jar包里相应的类.
分享到:
评论

相关推荐

    log4j:ERROR Failed to rename错误解决办法

    log4j:ERROR Failed to rename错误; 网上查找了下原因,大概意思是日志文件始终被占有,所以当log4j对日志文件进行rename时,就发生了Failed to rename错误 。要修改log4j的源码,附件是我修改好后的,方便大家...

    [AB PLC例程源码][MMS_044666]Translation N-A.zip

    AB PLC例程代码项目案例 【备注】 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用!有问题请及时沟通交流。 2、适用人群:计算机相关专业(如计科、信息安全、数据科学与大数据技术、人工智能、通信、物联网、自动化、电子信息等)在校学生、专业老师或者企业员工下载使用。 3、用途:项目具有较高的学习借鉴价值,不仅适用于小白学习入门进阶。也可作为毕设项目、课程设计、大作业、初期项目立项演示等。 4、如果基础还行,或热爱钻研,亦可在此项目代码基础上进行修改添加,实现其他不同功能。 欢迎下载!欢迎交流学习!不清楚的可以私信问我!

    kolesar_3cd_01_0716.pdf

    kolesar_3cd_01_0716

    latchman_01_0108.pdf

    latchman_01_0108

    matlab程序代码项目案例:matlab程序代码项目案例MPC在美国高速公路场景中移动的车辆上的实现.zip

    matlab程序代码项目案例 【备注】 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用!有问题请及时沟通交流。 2、适用人群:计算机相关专业(如计科、信息安全、数据科学与大数据技术、人工智能、通信、物联网、自动化、电子信息等)在校学生、专业老师或者企业员工下载使用。 3、用途:项目具有较高的学习借鉴价值,不仅适用于小白学习入门进阶。也可作为毕设项目、课程设计、大作业、初期项目立项演示等。 4、如果基础还行,或热爱钻研,亦可在此项目代码基础上进行修改添加,实现其他不同功能。 欢迎下载!欢迎交流学习!不清楚的可以私信问我!

    pimpinella_3cd_01_0716.pdf

    pimpinella_3cd_01_0716

    petrilla_01_0308.pdf

    petrilla_01_0308

    [AB PLC例程源码][MMS_041452]Speed Controls in Plastic Extrusion.zip

    AB PLC例程代码项目案例 【备注】 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用!有问题请及时沟通交流。 2、适用人群:计算机相关专业(如计科、信息安全、数据科学与大数据技术、人工智能、通信、物联网、自动化、电子信息等)在校学生、专业老师或者企业员工下载使用。 3、用途:项目具有较高的学习借鉴价值,不仅适用于小白学习入门进阶。也可作为毕设项目、课程设计、大作业、初期项目立项演示等。 4、如果基础还行,或热爱钻研,亦可在此项目代码基础上进行修改添加,实现其他不同功能。 欢迎下载!欢迎交流学习!不清楚的可以私信问我!

    强化学习驱动下DeepSeek技术创新及其对AI发展的影响

    内容概要:本文档由张卓老师讲解,重点探讨DeepSeek的技术革新及强化学习对未来AI发展的重要性。文章回顾了AI的历史与发展阶段,详细解析Transformer架构在AI上半场所起到的作用,深入介绍了MoE混合专家以及MLA低秩注意机制等技术特点如何帮助DeepSeek在AI中场建立优势,并探讨了当前强化学习的挑战和边界。文档不仅提及AlphaGo和小游戏等成功案例来说明强化学习的强大力量,还提出了关于未来人工通用智能(AGI)的展望,特别是如何利用强化学习提升现有LLMs的能力和性能。 适用人群:本资料适宜对深度学习感兴趣的研究人员、开发者以及想要深入了解人工智能最新进展的专业人士。 使用场景及目标:通过了解最新的AI技术和前沿概念,在实际工作中能够运用更先进的工具和技术解决问题。同时为那些寻求职业转型或者学术深造的人提供了宝贵的参考。 其他说明:文中提到了许多具体的例子和技术细节,如DeepSeek的技术特色、RL的理论背景等等,有助于加深读者对于现代AI系统的理解和认识。

    有师傅小程序开源版v2.4.14+前端.zip

    有师傅小程序开源版v2.4.14 新增报价短信奉告 优化部分细节

    [AB PLC例程源码][MMS_047333]Motor Sequence Starter with timers to start.zip

    AB PLC例程代码项目案例 【备注】 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用!有问题请及时沟通交流。 2、适用人群:计算机相关专业(如计科、信息安全、数据科学与大数据技术、人工智能、通信、物联网、自动化、电子信息等)在校学生、专业老师或者企业员工下载使用。 3、用途:项目具有较高的学习借鉴价值,不仅适用于小白学习入门进阶。也可作为毕设项目、课程设计、大作业、初期项目立项演示等。 4、如果基础还行,或热爱钻研,亦可在此项目代码基础上进行修改添加,实现其他不同功能。 欢迎下载!欢迎交流学习!不清楚的可以私信问我!

    商城二级三级分销系统(小程序+后台含源码).zip

    商城二级三级分销系统(小程序+后台含源码).zip

    li_3ck_01b_0918.pdf

    li_3ck_01b_0918

    nicholl_3cd_01_0516.pdf

    nicholl_3cd_01_0516

    1995-2022年 网络媒体关注度、报刊媒体关注度与媒体监督相关数据.zip

    媒体关注度是一个衡量公众对某个事件、话题或个体关注程度的重要指标。它主要反映了新闻媒体、社交媒体、博客等对于某一事件、话题或个体的报道和讨论程度。 媒体监督的J-F系数(Janis-Fadner系数)是一种用于测量媒体关注度的指标,特别是用于评估媒体对企业、事件或话题的监督力度。J-F系数基于媒体报道的正面和负面内容来计算,从而为公众、研究者或企业提供一个量化工具,以了解媒体对其关注的方向和强度。 本数据含原始数据、参考文献、代码do文件、最终结果。参考文献中JF系数计算公式。 指标 代码、年份、标题出现该公司的新闻总数、内容出现该公司的新闻总数、正面新闻数全部、中性新闻数全部、负面新闻数全部、正面新闻数原创、中性新闻数原创、负面新闻数原创,媒体监督JF系数。

    [AB PLC例程源码][MMS_040315]Double INC and Double DEC of INT datatype.zip

    AB PLC例程代码项目案例 【备注】 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用!有问题请及时沟通交流。 2、适用人群:计算机相关专业(如计科、信息安全、数据科学与大数据技术、人工智能、通信、物联网、自动化、电子信息等)在校学生、专业老师或者企业员工下载使用。 3、用途:项目具有较高的学习借鉴价值,不仅适用于小白学习入门进阶。也可作为毕设项目、课程设计、大作业、初期项目立项演示等。 4、如果基础还行,或热爱钻研,亦可在此项目代码基础上进行修改添加,实现其他不同功能。 欢迎下载!欢迎交流学习!不清楚的可以私信问我!

    [AB PLC例程源码][MMS_047773]Convert Feet to Millimeters.zip

    AB PLC例程代码项目案例 【备注】 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用!有问题请及时沟通交流。 2、适用人群:计算机相关专业(如计科、信息安全、数据科学与大数据技术、人工智能、通信、物联网、自动化、电子信息等)在校学生、专业老师或者企业员工下载使用。 3、用途:项目具有较高的学习借鉴价值,不仅适用于小白学习入门进阶。也可作为毕设项目、课程设计、大作业、初期项目立项演示等。 4、如果基础还行,或热爱钻研,亦可在此项目代码基础上进行修改添加,实现其他不同功能。 欢迎下载!欢迎交流学习!不清楚的可以私信问我!

    [AB PLC例程源码][MMS_042349]How to read-write data to-from a PLC using OPC in Visual Basic 6.zip

    AB PLC例程代码项目案例 【备注】 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用!有问题请及时沟通交流。 2、适用人群:计算机相关专业(如计科、信息安全、数据科学与大数据技术、人工智能、通信、物联网、自动化、电子信息等)在校学生、专业老师或者企业员工下载使用。 3、用途:项目具有较高的学习借鉴价值,不仅适用于小白学习入门进阶。也可作为毕设项目、课程设计、大作业、初期项目立项演示等。 4、如果基础还行,或热爱钻研,亦可在此项目代码基础上进行修改添加,实现其他不同功能。 欢迎下载!欢迎交流学习!不清楚的可以私信问我!

    matlab程序代码项目案例:matlab程序代码项目案例论文代码 多篇RMPC 鲁棒模型预测控制Paper-code-implementation.zip

    matlab程序代码项目案例 【备注】 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用!有问题请及时沟通交流。 2、适用人群:计算机相关专业(如计科、信息安全、数据科学与大数据技术、人工智能、通信、物联网、自动化、电子信息等)在校学生、专业老师或者企业员工下载使用。 3、用途:项目具有较高的学习借鉴价值,不仅适用于小白学习入门进阶。也可作为毕设项目、课程设计、大作业、初期项目立项演示等。 4、如果基础还行,或热爱钻研,亦可在此项目代码基础上进行修改添加,实现其他不同功能。 欢迎下载!欢迎交流学习!不清楚的可以私信问我!

Global site tag (gtag.js) - Google Analytics