`
redsky008
  • 浏览: 345949 次
  • 性别: Icon_minigender_1
  • 来自: 成都正在项目
社区版块
存档分类
最新评论

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

 
阅读更多
今天终于遇到了log4j配置成DailyRollingFileAppender后,没有正确生成文件,或者没有重命名log文件

在网上寻找了一遍,绝大部分都是复制了了就不管了,大致原文该链接http://www.blogjava.net/DreamAngel/archive/2011/11/10/363400.html中的代码有明显问题,比如日志文件过大怎么办,然而复制了原log文件后,那原来的log文件需要清空却没有做。


现修正如下,但修正的结果却是错误的(你可以不看修正的结果了):

真正占用文件的根源可以从这里说起,windows平台下你打开一个记事本,你是可以删除的该记事本的,但是如果用独占的方式打开一个记事本,因为加了锁,而另一个进程(对头这里是进程,而非线程)却要去重命名它,怎么可能做到呢,所以最好是两个独立的进程就做独立进程的事情了,具体解决思路当然是进程既然是独立的,log4j.properties中一定要修改日志记录的路径为不同的进程所独有。


	/**
	 * 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 = copy(file, target);
		if (result) {
			FileWriter fw =  new FileWriter(file);
			fw.write("");
			fw.flush();
			fw.close();
			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, true, this.bufferedIO, this.bufferSize);
		} catch (IOException e) {
			errorHandler.error("setFile(" + fileName + ", true) call failed.");
		}
		scheduledFilename = datedFilename;
	}

	// 最大的流为60MB,当文件的容量大于60MB的时候便分开流
	final int MAX_BYTE = 60000000;
	/**
	 * 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 {
			long streamTotal = 0; // 接受流的容量
			int streamNum = 0; // 流需要分开的数量
			int leave = 0; // 文件剩下的字符数
			byte[] inOutb;// byte数组接受文件的数据
			
			FileInputStream in = new FileInputStream(src);
			FileOutputStream out = new FileOutputStream(dst);
			
			// 通过available方法取得流的最大字符数
			streamTotal = in.available();
			// 取得流文件需要分开的数量
			streamNum = (int)Math.floor(streamTotal/MAX_BYTE);
			// 分开文件之后,剩余的数量
			leave = (int)(streamTotal % MAX_BYTE);
			// 文件的容量大于60MB时进入循环
			if(streamNum>0){
				for (int i = 0; i < streamNum; i++) {
					inOutb = new byte[MAX_BYTE];
					// 读入流,保存在byte数组
					in.read(inOutb, 0, MAX_BYTE);
					out.write(inOutb);
					out.flush();
				}
			}
			// 剩余的流数据
			inOutb = new byte[leave];
			in.read(inOutb, 0, leave);
			out.write(inOutb);
			out.flush();
			
			in.close();
			out.close();
			return true;
		} catch (FileNotFoundException e) {
			LogLog.error("源文件不存在,或者目标文件无法被识别.");
			return false;
		} catch (IOException e) {
			LogLog.error("文件读写错误.");
			return false;
		}
	}
分享到:
评论
2 楼 redsky008 2013-06-09  
独立的程序,独立的路径。

我已经转行了,以后这个很少关注的了。
1 楼 梅花簪 2013-05-02  
没明白,怎么改配置,为不同的进程所有?

相关推荐

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

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

    log4j:ERROR Failed to rename

    解决log4j:ERROR Failed to rename, win系统占用文件无法改名字的问题

    解决log4j:ERROR Failed to rename代码包

    log4j:ERROR Failed to rename错误解决办法 http://www.blogjava.net/DreamAngel/archive/2011/11/10/363400.html

    kafka 解决log4j:ERROR Failed to rename错误解决办法错误的jar包

    log4j:ERROR Failed to rename

    log4j修改源码后解决log4j:ERROR Failed to rename错误解决办法错误的jar包

    log4j:ERROR Failed to rename错误解决办法,修改源码里的DailyRollingFileAppender类,用此jar包就不会再出现ERROR Failed to rename的错误了

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

    包括修改后的[color=darkred]org.apache.log4j.DailyRollingFileAppender[/color]类的源代码和已编译好的文件. 请用DailyRollingFileAppender.class替换log4j-1.2.15.jar包里相应的类. 博文链接:...

    log4j-1.2.16.jar

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

    log4j-1.2.15.jar

    针对log4j:ERROR Failed to rename的问题,修改源代码中的DailyRollingFileAppender.java文件,将rename改为copy。 该log4j-1.2.15.jar,就是修改后的jar包。 已经过测试,可以正常生成日志文件。

    端口查看工具

    o CurrPorts now displays a simple error message if it fails to close one or more TCP connections. * Version 2.01: o The 'Remote Address' and 'Local Address' columns are now sorted by the IP ...

    GreenLuma-2.5.5 - Steam006

    Failed to contact key server is removed Play free games online such as Synergy Play games with custom protection such as Race Replaced the "sponsor" picture Log file is created, please post it if you ...

    最全的oracle常用命令大全.txt

    Connected to an idle instance. SQL&gt; startup^C SQL&gt; startup ORACLE instance started. 2、在双机环境下 要想启动或关闭ORACLE系统必须首先切换到root用户,如下 su - root a、启动ORACLE系统 hareg -y ...

    8-07-14_MegaCLI for linux_windows

    SCGCQ00322234 EnhancementRequest Utilities to show logical block size [AF512e, 512n, 4Kn,..] SCGCQ00322257 EnhancementRequest Implemented support for 4K sector drives in CLI. SCGCQ00322317 ...

    PHP基础教程 是一个比较有价值的PHP新手教程!

    因此你必须有办法将两者区别开来。以下就是你可以采用的几种方法。你可以选用其中一种你最适应的并且就这样坚持这种方法! 从HTML中分离 以下是可以使用的方法: &lt;script language="php"&gt; . . . 语句 与Perl...

Global site tag (gtag.js) - Google Analytics