You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by tn...@apache.org on 2013/02/20 13:28:04 UTC

svn commit: r1448119 - in /commons/proper/logging/trunk/src/main/java/org/apache/commons/logging/impl: Log4JLogger.java LogKitLogger.java

Author: tn
Date: Wed Feb 20 12:28:04 2013
New Revision: 1448119

URL: http://svn.apache.org/r1448119
Log:
[LOGGING-135] Use double-checked locking idiom to improve thread-safety of Log4JLogger and LogKitLogger.

Modified:
    commons/proper/logging/trunk/src/main/java/org/apache/commons/logging/impl/Log4JLogger.java
    commons/proper/logging/trunk/src/main/java/org/apache/commons/logging/impl/LogKitLogger.java

Modified: commons/proper/logging/trunk/src/main/java/org/apache/commons/logging/impl/Log4JLogger.java
URL: http://svn.apache.org/viewvc/commons/proper/logging/trunk/src/main/java/org/apache/commons/logging/impl/Log4JLogger.java?rev=1448119&r1=1448118&r2=1448119&view=diff
==============================================================================
--- commons/proper/logging/trunk/src/main/java/org/apache/commons/logging/impl/Log4JLogger.java (original)
+++ commons/proper/logging/trunk/src/main/java/org/apache/commons/logging/impl/Log4JLogger.java Wed Feb 20 12:28:04 2013
@@ -53,7 +53,7 @@ public class Log4JLogger implements Log,
     private static final String FQCN = Log4JLogger.class.getName();
 
     /** Log to this logger */
-    private transient Logger logger = null;
+    private transient volatile Logger logger = null;
 
     /** Logger name */
     private final String name;
@@ -111,7 +111,7 @@ public class Log4JLogger implements Log,
     /**
      * For use with a log4j factory.
      */
