我有这个Log4J配置,可以在日志文件中生成消息:
log = Logger.getLogger(LogMessages.class.getName());
BasicConfigurator.configure(); // Basic configuration for Log4J 1.x
ConsoleAppender console = new ConsoleAppender(); //create appender
//configure the appender
String PATTERN = "%d{DATE} [%p|%c|%C{1}] %m%n";
console.setLayout(new PatternLayout(PATTERN));
console.setThreshold(Level.FATAL);
console.activateOptions();
//add appender to any Logger (here is root)
Logger.getRootLogger().addAppender(console);
DailyRollingFileAppender fa = new DailyRollingFileAppender();
fa.setName("FileLogger");
fa.setFile("log" + File.separator + "messages.log");
fa.setDatePattern("'.'yyyy-MM-dd");
fa.setLayout(new PatternLayout("%d %-5p [%c{1}] %m%n"));
fa.setThreshold(Level.DEBUG);
fa.setAppend(true);
fa.activateOptions();
//add appender to any Logger (here is root)
Logger.getRootLogger().addAppender(fa); //repeat with all other desired appenders
这是生成的输出:
2013-12-26 10:19:27,501 WARN [LogMessages] test_message
我想生成这样的信息:
2013-12-26 10:19:27 WARN test_message
如何从邮件中删除[LogMessages]?
答案 0 :(得分:1)
对于文件记录,请将setLayout
调用更改为:
fa.setLayout(new PatternLayout("%d{dd/MM/yyyy HH\:mm\:ss} %-5p %m%n"));
对于控制台日志记录,请使用相同的模式:
String PATTERN = "%d{dd/MM/yyyy HH\:mm\:ss} %-5p %m%n";