我们在使用Log4j的RollingFileAppender循环生成文件的时候,生成的文件的名称有点儿恶心,例如,文件名称为app.log,那么生成的文件名依次为app.log.1,app.log.2,....
那么如何去改变生成文件的名称的规则呢?下面是一个简单示例:
1,log4j.properties
log4j.logger.major= INFO, majorMsg log4j.additivity.logError = false log4j.appender.majorMsg=com.zws.log.MyRollingFileAppender log4j.appender.majorMsg.File=${catalina.home}/logs/itc/majorMsg.log log4j.appender.majorMsg.layout=org.apache.log4j.PatternLayout log4j.appender.majorMsg.layout.ConversionPattern=%d{yyyy-MM-dd HH\:mm\:ss}|%p|%C|%M|%L|%m%n log4j.appender.majorMsg.MaxFileSize=1KB log4j.appender.majorMsg.MaxBackupIndex=10
2,MyRollingFileAppender.java
package com.zws.log; import java.io.File; import java.io.IOException; import java.io.InterruptedIOException; import org.apache.log4j.Priority; import org.apache.log4j.RollingFileAppender; import org.apache.log4j.helpers.CountingQuietWriter; import org.apache.log4j.helpers.LogLog; import org.apache.log4j.spi.LoggingEvent; /** * * @author wensh.zhu * */ public class MyRollingFileAppender extends RollingFileAppender { private long nextRollover = 0; public void rollOver() { File target; File file; if (qw != null) { long size = ((CountingQuietWriter) qw).getCount(); nextRollover = size + maxFileSize; } LogLog.debug("maxBackupIndex=" + maxBackupIndex); boolean renameSucceeded = true; if (maxBackupIndex > 0) { //删除序号最大(最早的文件)的文件 file = new File(genFileName(fileName, maxBackupIndex)); if (file.exists()) renameSucceeded = file.delete(); //所有文件名序号加1 for (int i = maxBackupIndex - 1; i >= 1 && renameSucceeded; i--) { file = new File(genFileName(fileName, i)); if (file.exists()) { target = new File(genFileName(fileName, i + 1)); renameSucceeded = file.renameTo(target); } } if (renameSucceeded) { target = new File(genFileName(fileName, 1)); this.closeFile(); file = new File(fileName); renameSucceeded = file.renameTo(target); if (!renameSucceeded) { try { this.setFile(fileName, true, bufferedIO, bufferSize); } catch (IOException e) { if (e instanceof InterruptedIOException) { Thread.currentThread().interrupt(); } LogLog.error("setFile(" + fileName + ", true) call failed.", e); } } } } if (renameSucceeded) { try { this.setFile(fileName, false, bufferedIO, bufferSize); nextRollover = 0; } catch (IOException e) { if (e instanceof InterruptedIOException) { Thread.currentThread().interrupt(); } LogLog.error("setFile(" + fileName + ", false) call failed.", e); } } } private String genFileName(String name, int index) { String fileName = ""; if (index > 0) { String num = index < 10 ? "0" + index : String.valueOf(index); fileName = name.replace(".log", "") + "_" + num + ".log"; } else { fileName = name; } return fileName; } protected void subAppend(LoggingEvent event) { super.subAppend(event); if (fileName != null && qw != null) { long size = ((CountingQuietWriter) qw).getCount(); if (size >= maxFileSize && size >= nextRollover) { rollOver(); } } } }
以上示例将文件名的生成规则为:如果文件名为app.log,那么后续的文件为app_01.log,app_02.log.
本文出自 “埃文” 博客,请务必保留此出处http://wenshengzhu.blog.51cto.com/5151285/1712913
原文地址:http://wenshengzhu.blog.51cto.com/5151285/1712913