Unify log format of default JDK logger with other systems

pull/9/head
Dave Syer 12 years ago
parent a97f4f670c
commit 217a6a4cd1

@ -29,10 +29,14 @@ import java.util.logging.LogRecord;
*/
public class SimpleFormatter extends Formatter {
private static final String FORMAT = "%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS.%1$tL - [%7$s] %4$s - %3$s : %5$s%6$s%n";
private static final String DEFAULT_FORMAT = "[%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS.%1$tL] - %8$s %4$s [%7$s] --- %3$s: %5$s%6$s%n";
private static String FORMAT = setOrUseDefault("LOG_FORMAT", DEFAULT_FORMAT);
private final Date date = new Date();
private static String PID = setOrUseDefault("PID", "????");
@Override
public synchronized String format(LogRecord record) {
this.date.setTime(record.getMillis());
@ -41,7 +45,7 @@ public class SimpleFormatter extends Formatter {
String throwable = getThrowable(record);
String thread = getThreadName();
return String.format(FORMAT, this.date, source, record.getLoggerName(), record
.getLevel().getLocalizedName(), message, throwable, thread);
.getLevel().getLocalizedName(), message, throwable, thread, PID);
}
private String getThrowable(LogRecord record) {
@ -61,4 +65,18 @@ public class SimpleFormatter extends Formatter {
return (name == null ? "" : name);
}
private static String setOrUseDefault(String key, String defaultValue) {
String value = null;
try {
value = System.getenv(key);
}
catch (Exception e) {
// ignore
}
if (value == null) {
value = defaultValue;
}
return System.getProperty(key, value);
}
}

@ -25,7 +25,7 @@ import org.apache.commons.logging.impl.Jdk14Logger;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.bootstrap.logging.java.JavaLoggingSystem;
import org.springframework.util.ClassUtils;
import static org.junit.Assert.assertTrue;
@ -53,7 +53,6 @@ public class JavaLoggerSystemTests {
this.savedOutput = System.err;
this.output = new ByteArrayOutputStream();
System.setErr(new PrintStream(this.output));
}
@After
@ -62,6 +61,7 @@ public class JavaLoggerSystemTests {
System.clearProperty("LOG_PATH");
System.clearProperty("PID");
System.setErr(this.savedOutput);
System.err.println(this.output);
}
private String getOutput() {
@ -69,12 +69,32 @@ public class JavaLoggerSystemTests {
}
@Test
public void testDefaultConfigLocation() throws Exception {
this.loggingSystem.initialize("classpath:logging-nondefault.properties");
public void testCustomFormatter() throws Exception {
this.logger.info("Hello world");
String output = getOutput().trim();
assertTrue("Wrong output:\n" + output, output.contains("Hello world"));
assertTrue("Wrong output:\n" + output, output.contains("INFO"));
assertTrue("Wrong output:\n" + output, output.contains("???? INFO ["));
}
@Test
public void testSystemPropertyInitializesFormat() throws Exception {
System.setProperty("PID", "1234");
this.loggingSystem.initialize("classpath:"
+ ClassUtils.addResourcePathToPackagePath(getClass(),
"logging.properties"));
this.logger.info("Hello world");
this.logger.info("Hello world");
String output = getOutput().trim();
assertTrue("Wrong output:\n" + output, output.contains("Hello world"));
assertTrue("Wrong output:\n" + output, output.contains("1234 INFO ["));
}
@Test
public void testNonDefaultConfigLocation() throws Exception {
this.loggingSystem.initialize("classpath:logging-nondefault.properties");
this.logger.info("Hello world");
String output = getOutput().trim();
assertTrue("Wrong output:\n" + output, output.contains("INFO: Hello"));
}
@Test(expected = IllegalStateException.class)

@ -1,9 +1,4 @@
handlers= java.util.logging.ConsoleHandler
.level= INFO
java.util.logging.FileHandler.pattern = %h/java%u.log
java.util.logging.FileHandler.limit = 50000
java.util.logging.FileHandler.count = 1
java.util.logging.FileHandler.formatter = java.util.logging.XMLFormatter
java.util.logging.ConsoleHandler.level = INFO
java.util.logging.ConsoleHandler.formatter = org.springframework.bootstrap.logging.java.SimpleFormatter
java.util.logging.SimpleFormatter.format = %4$s: %5$s [%1$tc]%n

Loading…
Cancel
Save