我正在以编程方式使用log4j。它似乎在控制台消息之外工作。 System.out消息不会出现。 logger.info/error/etc都出现在文件中。你不能记录System.out信息吗?我做错了什么?
Logger logger = Logger.getRootLogger();
logger = Logger.getLogger("com.mycodebase");
ConsoleAppender consoleAppender = new ConsoleAppender();
PatternLayout consolePatternLayout = new PatternLayout();
consolePatternLayout.setConversionPattern("%d{dd MMM yyyy} %d{HH:mm:ss}: %5p %c{1}:%L - %m%n");
consoleAppender.setLayout(consolePatternLayout);
consoleAppender.setName("Iridium Suite Console appender");
consoleAppender.setThreshold(Level.INFO);
consoleAppender.activateOptions();
//Create Iridium Suite log file appender
isAppender = new RollingFileAppender();
isAppender.setFile(logFileName);
PatternLayout isPatternLayout = new PatternLayout();
isPatternLayout.setConversionPattern("%d{dd MMM yyyy} %d{HH:mm:ss}: %5p %c{1}:%L - %m%n");
isAppender.setLayout(isPatternLayout);
isAppender.setThreshold(Level.INFO);
isAppender.setName("log appender");
isAppender.setAppend(true);
isAppender.setMaxBackupIndex(0);
isAppender.setMaxFileSize("2MB");
isAppender.activateOptions();
Logger.getRootLogger().addAppender(consoleAppender);
Logger.getRootLogger().addAppender(isAppender);
答案 0 :(得分:1)
ConsoleAppender用于将日志消息定向到stdout,但不是相反。人们在这里提出了很好的解决方案: log4j redirect stdout to DailyRollingFileAppender