From 2a4454b524ccb3b1ca5661f5ce605960d0a9089a Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Fri, 12 Jul 2013 00:42:50 -0700 Subject: [PATCH] Add simple Java Util Logging formatter Add simple formatter used with the default logging.properties to improve JUL output. --- .../logging/JavaLoggingFormatter.java | 64 +++++++++++++++++++ .../bootstrap/logging/logging.properties | 4 +- 2 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 spring-bootstrap/src/main/java/org/springframework/bootstrap/logging/JavaLoggingFormatter.java diff --git a/spring-bootstrap/src/main/java/org/springframework/bootstrap/logging/JavaLoggingFormatter.java b/spring-bootstrap/src/main/java/org/springframework/bootstrap/logging/JavaLoggingFormatter.java new file mode 100644 index 00000000000..9f50a169e65 --- /dev/null +++ b/spring-bootstrap/src/main/java/org/springframework/bootstrap/logging/JavaLoggingFormatter.java @@ -0,0 +1,64 @@ +/* + * Copyright 2012-2013 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.bootstrap.logging; + +import java.io.PrintWriter; +import java.io.StringWriter; +import java.util.Date; +import java.util.logging.Formatter; +import java.util.logging.LogRecord; + +/** + * Simple 'Java Logging' {@link Formatter}. + * + * @author Phillip Webb + */ +public class JavaLoggingFormatter 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 final Date date = new Date(); + + @Override + public synchronized String format(LogRecord record) { + this.date.setTime(record.getMillis()); + String source = record.getLoggerName(); + String message = formatMessage(record); + String throwable = getThrowable(record); + String thread = getThreadName(); + return String.format(FORMAT, this.date, source, record.getLoggerName(), record + .getLevel().getLocalizedName(), message, throwable, thread); + } + + private String getThrowable(LogRecord record) { + if (record.getThrown() == null) { + return ""; + } + StringWriter stringWriter = new StringWriter(); + PrintWriter printWriter = new PrintWriter(stringWriter); + printWriter.println(); + record.getThrown().printStackTrace(printWriter); + printWriter.close(); + return stringWriter.toString(); + } + + private String getThreadName() { + String name = Thread.currentThread().getName(); + return (name == null ? "" : name); + } + +} diff --git a/spring-bootstrap/src/main/resources/org/springframework/bootstrap/logging/logging.properties b/spring-bootstrap/src/main/resources/org/springframework/bootstrap/logging/logging.properties index d76fd91353b..675773b1344 100644 --- a/spring-bootstrap/src/main/resources/org/springframework/bootstrap/logging/logging.properties +++ b/spring-bootstrap/src/main/resources/org/springframework/bootstrap/logging/logging.properties @@ -3,7 +3,9 @@ handlers = java.util.logging.FileHandler, java.util.logging.ConsoleHandler # File Logging java.util.logging.FileHandler.pattern = %t/service.log -java.util.logging.FileHandler.formatter = java.util.logging.SimpleFormatter +java.util.logging.FileHandler.formatter = org.springframework.bootstrap.logging.JavaLoggingFormatter java.util.logging.FileHandler.level = INFO java.util.logging.FileHandler.limit = 10485760 java.util.logging.FileHandler.count = 10 + +java.util.logging.ConsoleHandler.formatter = org.springframework.bootstrap.logging.JavaLoggingFormatter