In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
The name of the log4j custom build file
When we use Log4j's RollingFileAppender loop to generate files, the name of the generated file is a little disgusting, for example, if the file name is app.log, then the generated file name is app.log.1,app.log.2,.....
So how do you change the rules for the name of the generated file? Here is a simple example:
Log4j.propertieslog4j.logger.major= INFO, majorMsglog4j.additivity.logError = falselog4j.appender.majorMsg=com.zws.log.MyRollingFileAppenderlog4j.appender.majorMsg.File=$ {catalina.home} / logs/itc/majorMsg.loglog4j.appender.majorMsg.layout=org.apache.log4j.PatternLayoutlog4j.appender.majorMsg.layout.ConversionPattern=%d {yyyy-MM-dd HH\: mm\: ss} |% p |% C |% M |% L |% m%nlog4j.appender.majorMsg.MaxFileSize=1KBlog4j.appender.majorMsg.MaxBackupIndex=10MyRollingFileAppender.javapackage 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) {/ / Delete the file with the largest sequence number (the earliest file) file = new File (genFileName (fileName, maxBackupIndex)); if (file.exists ()) renameSucceeded = file.delete (); / / all file names are numbered plus 1 for (int I = maxBackupIndex-1; I > = 1 & renameSucceeded) ) {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 ();} above is all the content of the article "what is the naming convention for custom log4j log files?" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.