From 252b42770d5a2c38626e4c2eeedd61cc113cba6f Mon Sep 17 00:00:00 2001 From: Simon Kitching Date: Sun, 15 Oct 2006 03:11:19 +0000 Subject: [PATCH] Fix thread-safety bug (SimpleDateFormat.format is not thread-safe). Thanks to Martin Wilson of bright-interactive for the bug report. git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/logging/trunk@464108 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/commons/logging/impl/SimpleLog.java | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/java/org/apache/commons/logging/impl/SimpleLog.java b/src/java/org/apache/commons/logging/impl/SimpleLog.java index 509c600..f8e85c0 100644 --- a/src/java/org/apache/commons/logging/impl/SimpleLog.java +++ b/src/java/org/apache/commons/logging/impl/SimpleLog.java @@ -100,7 +100,15 @@ public class SimpleLog implements Log, Serializable { static protected boolean showDateTime = false; /** The date and time format to use in the log message */ static protected String dateTimeFormat = DEFAULT_DATE_TIME_FORMAT; - /** Used to format times */ + + /** + * Used to format times. + *

+ * Any code that accesses this object should first obtain a lock on it, + * ie use synchronized(dateFormatter); this requirement was introduced + * in 1.1.1 to fix an existing thread safety bug (SimpleDateFormat.format + * is not thread-safe). + */ static protected DateFormat dateFormatter = null; // ---------------------------------------------------- Log Level Constants @@ -179,7 +187,6 @@ public class SimpleLog implements Log, Serializable { } } - // ------------------------------------------------------------- Attributes /** The name of this simple log instance */ @@ -281,7 +288,12 @@ public class SimpleLog implements Log, Serializable { // Append date-time if so configured if(showDateTime) { - buf.append(dateFormatter.format(new Date())); + Date now = new Date(); + String dateText; + synchronized(dateFormatter) { + dateText = dateFormatter.format(now); + } + buf.append(dateText); buf.append(" "); }