incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From fmesc...@apache.org
Subject svn commit: r1398675 - in /sling/trunk/bundles/commons/log/src: main/java/org/apache/sling/commons/log/internal/slf4j/ test/java/org/apache/sling/commons/log/internal/slf4j/
Date Tue, 16 Oct 2012 07:01:52 GMT
Author: fmeschbe
Date: Tue Oct 16 07:01:52 2012
New Revision: 1398675

URL: http://svn.apache.org/viewvc?rev=1398675&view=rev
Log:
SLING-2614 support numeric log levels for configuration in addition to strings and add unit
tests for the level conversions

Added:
    sling/trunk/bundles/commons/log/src/test/java/org/apache/sling/commons/log/internal/slf4j/SlingLoggerLevelTest.java
Modified:
    sling/trunk/bundles/commons/log/src/main/java/org/apache/sling/commons/log/internal/slf4j/LogConfigManager.java
    sling/trunk/bundles/commons/log/src/main/java/org/apache/sling/commons/log/internal/slf4j/SlingLoggerLevel.java

Modified: sling/trunk/bundles/commons/log/src/main/java/org/apache/sling/commons/log/internal/slf4j/LogConfigManager.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/log/src/main/java/org/apache/sling/commons/log/internal/slf4j/LogConfigManager.java?rev=1398675&r1=1398674&r2=1398675&view=diff
==============================================================================
--- sling/trunk/bundles/commons/log/src/main/java/org/apache/sling/commons/log/internal/slf4j/LogConfigManager.java
(original)
+++ sling/trunk/bundles/commons/log/src/main/java/org/apache/sling/commons/log/internal/slf4j/LogConfigManager.java
Tue Oct 16 07:01:52 2012
@@ -479,7 +479,11 @@ public class LogConfigManager implements
                 throw new ConfigurationException(LogManager.LOG_LEVEL,
                     "Value required");
             }
