目前,默认条目如下所示:
Oct 12, 2008 9:45:18 AM myClassInfoHere INFO: MyLogMessageHere
我怎么做到这一点?
Oct 12, 2008 9:45:18 AM myClassInfoHere - INFO: MyLogMessageHere
澄清我正在使用java.util.logging
从Java 7开始,java.util.logging.SimpleFormatter支持从系统属性获取其格式,因此将这样的内容添加到JVM命令行将导致它在一行上打印:
-Djava.util.logging.SimpleFormatter.format='%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS %4$s %2$s %5$s%6$s%n'
或者,您也可以将此添加到您的logger.properties
:
java.util.logging.SimpleFormatter.format='%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS %4$s %2$s %5$s%6$s%n'
-Djava.util.logging.SimpleFormatter.format
Java 7支持具有java.util.Formatter
格式字符串语法的属性.
-Djava.util.logging.SimpleFormatter.format=...
看到这里.
我最喜欢的是:
-Djava.util.logging.SimpleFormatter.format=%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS %4$-6s %2$s %5$s%6$s%n
这使输出像:
2014-09-02 16:44:57 SEVERE org.jboss.windup.util.ZipUtil unzip: Failed to load: foo.zip
IDE通常允许您为项目设置系统属性.例如,在NetBeans中,而不是在某处添加-D ... = ...,在操作对话框中添加属性,其形式为java.util.logging.SimpleFormatter.format=%1$tY-%1$tm-...
- 没有任何引号.IDE应该弄明白.
为了您的方便,以下是如何将它放到Surefire:
org.apache.maven.plugins maven-surefire-plugin 2.17 %1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS %4$-6s %2$s %5$s%6$s%n
我有一个几乎没有java.util.logging
相关课程的图书馆.其中,它是SingleLineFormatter
.可下载的jar 在这里.
public class SingleLineFormatter extends Formatter { Date dat = new Date(); private final static String format = "{0,date} {0,time}"; private MessageFormat formatter; private Object args[] = new Object[1]; // Line separator string. This is the value of the line.separator // property at the moment that the SimpleFormatter was created. //private String lineSeparator = (String) java.security.AccessController.doPrivileged( // new sun.security.action.GetPropertyAction("line.separator")); private String lineSeparator = "\n"; /** * Format the given LogRecord. * @param record the log record to be formatted. * @return a formatted log record */ public synchronized String format(LogRecord record) { StringBuilder sb = new StringBuilder(); // Minimize memory allocations here. dat.setTime(record.getMillis()); args[0] = dat; // Date and time StringBuffer text = new StringBuffer(); if (formatter == null) { formatter = new MessageFormat(format); } formatter.format(args, text, null); sb.append(text); sb.append(" "); // Class name if (record.getSourceClassName() != null) { sb.append(record.getSourceClassName()); } else { sb.append(record.getLoggerName()); } // Method name if (record.getSourceMethodName() != null) { sb.append(" "); sb.append(record.getSourceMethodName()); } sb.append(" - "); // lineSeparator String message = formatMessage(record); // Level sb.append(record.getLevel().getLocalizedName()); sb.append(": "); // Indent - the more serious, the more indented. //sb.append( String.format("% ""s") ); int iOffset = (1000 - record.getLevel().intValue()) / 100; for( int i = 0; i < iOffset; i++ ){ sb.append(" "); } sb.append(message); sb.append(lineSeparator); if (record.getThrown() != null) { try { StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); record.getThrown().printStackTrace(pw); pw.close(); sb.append(sw.toString()); } catch (Exception ex) { } } return sb.toString(); } }
与Tervor类似,但我喜欢在运行时更改属性.
请注意,这需要在创建第一个SimpleFormatter之前设置 - 正如在注释中所写的那样.
System.setProperty("java.util.logging.SimpleFormatter.format", "%1$tF %1$tT %4$s %2$s %5$s%6$s%n");
像Obediah Stane所说,有必要创建自己的format
方法.但我会改变一些事情:
创建一个直接派生自Formatter
而不是派生自的子类SimpleFormatter
.该SimpleFormatter
有什么可再补充.
小心创建一个新Date
对象!你应该确保代表日期LogRecord
.Date
使用默认构造函数创建new 时,它将表示Formatter
进程的日期和时间LogRecord
,而不是LogRecord
创建的日期.
下面的类可以被用作格式化器中一个Handler
,这反过来又可以加入到Logger
.请注意,它忽略了可用的所有类和方法信息LogRecord
.
import java.io.PrintWriter; import java.io.StringWriter; import java.util.Date; import java.util.logging.Formatter; import java.util.logging.LogRecord; public final class LogFormatter extends Formatter { private static final String LINE_SEPARATOR = System.getProperty("line.separator"); @Override public String format(LogRecord record) { StringBuilder sb = new StringBuilder(); sb.append(new Date(record.getMillis())) .append(" ") .append(record.getLevel().getLocalizedName()) .append(": ") .append(formatMessage(record)) .append(LINE_SEPARATOR); if (record.getThrown() != null) { try { StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); record.getThrown().printStackTrace(pw); pw.close(); sb.append(sw.toString()); } catch (Exception ex) { // ignore } } return sb.toString(); } }
这就是我正在使用的.
public class VerySimpleFormatter extends Formatter {
private static final String PATTERN = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX";
@Override
public String format(final LogRecord record) {
return String.format(
"%1$s %2$-7s %3$s\n",
new SimpleDateFormat(PATTERN).format(
new Date(record.getMillis())),
record.getLevel().getName(), formatMessage(record));
}
}
你会得到类似......
2016-08-19T17:43:14.295+09:00 INFO Hey~ 2016-08-19T17:43:16.068+09:00 SEVERE Seriously? 2016-08-19T17:43:16.068+09:00 WARNING I'm warning you!!!
每个截图,在Eclipse中选择"运行方式",然后选择"运行配置...",并使用双引号而不是引号添加Trevor Robinson的答案.如果你错过双引号,你会得到"无法找到或加载主类"的错误.