db-torque-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tfisc...@apache.org
Subject svn commit: r1446063 - in /db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/configuration/controller: DummyLoggingAdapter.java Log4jLoggingAdapter.java LoggingAdapter.java Loglevel.java
Date Thu, 14 Feb 2013 08:27:22 GMT
Author: tfischer
Date: Thu Feb 14 08:27:22 2013
New Revision: 1446063

URL: http://svn.apache.org/r1446063
Log:
TORQUE-265 
Provide a bridge between the torque generator and the underlying logging system

Added:
    db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/configuration/controller/DummyLoggingAdapter.java
    db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/configuration/controller/Log4jLoggingAdapter.java
    db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/configuration/controller/LoggingAdapter.java
Modified:
    db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/configuration/controller/Loglevel.java

Added: db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/configuration/controller/DummyLoggingAdapter.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/configuration/controller/DummyLoggingAdapter.java?rev=1446063&view=auto
==============================================================================
--- db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/configuration/controller/DummyLoggingAdapter.java
(added)
+++ db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/configuration/controller/DummyLoggingAdapter.java
Thu Feb 14 08:27:22 2013
@@ -0,0 +1,39 @@
+package org.apache.torque.generator.configuration.controller;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+/**
+ * Dummy implementation of the LoggingAdapter interface.
+ * Actually does nothing.
+
+ * @version $Id: $
+ *
+ */
+public class DummyLoggingAdapter implements LoggingAdapter
+{
+    /** The logger. */
+    private static Log log = LogFactory.getLog(DummyLoggingAdapter.class);
+
+
+    /**
+     * Always returns <code>Loglevel.INFO</code>.
+     *
+     * @return <code>Loglevel.INFO</code>.
+     */
+    public Loglevel getCurrentLoglevel()
+    {
+        return Loglevel.INFO;
+    }
+
+    /**
+     * Does nothing except logging the argument.
+     *
+     * @param loglevel ignored.
+     */
+    public void setLoglevel(Loglevel loglevel)
+    {
+        log.debug("apply() : Not Setting loglevel to " + loglevel
+                + " because this is a dummy implementation");
+    }
+}

Added: db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/configuration/controller/Log4jLoggingAdapter.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/configuration/controller/Log4jLoggingAdapter.java?rev=1446063&view=auto
==============================================================================
--- db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/configuration/controller/Log4jLoggingAdapter.java
(added)
+++ db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/configuration/controller/Log4jLoggingAdapter.java
Thu Feb 14 08:27:22 2013
@@ -0,0 +1,75 @@
+package org.apache.torque.generator.configuration.controller;
+
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.log4j.Level;
+import org.apache.log4j.Logger;
+
+/**
+ * Allows reading and setting the current loglevel using log4j.
+ * @version $Id: $
+ *
+ */
+public class Log4jLoggingAdapter implements LoggingAdapter
+{
+    /** The logger. */
+    private static Log log = LogFactory.getLog(Log4jLoggingAdapter.class);
+
+    /** Map loglevel -> corresponding log4j level. */
+    private static final Map<Loglevel, Level> LOG4J_LEVELS;
+
+    static
+    {
+        Map<Loglevel, Level> log4jLevels = new HashMap<Loglevel, Level>();
+        log4jLevels.put(Loglevel.TRACE, Level.TRACE);
+        log4jLevels.put(Loglevel.DEBUG, Level.DEBUG);
+        log4jLevels.put(Loglevel.INFO, Level.INFO);
+        log4jLevels.put(Loglevel.WARN, Level.WARN);
+        log4jLevels.put(Loglevel.ERROR, Level.ERROR);
+        LOG4J_LEVELS = Collections.unmodifiableMap(log4jLevels);
+    }
+
+    /**
+     * Returns the current loglevel by reading the loglevel of the root logger.
+     *
+     * @return the current loglevel, or INFO if the current loglevel cannot
+     *         be determined.
+     */
+    public Loglevel getCurrentLoglevel()
+    {
+        Level level = Logger.getRootLogger().getLevel();
+        for (Map.Entry<Loglevel, Level> loglevel : LOG4J_LEVELS.entrySet())
+        {
+            if (loglevel.getValue().equals(level))
+            {
+                return loglevel.getKey();
+            }
+        }
+        return Loglevel.INFO;
+    }
+
+    /**
+     * Sets the loglevel to the given loglevel
+     * by changing the level of the log4j root logger.
+     *
+     *  @param loglevel the loglevel to set, not null.
+     */
+    public void setLoglevel(Loglevel loglevel)
+    {
+        if (loglevel == null)
+        {
+            return;
+        }
+        Level log4jLevel = LOG4J_LEVELS.get(loglevel);
+        if (Logger.getRootLogger().getLevel() != LOG4J_LEVELS.get(loglevel))
+        {
+            log.info("apply() : Setting loglevel to " + loglevel);
+            Logger.getRootLogger().setLevel(log4jLevel);
+        }
+    }
+
+}

