diff --git a/src/java/org/apache/commons/logging/impl/SimpleLog.java b/src/java/org/apache/commons/logging/impl/SimpleLog.java
index 82968ef..1120614 100644
--- a/src/java/org/apache/commons/logging/impl/SimpleLog.java
+++ b/src/java/org/apache/commons/logging/impl/SimpleLog.java
@@ -52,7 +52,13 @@ import org.apache.commons.logging.LogConfigurationException;
* included in output messages. Defaults to true.
*
org.apache.commons.logging.simplelog.showdatetime -
* Set to true if you want the current date and time
- * to be included in output messages. Default is false.false.
+ * org.apache.commons.logging.simplelog.dateTimeFormat -
+ * The date and time format to be used in the output messages.
+ * The pattern describing the date and time format is the same that is
+ * used in java.text.SimpleDateFormat. If the format is not
+ * specified or is invalid, the default format is used.
+ * The default format is yyyy/MM/dd HH:mm:ss:SSS zzz.In addition to looking for system properties with the names specified
@@ -64,7 +70,7 @@ import org.apache.commons.logging.LogConfigurationException;
* @author Rod Waldhoff
* @author Robert Burrell Donkin
*
- * @version $Id: SimpleLog.java,v 1.18 2004/03/01 02:12:48 craigmcc Exp $
+ * @version $Id: SimpleLog.java,v 1.19 2004/05/29 10:43:35 rdonkin Exp $
*/
public class SimpleLog implements Log, Serializable {
@@ -78,6 +84,10 @@ public class SimpleLog implements Log, Serializable {
/** Properties loaded from simplelog.properties */
static protected final Properties simpleLogProps = new Properties();
+ /** The default format to use when formating dates */
+ static protected final String DEFAULT_DATE_TIME_FORMAT =
+ "yyyy/MM/dd HH:mm:ss:SSS zzz";
+
/** Include the instance name in the log message? */
static protected boolean showLogName = false;
/** Include the short name ( last component ) of the logger in the log
@@ -87,6 +97,8 @@ public class SimpleLog implements Log, Serializable {
static protected boolean showShortName = true;
/** Include the current time in the log message */
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 */
static protected DateFormat dateFormatter = null;
@@ -154,9 +166,19 @@ public class SimpleLog implements Log, Serializable {
showDateTime = getBooleanProperty( systemPrefix + "showdatetime", showDateTime);
if(showDateTime) {
- dateFormatter = new SimpleDateFormat(
- getStringProperty(systemPrefix + "dateformat",
- "yyyy/MM/dd HH:mm:ss:SSS zzz"));
+ dateTimeFormat = getStringProperty(systemPrefix + "dateTimeFormat",
+ dateTimeFormat);
+ if (dateTimeFormat == null || "".equals(dateTimeFormat)) {
+ // if this property has not been set then use the default
+ dateTimeFormat = DEFAULT_DATE_TIME_FORMAT;
+ }
+ try {
+ dateFormatter = new SimpleDateFormat(dateTimeFormat);
+ } catch(IllegalArgumentException e) {
+ // If the format pattern is invalid - use the default format
+ dateTimeFormat = DEFAULT_DATE_TIME_FORMAT;
+ dateFormatter = new SimpleDateFormat(dateTimeFormat);
+ }
}
}
@@ -170,7 +192,7 @@ public class SimpleLog implements Log, Serializable {
/** The short name of this simple log instance */
private String shortLogName = null;
-
+
// ------------------------------------------------------------ Constructor
/**
@@ -252,7 +274,7 @@ public class SimpleLog implements Log, Serializable {
* This method assembles the message
* and then calls write() to cause it to be written.
TestCase for sipmle logging when running with custom configuration + *
TestCase for simple logging when running with custom configuration * properties.
* * @author Craig R. McClanahan - * @version $Revision: 1.4 $ $Date: 2004/02/28 21:46:46 $ + * @version $Revision: 1.5 $ $Date: 2004/05/29 10:43:35 $ */ public class CustomConfigTestCase extends DefaultConfigTestCase { @@ -164,6 +163,8 @@ public class CustomConfigTestCase extends DefaultConfigTestCase { assertEquals(SimpleLog.LOG_LEVEL_DEBUG, ((SimpleLog) log).getLevel()); // Can we validate the extra exposed properties? + assertEquals("yyyy/MM/dd HH:mm:ss:SSS zzz", + ((DecoratedSimpleLog) log).getDateTimeFormat()); assertEquals("DecoratedLogger", ((DecoratedSimpleLog) log).getLogName()); assertTrue(!((DecoratedSimpleLog) log).getShowDateTime()); diff --git a/src/test/org/apache/commons/logging/simple/DecoratedSimpleLog.java b/src/test/org/apache/commons/logging/simple/DecoratedSimpleLog.java index 76e360f..3e0f49a 100644 --- a/src/test/org/apache/commons/logging/simple/DecoratedSimpleLog.java +++ b/src/test/org/apache/commons/logging/simple/DecoratedSimpleLog.java @@ -41,6 +41,11 @@ public class DecoratedSimpleLog extends SimpleLog { // ------------------------------------------------------------- Properties + public String getDateTimeFormat() { + return (dateTimeFormat); + } + + public String getLogName() { return (logName); } diff --git a/src/test/org/apache/commons/logging/simple/DefaultConfigTestCase.java b/src/test/org/apache/commons/logging/simple/DefaultConfigTestCase.java index 4b58afa..711f38d 100644 --- a/src/test/org/apache/commons/logging/simple/DefaultConfigTestCase.java +++ b/src/test/org/apache/commons/logging/simple/DefaultConfigTestCase.java @@ -36,7 +36,7 @@ import org.apache.commons.logging.impl.SimpleLog; * other than selecting the SimpleLog implementation. * * @author Craig R. McClanahan - * @version $Revision: 1.4 $ $Date: 2004/02/28 21:46:46 $ + * @version $Revision: 1.5 $ $Date: 2004/05/29 10:43:35 $ */ public class DefaultConfigTestCase extends TestCase { @@ -178,6 +178,8 @@ public class DefaultConfigTestCase extends TestCase { assertEquals(SimpleLog.LOG_LEVEL_INFO, ((SimpleLog) log).getLevel()); // Can we validate the extra exposed properties? + assertEquals("yyyy/MM/dd HH:mm:ss:SSS zzz", + ((DecoratedSimpleLog) log).getDateTimeFormat()); assertEquals("DecoratedLogger", ((DecoratedSimpleLog) log).getLogName()); assertTrue(!((DecoratedSimpleLog) log).getShowDateTime());