1

我是 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 方法。请帮忙。提前致谢。

4

1 回答 1

0

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

以下示例适用于 log4j 1.2。
debug消息将进入文件debug.loginfo消息将进入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:我知道这是一个老问题,但我希望它对某人有所帮助。

于 2018-05-30T13:12:12.643 回答