Added: db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/configuration/controller/LoggingAdapter.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/configuration/controller/LoggingAdapter.java?rev=1446063&view=auto
==============================================================================
--- db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/configuration/controller/LoggingAdapter.java
(added)
+++ db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/configuration/controller/LoggingAdapter.java
Thu Feb 14 08:27:22 2013
@@ -0,0 +1,25 @@
+package org.apache.torque.generator.configuration.controller;
+
+/**
+ * Allows reading and setting the current loglevel.
+ * The implementation depends on the logging framework used.
+ *
+ * @version $Id: $
+ */
+public interface LoggingAdapter
+{
+    /**
+     * Returns the current loglevel.
+     *
+     * @return the current loglevel, or INFO if the current loglevel cannot
+     *         be determined.
+     */
+    public Loglevel getCurrentLoglevel();
+
+    /**
+     * Sets the loglevel to the given loglevel.
+     *
+     *  @param loglevel the loglevel to set, not null.
+     */
+    public void setLoglevel(Loglevel loglevel);
+}

Modified: db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/configuration/controller/Loglevel.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/configuration/controller/Loglevel.java?rev=1446063&r1=1446062&r2=1446063&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/configuration/controller/Loglevel.java
(original)
+++ db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/configuration/controller/Loglevel.java
Thu Feb 14 08:27:22 2013
@@ -19,46 +19,39 @@ package org.apache.torque.generator.conf
  * under the License.
  */
 
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
-import org.apache.log4j.Level;
-import org.apache.log4j.Logger;
 
 /**
- * The possible log levels. This implementation uses log4j internally,
+ * The possible log levels.
+ * The logging framework tio use can be set by  uses log4j internally,
  * but the interface is agnostic of the logging framework.
  */
 public enum Loglevel
 {
     /** Loglevel trace. */
-    TRACE("trace", Level.TRACE),
+    TRACE("trace"),
     /** Loglevel debug. */
-    DEBUG("debug", Level.DEBUG),
+    DEBUG("debug"),
     /** Loglevel info. */
-    INFO("info", Level.INFO),
+    INFO("info"),
     /** Loglevel warn. */
-    WARN("warn", Level.WARN),
+    WARN("warn"),
     /** Loglevel error. */
-    ERROR("error", Level.ERROR);
+    ERROR("error");
 
     /** The key of the loglevel. */
     private String key;
 
-    /** The log4j loglevel. */
-    private Level log4jLevel;
-
-    /** The logger. */
-    private static Log log = LogFactory.getLog(Loglevel.class);
+    /** The logging adapter to use. */
+    private static LoggingAdapter loggingAdapter = new Log4jLoggingAdapter();
 
     /**
      * Constructor.
      *
      * @param key the key, not null.
      */
-    private Loglevel(String key, Level log4jLevel)
+    private Loglevel(String key)
     {
         this.key = key;
-        this.log4jLevel = log4jLevel;
     }
 
     /**
@@ -76,11 +69,7 @@ public enum Loglevel
      */
     public void apply()
     {
-        if (Logger.getRootLogger().getLevel() != log4jLevel)
-        {
-            log.info("apply() : Setting loglevel to " + this);
-            Logger.getRootLogger().setLevel(log4jLevel);
-        }
+        loggingAdapter.setLoglevel(this);
     }
 
     /**
@@ -114,14 +103,32 @@ public enum Loglevel
      */
     public static Loglevel getCurrentLoglevel()
     {
-        Level level = Logger.getRootLogger().getLevel();
-        for (Loglevel loglevel : values())
+        return loggingAdapter.getCurrentLoglevel();
+    }
+
+    /**
+     * Returns the currently used logging adapter.
+     *
+     * @return the current logging adapter, not null.
+     */
+    public static LoggingAdapter getLoggingAdapter()
+    {
+        return loggingAdapter;
+    }
+
+    /**
+     * Sets the logging adapter.
+     *
+     * @return the logging adapter, not null.
+     *
+     * @throws NullPointerException if loggingAdapter is null.
+     */
+    public static void setLoggingAdapter(LoggingAdapter loggingAdapter)
+    {
+        if (loggingAdapter == null)
         {
-            if (loglevel.log4jLevel.equals(level))
-            {
-                return loglevel;
-            }
+            throw new NullPointerException("loggingAdapter must not be null");
         }
-        return INFO;
+        Loglevel.loggingAdapter = loggingAdapter;
     }
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: torque-dev-unsubscribe@db.apache.org
For additional commands, e-mail: torque-dev-help@db.apache.org


Mime
View raw message