小编给大家分享一下自定义log4j日志文件命名规则是什么,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!
项目中的日志需要采用一致的命名规范和文件规范,命名规则为:项目模块标识_index_日期时间_日志级别.log,且每个级别日志文件放在单独的文件夹,且每个文件夹下日志的数量不得超过10个,当数量超过限制时,删除相对较旧的日志,保留较新的日志。
但是发现log4j并不能满足此要求,于是
代码如下:
package com.dear.simpler.dbrpc.util.log; import java.io.File; import java.io.IOException; import java.io.InterruptedIOException; import java.text.SimpleDateFormat; import java.util.Arrays; import java.util.Comparator; import java.util.Date; import java.util.concurrent.atomic.AtomicInteger; import java.util.regex.Matcher; import java.util.regex.Pattern; import org.apache.log4j.RollingFileAppender; import org.apache.log4j.helpers.CountingQuietWriter; import org.apache.log4j.helpers.LogLog; import org.apache.log4j.spi.LoggingEvent; /**; * * @author lixiang * 自定义log文件的命名规则 */ public class MyLogFileAppender extends RollingFileAppender { private long nextRollover = 0; private static AtomicInteger logIndex = new AtomicInteger(0); //index public void rollOver() { File file = null; if (qw != null) { long size = ((CountingQuietWriter) qw).getCount(); LogLog.debug("rolling over count=" + size); // if operation fails, do not roll again until // maxFileSize more bytes are written nextRollover = size + maxFileSize; } LogLog.debug("maxBackupIndex=" + maxBackupIndex); if (maxBackupIndex > 0) { file = new File(getRollingFileName(fileName, logIndex.incrementAndGet())); if (fileExisted(file)){ file = new File(getRollingFileName(fileName, logIndex.incrementAndGet())); } deleteOldFile(file.getParentFile(), maxBackupIndex); this.closeFile(); } try { this.setFile(getRollingFileName(fileName, logIndex.get()), 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 getRollingFileName(String fileName, int index) { //使用正则表达式替代index Pattern p = Pattern.compile("_\\d+\\_"); Matcher m=p.matcher(fileName); String str = m.replaceFirst(String.format("_%d_", index)); SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss"); //日期 String dateString = format.format(new Date(System.currentTimeMillis())); str = str.replaceAll("\\d{14}", dateString); return str; } public synchronized void setFile(String fileName, boolean append, //修改文件名 boolean bufferedIO, int bufferSize) throws IOException { SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss"); //日期 String dateString = format.format(new Date(System.currentTimeMillis())); String temp = String.format(fileName , dateString); //文件名 super.setFile(temp, append, bufferedIO, bufferSize); if(append) { File f = new File(temp); ((CountingQuietWriter)this.qw).setCount(f.length()); } } private boolean fileExisted(File file){ boolean res = false; String[] fts = file.getName().split("_"); File parentFile = file.getParentFile(); for(File f : parentFile.listFiles()){ String[] fns = f.getName().split("_"); if(fns[0].equals(fts[0]) && fns[1].equals(fts[1])){ res = true; break; } } return res; } private void deleteOldFile(File dir , int maxInt){ if(getFileNum(dir) >= maxBackupIndex ){ File[] files = orderByDate(dir); for (int i = 0; i <= files.length - maxBackupIndex; i++) { File f = files[i]; f.delete(); } } } private int getFileNum(File file){ return file.list().length; } //将文件按日期排序 public File[] orderByDate(File dir) { File[] fs = dir.listFiles(); Arrays.sort(fs,new Comparator< File>(){ @Override public int compare(File f1, File f2) { long diff = f1.lastModified() - f2.lastModified(); if (diff > 0) return 1; else if (diff == 0) return 0; else return -1; } @Override public boolean equals(Object obj) { return true; } }); return fs; } @Override protected void subAppend(LoggingEvent event) { super.subAppend(event); if (fileName != null && qw != null) { long size = ((CountingQuietWriter) qw).getCount(); if (size >= maxFileSize && size >= nextRollover) { rollOver(); } } } }
### set log levels ### log4j.rootLogger = out,E,I #log4j.logger.com.dear.simpler.dbrpc.util.log.TestUtil=out,D log4j.appender.D = com.dear.simpler.dbrpc.util.log.MyLogFileAppender log4j.appender.D.File = ../../logs/db_logs/debug/DB_0_%s_debug.log log4j.appender.D.Append = true log4j.appender.D.MaxFileSize=1024MB log4j.appender.D.MaxBackupIndex=10 log4j.appender.D.Threshold = DEBUG log4j.appender.D.layout = com.dear.simpler.dbrpc.util.log.ExPatternLayout log4j.appender.D.layout.ConversionPattern = [%d{yyyy/MM/dd HH:mm:ss,SSS}][%T:%t][%p][%F:%L:%M][%m]%n log4j.appender.E = com.dear.simpler.dbrpc.util.log.MyLogFileAppender log4j.appender.E.File = ../../logs/db_logs/error/DB_0_%s_error.log log4j.appender.E.Append = true log4j.appender.E.MaxFileSize=10MB log4j.appender.E.MaxBackupIndex=10 log4j.appender.E.Threshold = ERROR log4j.appender.E.layout = com.dear.simpler.dbrpc.util.log.ExPatternLayout log4j.appender.E.layout.ConversionPattern = [%d{yyyy/MM/dd HH:mm:ss,SSS}][%T:%t][%p][%F:%L:%M][%m]%n log4j.appender.I = com.dear.simpler.dbrpc.util.log.MyLogFileAppender log4j.appender.I.File = ../../logs/db_logs/info/DB_0_%s_info.log log4j.appender.I.Append = true log4j.appender.I.MaxFileSize=10MB log4j.appender.I.MaxBackupIndex=10 log4j.appender.I.Threshold = INFO log4j.appender.I.layout = com.dear.simpler.dbrpc.util.log.ExPatternLayout log4j.appender.I.layout.ConversionPattern = [%d{yyyy/MM/dd HH:mm:ss,SSS}][%T:%t][%p][%F:%L:%M][%m]%n
我们在使用Log4j的RollingFileAppender循环生成文件的时候,生成的文件的名称有点儿恶心,例如,文件名称为app.log,那么生成的文件名依次为app.log.1,app.log.2,....
那么如何去改变生成文件的名称的规则呢?下面是一个简单示例:
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
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(); } } } }
以上是“自定义log4j日志文件命名规则是什么”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。