Log4j多个自定义appender没有被调用

时间:2016-11-09 10:57:22

标签: java log4j

请检查下面的配置,其中我的唯一一个appender(错误)被调用,不知道为什么Info没有被调用。 InfoFileAppender和ErrorFileAppender都是自定义appender。 我还需要其他任何配置吗?

log4j.rootLogger=InfoFileAppender,ErrorFileAppender,stdout

log4j.appender.InfoFileAppender =com.configuration.InfoFileAppender
log4j.appender.InfoFileAppender.layout.ConversionPattern = %d{ABSOLUTE} %5p %c - %m%n
log4j.appender.InfoFileAppender.Threshold=INFO
log4j.appender.InfoFileAppender.layout = org.apache.log4j.PatternLayout
log4j.appender.InfoFileAppender.File =Info.log

log4j.appender.ErrorFileAppender =com.configuration.ErrorFileAppender
log4j.appender.ErrorFileAppender.layout.ConversionPattern = %d{ABSOLUTE} %5p %c - %m%n
log4j.appender.ErrorFileAppender.Threshold=ERROR
log4j.appender.ErrorFileAppender.layout = org.apache.log4j.PatternLayout
log4j.appender.ErrorFileAppender.File =Error.log

# Redirect log messages to console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c - %m%n

InfoFileAppender.java

public class InfoFileAppender extends FileAppender {

String pattern = "ddMMMyyyy";
SimpleDateFormat dtFormat = new SimpleDateFormat(pattern);

public InfoFileAppender() {
}

public InfoFileAppender(Layout layout, String filename, boolean append,
        boolean bufferedIO, int bufferSize) throws IOException {
    super(layout, filename, append, bufferedIO, bufferSize);
}

public InfoFileAppender(Layout layout, String filename, boolean append)
        throws IOException {
    super(layout, filename, append);
}

public InfoFileAppender(Layout layout, String filename) throws IOException {
    super(layout, filename);
}

public void activateOptions() {
    if (fileName != null) {
        try {
            fileName = getNewLogFileName();
            setFile(fileName, fileAppend, bufferedIO, bufferSize);
        } catch (Exception e) {
            errorHandler.error("Error while activating log options", e,
                    ErrorCode.FILE_OPEN_FAILURE);
        }
    }
}

private String getNewLogFileName() {
    if (fileName != null) {
        final String DOT = ".";
        final File logFile = new File(fileName);
        final String fileName = logFile.getName();
        String newFileName = "";
        Calendar calendar = Calendar.getInstance();
        final int dotIndex = fileName.indexOf(DOT);
        if (dotIndex != -1) {
            newFileName = fileName.substring(0, dotIndex) + "_"
                    + dtFormat.format(calendar.getTime()) + "_"
                    + calendar.get(Calendar.HOUR) + "_"
                    + calendar.get(Calendar.MINUTE) + "_"
                    + calendar.get(Calendar.SECOND);
            newFileName = newFileName + "." + fileName.substring(5);

        } else {
            // the file name has no extension. So, just append the timestamp
            // at the end.
            newFileName = fileName + "_" + System.currentTimeMillis();
        }
        return newFileName;
    }
    return null;
}

}

ErrorFileAppender.java

public class ErrorFileAppender extends FileAppender {

String pattern = "ddMMMyyyy";
SimpleDateFormat dtFormat = new SimpleDateFormat(pattern);

public ErrorFileAppender() {
}

public ErrorFileAppender(Layout layout, String filename, boolean append,
        boolean bufferedIO, int bufferSize) throws IOException {
    super(layout, filename, append, bufferedIO, bufferSize);
}

public ErrorFileAppender(Layout layout, String filename, boolean append)
        throws IOException {
    super(layout, filename, append);
}

public ErrorFileAppender(Layout layout, String filename) throws IOException {
    super(layout, filename);
}

public void activateOptions() {
    if (fileName != null) {
        try {
            fileName = getNewLogFileName();
            setFile(fileName, fileAppend, bufferedIO, bufferSize);
        } catch (Exception e) {
            errorHandler.error("Error while activating log options", e,
                    ErrorCode.FILE_OPEN_FAILURE);
        }
    }
}

private String getNewLogFileName() {
    if (fileName != null) {
        final String DOT = ".";
        final File logFile = new File(fileName);
        final String fileName = logFile.getName();
        String newFileName = "";
        Calendar calendar = Calendar.getInstance();
        final int dotIndex = fileName.indexOf(DOT);
        if (dotIndex != -1) {
            newFileName = fileName.substring(0, dotIndex) + "_"
                    + dtFormat.format(calendar.getTime()) + "_"
                    + calendar.get(Calendar.HOUR) + "_"
                    + calendar.get(Calendar.MINUTE) + "_"
                    + calendar.get(Calendar.SECOND);
            newFileName = newFileName + "." + fileName.substring(6);
        } else {
            // the file name has no extension. So, just append the timestamp
            // at the end.
            newFileName = fileName + "_" + System.currentTimeMillis();
        }
        return newFileName;
    }
    return null;
}

}

提前致谢..

1 个答案:

答案 0 :(得分:0)

在rootLogger中添加了调试... log4j.rootLogger = Debug,InfoFileAppender,ErrorFileAppender,stdout