gpt4 book ai didi

java - 在 RollingFileAppender 的情况下在 log4j 中滚动

转载 作者:行者123 更新时间:2023-12-01 04:36:09 26 4
gpt4 key购买 nike

我是 log4j 的新手。在我们的应用程序中,我们需要以这样的方式实现日志记录,即所有不同级别都将转到不同的文件。所以我通过谷歌搜索扩展了 RollingFileAppender,如下所示。

import java.io.File;
import java.io.IOException;

import org.apache.log4j.FileAppender;
import org.apache.log4j.Layout;
import org.apache.log4j.MDC;
import org.apache.log4j.RollingFileAppender;
import org.apache.log4j.spi.ErrorCode;
import org.apache.log4j.spi.LoggingEvent;


public class LogLevelFilterFileAppender extends RollingFileAppender {

private final static String DOT = ".";
private final static String HIPHEN = "-";
private static final String ORIG_LOG_FILE_NAME = "OrginalLogFileName";

public LogLevelFilterFileAppender() {

}

/*public LogLevelFilterFileAppender(Layout layout, String fileName,
boolean append, boolean bufferedIO, int bufferSize)
throws IOException {
super(layout, fileName, append, bufferedIO, bufferSize);
}
*/
public LogLevelFilterFileAppender(Layout layout, String fileName,
boolean append) throws IOException {
super(layout, fileName, append);
}

public LogLevelFilterFileAppender(Layout layout, String fileName)
throws IOException {
super(layout, fileName);
}

@Override
public void activateOptions() {
MDC.put(ORIG_LOG_FILE_NAME, fileName);
super.activateOptions();
}

@Override
public void append(LoggingEvent event) {
try {
setFile(appendLevelToFileName((String) MDC.get(ORIG_LOG_FILE_NAME),
event.getLevel().toString()), fileAppend, bufferedIO,
bufferSize);
} catch (IOException ie) {
errorHandler
.error(
"Error occured while setting file for the log level "
+ event.getLevel(), ie,
ErrorCode.FILE_OPEN_FAILURE);
}
super.append(event);
}

private String appendLevelToFileName(String oldLogFileName, String level) {
if (oldLogFileName != null) {
final File logFile = new File(oldLogFileName);
String newFileName = "";
final String fn = logFile.getName();
final int dotIndex = fn.indexOf(DOT);
if (dotIndex != -1) {
// the file name has an extension. so, insert the level
// between the file name and the extension
/*newFileName = fn.substring(0, dotIndex) + HIPHEN + level + DOT
+ fn.substring(dotIndex + 1);*/
newFileName= level+".log";
} else {
// the file name has no extension. So, just append the level
// at the end.
newFileName = fn + HIPHEN + level;
}
return logFile.getParent() + File.separator + newFileName;
}
return null;
}


}

我的log4j.properties是这样的:

logDir=/opt/ems/release/logs

log4j.logger.A= SECURITY#SecurityLevel,A1,A31
log4j.additivity.A=false
log4j.appender.A1 = LogLevelFilterFileAppender
log4j.appender.A1.MaxBackupIndex=20
log4j.appender.A1.append=true
log4j.appender.A1.layout.ConversionPattern = %d [%-15.15t] %-5p %-30.30c{1} - %m%n
log4j.appender.A1.layout = org.apache.log4j.PatternLayout
log4j.appender.A1.File = /home/krishna/Desktop/logs1.log
log4j.appender.A1.maxFileSize=5MB
log4j.category=SecurityLevel

log4j.logger.B= PRF#PRFLevel,A2,A31
log4j.additivity.B=false
log4j.appender.A2 = LogLevelFilterFileAppender
log4j.appender.A2.MaxBackupIndex=20
log4j.appender.A2.append=true
log4j.appender.A2.layout.ConversionPattern =%d [%-15.15t] %-5p %-30.30c{1} - %m%n
log4j.appender.A2.layout = org.apache.log4j.PatternLayout
log4j.appender.A2.File = /home/krishna/Desktop/logs1.log
log4j.appender.A2.maxFileSize=5MB
log4j.category=PRFLevel

log4j.logger.C= VRB#VRBLevel,A3,A31
log4j.additivity.C=false
log4j.appender.A3 = LogLevelFilterFileAppender
log4j.appender.A3.MaxBackupIndex=20
log4j.appender.A3.append=true
log4j.appender.A3.layout.ConversionPattern =%d [%-15.15t] %-5p %-30.30c{1} - %m%n
log4j.appender.A3.layout = org.apache.log4j.PatternLayout
log4j.appender.A3.File = /home/krishna/Desktop/logs1.log
log4j.appender.A3.maxFileSize=5MB
log4j.category=VRBLevel


