[logback-dev] svn commit: r2343 - in logback/trunk/logback-core/src: main/java/ch/qos/logback/core/util test/java/ch/qos/logback/core/status test/java/ch/qos/logback/core/util

noreply.ceki at qos.ch noreply.ceki at qos.ch
Tue Jul 14 16:59:15 CEST 2009


Author: ceki
Date: Tue Jul 14 16:59:15 2009
New Revision: 2343

Modified:
   logback/trunk/logback-core/src/main/java/ch/qos/logback/core/util/Duration.java
   logback/trunk/logback-core/src/test/java/ch/qos/logback/core/status/StatusChecker.java
   logback/trunk/logback-core/src/test/java/ch/qos/logback/core/util/DurationTest.java

Log:
Adding support for automatic reconfiguration. Related to LBCORE-59
This implementation checks for changes in the config file 
every N seconds, where N is a user specified parameter. However, as a 
performance twist, the check is performed every 16 logging events.

It's still ongoing work

Modified: logback/trunk/logback-core/src/main/java/ch/qos/logback/core/util/Duration.java
==============================================================================
--- logback/trunk/logback-core/src/main/java/ch/qos/logback/core/util/Duration.java	(original)
+++ logback/trunk/logback-core/src/main/java/ch/qos/logback/core/util/Duration.java	Tue Jul 14 16:59:15 2009
@@ -13,14 +13,15 @@
 import java.util.regex.Pattern;
 
 /**
- * Duration instances represent a lapse of time. Internally, the duration is stored
- * in milliseconds.
+ * Duration instances represent a lapse of time. Internally, the duration is
+ * stored in milliseconds.
  * <p>
  * 
- * The {@link #valueOf} method can convert strings such as "3.5 minutes", "5 hours", into
- * Duration instances. The recognized units of time are the "millisecond", "second", "minute"
- * "hour" and "day". The unit name may be followed by an "s". Thus, "2 day" and "2 days" are
- * equivalent. In the absence of a time unit specification, milliseconds are assumed.
+ * The {@link #valueOf} method can convert strings such as "3.5 minutes", "5
+ * hours", into Duration instances. The recognized units of time are the
+ * "millisecond", "second", "minute" "hour" and "day". The unit name may be
+ * followed by an "s". Thus, "2 day" and "2 days" are equivalent. In the absence
+ * of a time unit specification, milliseconds are assumed.
  * 
  * 
  * @author Ceki Gulcu
@@ -30,7 +31,7 @@
   private final static String DOUBLE_PART = "([0-9]*(.[0-9]+)?)";
   private final static int DOUBLE_GROUP = 1;
 
-  private final static String UNIT_PART = "(|millisecond|second(e)?|minute|hour|day)s?";
+  private final static String UNIT_PART = "(|milli(second)?|second(e)?|minute|hour|day)s?";
   private final static int UNIT_GROUP = 3;
 
   private static final Pattern DURATION_PATTERN = Pattern.compile(DOUBLE_PART
@@ -50,27 +51,27 @@
   public static Duration buildByMilliseconds(double value) {
     return new Duration((long) (value));
   }
-  
+
   public static Duration buildBySeconds(double value) {
-    return new Duration((long) (SECONDS_COEFFICIENT*value));
+    return new Duration((long) (SECONDS_COEFFICIENT * value));
   }
 
   public static Duration buildByMinutes(double value) {
-    return new Duration((long) (MINUTES_COEFFICIENT*value));
+    return new Duration((long) (MINUTES_COEFFICIENT * value));
   }
-  
+
   public static Duration buildByHours(double value) {
-    return new Duration((long) (HOURS_COEFFICIENT*value));
+    return new Duration((long) (HOURS_COEFFICIENT * value));
   }
 
   public static Duration buildByDays(double value) {
-    return new Duration((long) (DAYS_COEFFICIENT*value));
+    return new Duration((long) (DAYS_COEFFICIENT * value));
   }
 
   public static Duration buildUnbounded() {
     return new Duration(Long.MAX_VALUE);
   }
-  
+
   public long getMilliseconds() {
     return millis;
   }
@@ -83,9 +84,11 @@
       String unitStr = matcher.group(UNIT_GROUP);
 
       double doubleValue = Double.valueOf(doubleStr);
-      if (unitStr.equalsIgnoreCase("millisecond") || unitStr.length() == 0) {
+      if (unitStr.equalsIgnoreCase("milli")
+          || unitStr.equalsIgnoreCase("millisecond") || unitStr.length() == 0) {
         return buildByMilliseconds(doubleValue);
-      } else if (unitStr.equalsIgnoreCase("second") || unitStr.equalsIgnoreCase("seconde")) {
+      } else if (unitStr.equalsIgnoreCase("second")
+          || unitStr.equalsIgnoreCase("seconde")) {
         return buildBySeconds(doubleValue);
       } else if (unitStr.equalsIgnoreCase("minute")) {
         return buildByMinutes(doubleValue);
@@ -94,25 +97,25 @@
       } else if (unitStr.equalsIgnoreCase("day")) {
         return buildByDays(doubleValue);
       } else {
-        throw new IllegalStateException("Unexpected" + unitStr);
+        throw new IllegalStateException("Unexpected " + unitStr);
       }
     } else {
       throw new IllegalArgumentException("String value [" + durationStr
           + "] is not in the expected format.");
     }
   }
-  
+
   @Override
   public String toString() {
-    if(millis < SECONDS_COEFFICIENT) {
+    if (millis < SECONDS_COEFFICIENT) {
       return millis + " milliseconds";
-    } else if (millis < MINUTES_COEFFICIENT){
-      return millis/SECONDS_COEFFICIENT +" seconds";   
-    } else if(millis < HOURS_COEFFICIENT) {
-      return millis/MINUTES_COEFFICIENT +" minutes";   
+    } else if (millis < MINUTES_COEFFICIENT) {
+      return millis / SECONDS_COEFFICIENT + " seconds";
+    } else if (millis < HOURS_COEFFICIENT) {
+      return millis / MINUTES_COEFFICIENT + " minutes";
     } else {
-      return millis/HOURS_COEFFICIENT+" hours";   
+      return millis / HOURS_COEFFICIENT + " hours";
     }
-   
+
   }
 }

Modified: logback/trunk/logback-core/src/test/java/ch/qos/logback/core/status/StatusChecker.java
==============================================================================
--- logback/trunk/logback-core/src/test/java/ch/qos/logback/core/status/StatusChecker.java	(original)
+++ logback/trunk/logback-core/src/test/java/ch/qos/logback/core/status/StatusChecker.java	Tue Jul 14 16:59:15 2009
@@ -13,8 +13,7 @@
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
-import ch.qos.logback.core.status.Status;
-import ch.qos.logback.core.status.StatusManager;
+import ch.qos.logback.core.Context;
 
 public class StatusChecker {
 
@@ -24,6 +23,10 @@
     this.sm = sm;
   }
 
+  public StatusChecker(Context context) {
+    this.sm = context.getStatusManager();
+  }
+  
   public boolean isErrorFree() {
     int level = sm.getLevel();
     return level < Status.ERROR;

Modified: logback/trunk/logback-core/src/test/java/ch/qos/logback/core/util/DurationTest.java
==============================================================================
--- logback/trunk/logback-core/src/test/java/ch/qos/logback/core/util/DurationTest.java	(original)
+++ logback/trunk/logback-core/src/test/java/ch/qos/logback/core/util/DurationTest.java	Tue Jul 14 16:59:15 2009
@@ -27,6 +27,16 @@
       Duration d = Duration.valueOf("12");
       assertEquals(12, d.getMilliseconds());
     }
+
+    {
+      Duration d = Duration.valueOf("159 milli");
+      assertEquals(159, d.getMilliseconds());
+    }
+    
+    {
+      Duration d = Duration.valueOf("15 millis");
+      assertEquals(15, d.getMilliseconds());
+    }
     
     {
       Duration d = Duration.valueOf("8 milliseconds");


More information about the logback-dev mailing list