1
0

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
This commit is contained in:
Simon Kitching
2006-10-15 03:11:19 +00:00
parent c9d05cd4b6
commit 252b42770d

View File

@@ -100,7 +100,15 @@ public class SimpleLog implements Log, Serializable {
static protected boolean showDateTime = false; static protected boolean showDateTime = false;
/** The date and time format to use in the log message */ /** The date and time format to use in the log message */
static protected String dateTimeFormat = DEFAULT_DATE_TIME_FORMAT; static protected String dateTimeFormat = DEFAULT_DATE_TIME_FORMAT;
/** Used to format times */
/**
* Used to format times.
* <p>
* 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; static protected DateFormat dateFormatter = null;
// ---------------------------------------------------- Log Level Constants // ---------------------------------------------------- Log Level Constants
@@ -179,7 +187,6 @@ public class SimpleLog implements Log, Serializable {
} }
} }
// ------------------------------------------------------------- Attributes // ------------------------------------------------------------- Attributes
/** The name of this simple log instance */ /** The name of this simple log instance */
@@ -281,7 +288,12 @@ public class SimpleLog implements Log, Serializable {
// Append date-time if so configured // Append date-time if so configured
if(showDateTime) { 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(" "); buf.append(" ");
} }