私は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 メソッドも追加する必要がありますか。親切に助けてください。前もって感謝します。