问题
目前,默认条目如下所示:
Oct 12, 2008 9:45:18 AM myClassInfoHere
INFO: MyLogMessageHere
我怎么做到这一点?
Oct 12, 2008 9:45:18 AM myClassInfoHere - INFO: MyLogMessageHere
澄清我正在使用java.util.logging
#1 热门回答(71 赞)
从Java 7,java.util.logging.SimpleFormatter开始,支持从系统属性获取其format,因此将这样的内容添加到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'
#2 热门回答(67 赞)
1)-Djava.util.logging.SimpleFormatter.format
Java 7支持具有java.util.Formatter
格式字符串语法的属性。
-Djava.util.logging.SimpleFormatter.format=...
Seehere。
我最喜欢的是:
-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
2)将它放到IDE中
IDE通常允许你为项目设置系统属性。例如。在NetBeans中,而不是在某处添加-D ... = ...,在动作对话框中添加属性,形式为java.util.logging.SimpleFormatter.format=%1$tY-%1$tm-...
-没有任何引号。 IDE应该弄清楚。
3)把它放到Maven - Surefire
为了你的方便,以下是如何将它放到Surefire:
<!-- Surefire -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.17</version>
<configuration>
<systemPropertyVariables>
<!-- Set JUL Formatting -->
<java.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</java.util.logging.SimpleFormatter.format>
</systemPropertyVariables>
</configuration>
</plugin>
4)手工制作
我有一个图书馆withfewjava.util.logging
相关课程。其中,它是SingleLineFormatter
。可下载的jarhere。
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();
}
}
#3 热门回答(38 赞)
与Tervor类似,但我喜欢在运行时更改属性。
请注意,这需要在创建第一个SimpleFormatter之前设置 - 正如在注释中所写的那样。
System.setProperty("java.util.logging.SimpleFormatter.format",
"%1$tF %1$tT %4$s %2$s %5$s%6$s%n");