log4j.appender.A31 = org.apache.log4j.ConsoleAppender
log4j.appender.A31.layout.ConversionPattern =%d [%-15.15t] %-5p %-30.30c{1} - %m%n
log4j.appender.A31.layout = org.apache.log4j.PatternLayout

log4j.logger.D = INFO,A4,A31
log4j.additivity.D=false
log4j.appender.A4 = LogLevelFilterFileAppender
log4j.appender.A4.MaxBackupIndex=20
log4j.appender.A4.append=true
log4j.appender.A4.layout.ConversionPattern =%d [%-15.15t] %-5p %-30.30c{1} - %m%n
log4j.appender.A4.layout = org.apache.log4j.PatternLayout
log4j.appender.A4.File = /home/krishna/Desktop/logs1.log
log4j.appender.A4.maxFileSize=5MB
log4j.category=INFO

log4j.logger.E= WARN, A5,A31
log4j.additivity.E=false
log4j.appender.A5 = LogLevelFilterFileAppender
log4j.appender.A5.MaxBackupIndex=20
log4j.appender.A5.append=true
log4j.appender.A5.layout.ConversionPattern =%d [%-15.15t] %-5p %-30.30c{1} - %m%n
log4j.appender.A5.layout = org.apache.log4j.PatternLayout
log4j.appender.A5.File = /home/krishna/Desktop/logs1.log
log4j.appender.A5.maxFileSize=5MB
log4j.category= WARN

log4j.logger.F= ERROR,A6,A31
log4j.additivity.F=false
log4j.appender.A6 = LogLevelFilterFileAppender
log4j.appender.A6.MaxBackupIndex=20
log4j.appender.A6.append=true
log4j.appender.A6.layout.ConversionPattern =%d [%-15.15t] %-5p %-30.30c{1} - %m%n
log4j.appender.A6.layout = org.apache.log4j.PatternLayout
log4j.appender.A6.File = /home/krishna/Desktop/logs1.log
log4j.appender.A6.maxFileSize=10KB
log4j.category= ERROR

log4j.logger.G= DEBUG,A7,A31
log4j.additivity.F=false
log4j.appender.A7 = LogLevelFilterFileAppender
log4j.appender.A7.MaxBackupIndex=20
log4j.appender.A7.append=true
log4j.appender.A7.layout.ConversionPattern =%d [%-15.15t] %-5p %-30.30c{1} - %m%n
log4j.appender.A7.layout = org.apache.log4j.PatternLayout
log4j.appender.A7.File = /home/krishna/Desktop/logs1.log
log4j.appender.A7.maxFileSize=5MB
log4j.category= DEBUG

现在的问题是滚动没有发生,我不知道该怎么做。我知道我们需要重写翻转方法,但是我们需要在其中做什么。我还需要添加 subappend 方法吗?请帮忙。 提前致谢。

最佳答案

您可以为每个级别使用不同的附加程序,而不是扩展RollingFileAdapter,这可以通过配置来实现。

以下示例适用于 log4j 1.2。
debug 消息将转到文件 debug.log,而 info 消息将转到 info.log

 <appender name="debugAppender" class="org.apache.log4j.rolling.RollingFileAppender">
<param name="Append" value="true"/>
<rollingPolicy class="org.apache.log4j.rolling.TimeBasedRollingPolicy">
<param name="FileNamePattern" value="./Archive/debug.%d{yyyy-MM-dd}.log.zip" />
<param name="ActiveFileName" value="./debug.log" />
</rollingPolicy>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %C - %m%n"/>
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMin" value="TRACE"/>
<param name="LevelMax" value="DEBUG"/>
<param name="AcceptOnMatch" value="true"/>
</filter>
</appender>

<appender name="infoLogAppender" class="org.apache.log4j.rolling.RollingFileAppender">
<param name="Append" value="true"/>
<rollingPolicy class="org.apache.log4j.rolling.TimeBasedRollingPolicy">
<param name="FileNamePattern" value="./Archive/info.%d{yyyy-MM-dd}.log.zip" />
<param name="ActiveFileName" value="./info.log" />
</rollingPolicy>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %C (line:%L) - %m%n"/>
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMin" value="INFO"/>
<param name="LevelMax" value="INFO"/>
<param name="AcceptOnMatch" value="true"/>
</filter>
</appender>

PS:我知道这是一个老问题,但我希望它对某人有所帮助。

关于java - 在 RollingFileAppender 的情况下在 log4j 中滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17313237/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com