-    public Log4JLogger(Logger logger ) {
+    public Log4JLogger(Logger logger) {
         if (logger == null) {
             throw new IllegalArgumentException(
                 "Warning - null logger in constructor; possible log4j misconfiguration.");
@@ -129,7 +129,7 @@ public class Log4JLogger implements Log,
      * @see org.apache.commons.logging.Log#trace(Object)
      */
     public void trace(Object message) {
-        getLogger().log(FQCN, traceLevel, message, null );
+        getLogger().log(FQCN, traceLevel, message, null);
     }
 
     /**
@@ -142,7 +142,7 @@ public class Log4JLogger implements Log,
      * @see org.apache.commons.logging.Log#trace(Object, Throwable)
      */
     public void trace(Object message, Throwable t) {
-        getLogger().log(FQCN, traceLevel, message, t );
+        getLogger().log(FQCN, traceLevel, message, t);
     }
 
     /**
@@ -152,7 +152,7 @@ public class Log4JLogger implements Log,
      * @see org.apache.commons.logging.Log#debug(Object)
      */
     public void debug(Object message) {
-        getLogger().log(FQCN, Level.DEBUG, message, null );
+        getLogger().log(FQCN, Level.DEBUG, message, null);
     }
 
     /**
@@ -163,7 +163,7 @@ public class Log4JLogger implements Log,
      * @see org.apache.commons.logging.Log#debug(Object, Throwable)
      */
     public void debug(Object message, Throwable t) {
-        getLogger().log(FQCN, Level.DEBUG, message, t );
+        getLogger().log(FQCN, Level.DEBUG, message, t);
     }
 
     /**
@@ -173,7 +173,7 @@ public class Log4JLogger implements Log,
      * @see org.apache.commons.logging.Log#info(Object)
      */
     public void info(Object message) {
-        getLogger().log(FQCN, Level.INFO, message, null );
+        getLogger().log(FQCN, Level.INFO, message, null);
     }
 
     /**
@@ -184,7 +184,7 @@ public class Log4JLogger implements Log,
      * @see org.apache.commons.logging.Log#info(Object, Throwable)
      */
     public void info(Object message, Throwable t) {
-        getLogger().log(FQCN, Level.INFO, message, t );
+        getLogger().log(FQCN, Level.INFO, message, t);
     }
 
     /**
@@ -194,7 +194,7 @@ public class Log4JLogger implements Log,
      * @see org.apache.commons.logging.Log#warn(Object)
      */
     public void warn(Object message) {
-        getLogger().log(FQCN, Level.WARN, message, null );
+        getLogger().log(FQCN, Level.WARN, message, null);
     }
 
     /**
@@ -205,7 +205,7 @@ public class Log4JLogger implements Log,
      * @see org.apache.commons.logging.Log#warn(Object, Throwable)
      */
     public void warn(Object message, Throwable t) {
-        getLogger().log(FQCN, Level.WARN, message, t );
+        getLogger().log(FQCN, Level.WARN, message, t);
     }
 
     /**
@@ -215,7 +215,7 @@ public class Log4JLogger implements Log,
      * @see org.apache.commons.logging.Log#error(Object)
      */
     public void error(Object message) {
-        getLogger().log(FQCN, Level.ERROR, message, null );
+        getLogger().log(FQCN, Level.ERROR, message, null);
     }
 
     /**
@@ -226,7 +226,7 @@ public class Log4JLogger implements Log,
      * @see org.apache.commons.logging.Log#error(Object, Throwable)
      */
     public void error(Object message, Throwable t) {
-        getLogger().log(FQCN, Level.ERROR, message, t );
+        getLogger().log(FQCN, Level.ERROR, message, t);
     }
 
     /**
@@ -236,7 +236,7 @@ public class Log4JLogger implements Log,
      * @see org.apache.commons.logging.Log#fatal(Object)
      */
     public void fatal(Object message) {
-        getLogger().log(FQCN, Level.FATAL, message, null );
+        getLogger().log(FQCN, Level.FATAL, message, null);
     }
 
     /**
@@ -247,17 +247,23 @@ public class Log4JLogger implements Log,
      * @see org.apache.commons.logging.Log#fatal(Object, Throwable)
      */
     public void fatal(Object message, Throwable t) {
-        getLogger().log(FQCN, Level.FATAL, message, t );
+        getLogger().log(FQCN, Level.FATAL, message, t);
     }
 
     /**
      * Return the native Logger instance we are using.
      */
     public Logger getLogger() {
-        if (logger == null) {
-            logger = Logger.getLogger(name);
+        Logger result = logger;
+        if (result == null) {
+            synchronized(this) {
+                result = logger;
+                if (result == null) {
+                    logger = result = Logger.getLogger(name);
+                }
+            }
         }
-        return this.logger;
+        return result;
     }
 
     /**

Modified: commons/proper/logging/trunk/src/main/java/org/apache/commons/logging/impl/LogKitLogger.java
URL: http://svn.apache.org/viewvc/commons/proper/logging/trunk/src/main/java/org/apache/commons/logging/impl/LogKitLogger.java?rev=1448119&r1=1448118&r2=1448119&view=diff
==============================================================================
--- commons/proper/logging/trunk/src/main/java/org/apache/commons/logging/impl/LogKitLogger.java (original)
+++ commons/proper/logging/trunk/src/main/java/org/apache/commons/logging/impl/LogKitLogger.java Wed Feb 20 12:28:04 2013
@@ -41,7 +41,7 @@ public class LogKitLogger implements Log
     // ------------------------------------------------------------- Attributes
 
     /** Logging goes to this <code>LogKit</code> logger */
-    protected transient Logger logger = null;
+    protected transient volatile Logger logger = null;
 
     /** Name of this logger */
     protected String name = null;
@@ -65,12 +65,16 @@ public class LogKitLogger implements Log
      * Return the underlying Logger we are using.
      */
     public Logger getLogger() {
-
-        if (logger == null) {
-            logger = Hierarchy.getDefaultHierarchy().getLoggerFor(name);
+        Logger result = logger;
+        if (result == null) {
+            synchronized(this) {
+                result = logger;
+                if (result == null) {
+                    logger = result = Hierarchy.getDefaultHierarchy().getLoggerFor(name);
+                }
+            }
         }
-        return logger;
-
+        return result;
     }
 
     // ----------------------------------------------------- Log Implementation