-            SlingLoggerLevel logLevel = SlingLoggerLevel.valueOf(level.toUpperCase());
+            SlingLoggerLevel logLevel = SlingLoggerLevel.fromString(level);
+            if (logLevel == null) {
+                throw new ConfigurationException(LogManager.LOG_LEVEL,
+                    "Unsupported value: " + level);
+            }
 
             // verify pattern
             if (pattern == null || pattern.length() == 0) {

Modified: sling/trunk/bundles/commons/log/src/main/java/org/apache/sling/commons/log/internal/slf4j/SlingLoggerLevel.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/log/src/main/java/org/apache/sling/commons/log/internal/slf4j/SlingLoggerLevel.java?rev=1398675&r1=1398674&r2=1398675&view=diff
==============================================================================
--- sling/trunk/bundles/commons/log/src/main/java/org/apache/sling/commons/log/internal/slf4j/SlingLoggerLevel.java
(original)
+++ sling/trunk/bundles/commons/log/src/main/java/org/apache/sling/commons/log/internal/slf4j/SlingLoggerLevel.java
Tue Oct 16 07:01:52 2012
@@ -30,7 +30,7 @@ public enum SlingLoggerLevel {
 
     /**
      * Translates SLF4J logging levels into {@link SlingLoggerLevel}
-     * 
+     *
      * @param level The SLF4J logging level
      * @return The matching {@link SlingLoggerLevel}
      */
@@ -52,4 +52,76 @@ public enum SlingLoggerLevel {
         return slingLevel;
     }
 
+    /**
+     * Converts the given {@code levelString} to a {@link SlingLoggerLevel}
+     * instance or {@code null} of not possible. If the value converted to an
+     * upper case string is one of the {@link SlingLoggerLevel} constant names,
+     * the respective constant is returned. Otherwise the string is converted to
+     * an int (if possible) and the following mapping is applied:
+     * <table>
+     * <tr>
+     * <th>level</th>
+     * <th>SlingLoggerLevel</th>
+     * </tr>
+     * <tr>
+     * <td>0 or 1</td>
+     * <td>{@link #ERROR}</td>
+     * </tr>
+     * <tr>
+     * <td>2</td>
+     * <td>{@link #WARN}</td>
+     * </tr>
+     * <tr>
+     * <td>3</td>
+     * <td>{@link #INFO}</td>
+     * </tr>
+     * <tr>
+     * <td>4</td>
+     * <td>{@link #DEBUG}</td>
+     * </tr>
+     * <tr>
+     * <td>5</td>
+     * <td>{@link #TRACE}</td>
+     * </tr>
+     * </table>
+     * <p>
+     * If the string value is not one of the constant names, or cannot be
+     * converted to a number or the number is not one of the supported values,
+     * {@code null} is returned.
+     *
+     * @param levelString The string value to convert into a
+     *            {@code SlingLoggerLevel}
+     * @return The {@link SlingLoggerLevel} instance or {@code null} if the
+     *         string value cannot be converted.
+     */
+    public static SlingLoggerLevel fromString(final String levelString) {
+        if (levelString != null && levelString.length() > 0) {
+            try {
+                return SlingLoggerLevel.valueOf(levelString.toUpperCase());
+            } catch (IllegalArgumentException iae) {
+                // ignore
+            }
+
+            try {
+                final int level = Integer.parseInt(levelString);
+                switch (level) {
+                    case 0:
+                    case 1:
+                        return ERROR;
+                    case 2:
+                        return WARN;
+                    case 3:
+                        return INFO;
+                    case 4:
+                        return DEBUG;
+                    case 5:
+                        return TRACE;
+                }
+            } catch (NumberFormatException nfe) {
+                // ignore
+            }
+        }
+
+        return null;
+    }
 }

Added: sling/trunk/bundles/commons/log/src/test/java/org/apache/sling/commons/log/internal/slf4j/SlingLoggerLevelTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/log/src/test/java/org/apache/sling/commons/log/internal/slf4j/SlingLoggerLevelTest.java?rev=1398675&view=auto
==============================================================================
--- sling/trunk/bundles/commons/log/src/test/java/org/apache/sling/commons/log/internal/slf4j/SlingLoggerLevelTest.java
(added)
+++ sling/trunk/bundles/commons/log/src/test/java/org/apache/sling/commons/log/internal/slf4j/SlingLoggerLevelTest.java
Tue Oct 16 07:01:52 2012
@@ -0,0 +1,81 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.sling.commons.log.internal.slf4j;
+
+import org.slf4j.spi.LocationAwareLogger;
+
+import junit.framework.TestCase;
+
+public class SlingLoggerLevelTest extends TestCase {
+
+    public void test_fromSlf4jLevel() {
+        assertEquals(SlingLoggerLevel.TRACE, SlingLoggerLevel.fromSlf4jLevel(LocationAwareLogger.TRACE_INT-1));
+        assertEquals(SlingLoggerLevel.TRACE, SlingLoggerLevel.fromSlf4jLevel(LocationAwareLogger.TRACE_INT));
+        assertEquals(SlingLoggerLevel.TRACE, SlingLoggerLevel.fromSlf4jLevel(LocationAwareLogger.TRACE_INT+1));
+
+        assertEquals(SlingLoggerLevel.TRACE, SlingLoggerLevel.fromSlf4jLevel(LocationAwareLogger.DEBUG_INT-1));
+        assertEquals(SlingLoggerLevel.DEBUG, SlingLoggerLevel.fromSlf4jLevel(LocationAwareLogger.DEBUG_INT));
+        assertEquals(SlingLoggerLevel.DEBUG, SlingLoggerLevel.fromSlf4jLevel(LocationAwareLogger.DEBUG_INT+1));
+
+        assertEquals(SlingLoggerLevel.DEBUG, SlingLoggerLevel.fromSlf4jLevel(LocationAwareLogger.INFO_INT-1));
+        assertEquals(SlingLoggerLevel.INFO, SlingLoggerLevel.fromSlf4jLevel(LocationAwareLogger.INFO_INT));
+        assertEquals(SlingLoggerLevel.INFO, SlingLoggerLevel.fromSlf4jLevel(LocationAwareLogger.INFO_INT+1));
+
+        assertEquals(SlingLoggerLevel.INFO, SlingLoggerLevel.fromSlf4jLevel(LocationAwareLogger.WARN_INT-1));
+        assertEquals(SlingLoggerLevel.WARN, SlingLoggerLevel.fromSlf4jLevel(LocationAwareLogger.WARN_INT));
+        assertEquals(SlingLoggerLevel.WARN, SlingLoggerLevel.fromSlf4jLevel(LocationAwareLogger.WARN_INT+1));
+
+        assertEquals(SlingLoggerLevel.WARN, SlingLoggerLevel.fromSlf4jLevel(LocationAwareLogger.ERROR_INT-1));
+        assertEquals(SlingLoggerLevel.ERROR, SlingLoggerLevel.fromSlf4jLevel(LocationAwareLogger.ERROR_INT));
+        assertEquals(SlingLoggerLevel.ERROR, SlingLoggerLevel.fromSlf4jLevel(LocationAwareLogger.ERROR_INT+1));
+    }
+
+    public void test_fromString() {
+        assertNull(SlingLoggerLevel.fromString(null));
+
+        assertNull(SlingLoggerLevel.fromString("traze"));
+        assertNull(SlingLoggerLevel.fromString("-1"));
+        assertNull(SlingLoggerLevel.fromString("7"));
+
+        assertEquals(SlingLoggerLevel.TRACE, SlingLoggerLevel.fromString("trace"));
+        assertEquals(SlingLoggerLevel.DEBUG, SlingLoggerLevel.fromString("debug"));
+        assertEquals(SlingLoggerLevel.INFO, SlingLoggerLevel.fromString("info"));
+        assertEquals(SlingLoggerLevel.WARN, SlingLoggerLevel.fromString("warn"));
+        assertEquals(SlingLoggerLevel.ERROR, SlingLoggerLevel.fromString("error"));
+
+        assertEquals(SlingLoggerLevel.TRACE, SlingLoggerLevel.fromString("TRACE"));
+        assertEquals(SlingLoggerLevel.DEBUG, SlingLoggerLevel.fromString("DEBUG"));
+        assertEquals(SlingLoggerLevel.INFO, SlingLoggerLevel.fromString("INFO"));
+        assertEquals(SlingLoggerLevel.WARN, SlingLoggerLevel.fromString("WARN"));
+        assertEquals(SlingLoggerLevel.ERROR, SlingLoggerLevel.fromString("ERROR"));
+
+        assertEquals(SlingLoggerLevel.TRACE, SlingLoggerLevel.fromString("TrAcE"));
+        assertEquals(SlingLoggerLevel.DEBUG, SlingLoggerLevel.fromString("dEbUg"));
+        assertEquals(SlingLoggerLevel.INFO, SlingLoggerLevel.fromString("Info"));
+        assertEquals(SlingLoggerLevel.WARN, SlingLoggerLevel.fromString("Warn"));
+        assertEquals(SlingLoggerLevel.ERROR, SlingLoggerLevel.fromString("ErroR"));
+
+        assertEquals(SlingLoggerLevel.TRACE, SlingLoggerLevel.fromString("5"));
+        assertEquals(SlingLoggerLevel.DEBUG, SlingLoggerLevel.fromString("4"));
+        assertEquals(SlingLoggerLevel.INFO, SlingLoggerLevel.fromString("3"));
+        assertEquals(SlingLoggerLevel.WARN, SlingLoggerLevel.fromString("2"));
+        assertEquals(SlingLoggerLevel.ERROR, SlingLoggerLevel.fromString("1"));
+        assertEquals(SlingLoggerLevel.ERROR, SlingLoggerLevel.fromString("0"));
+    }
+}
\ No newline at end of file



Mime
View raw message