我想让用户指定他们想要写入 Windows 事件查看器的级别,但是我还将日志写入控制台和输出文件。有没有办法将所有日志写入文件和控制台,只将更高级别的日志写入事件查看器,而不必有两个单独的 Logger 变量。
private static PatternLayout myLayout
= new PatternLayout("%-4r [%t] %-5p %c %x - %m%n");
// Read from INI file
private static boolean enableWindowsEventLogging;
static final Logger log4jLogger = Logger.getLogger(Main.class);
private static NTEventLogAppender eventLogAppender = new NTEventLogAppender();
private static RollingFileAppender rollingFileAppender;
private static ConsoleAppender consoleAppender = new ConsoleAppender(myLayout);
....
if(enableWindowsEventLogging)
{
switch(Integer.valueOf(thisProperties.getProperty("warningLevel")))
{
case 1: log4jLogger.setLevel(Level.ALL); break;
case 2: log4jLogger.setLevel(Level.FATAL); break;
case 3: log4jLogger.setLevel(Level.ERROR); break;
case 4: log4jLogger.setLevel(Level.WARN); break;
case 5: log4jLogger.setLevel(Level.INFO); break;
case 6: log4jLogger.setLevel(Level.DEBUG); break;
};
// Event viewer logging
log4jLogger.addAppender(eventLogAppender);
}else
log4jLogger.setLevel(Level.OFF);
....
rollingFileAppender = new RollingFileAppender(myLayout,"\\logs\\log4joutput.log", true);
我正在使用 Apache log4j 1.2.12