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:
@@ -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.
|
||||
* <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;
|
||||
|
||||
// ---------------------------------------------------- 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(" ");
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user