You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-dev@logging.apache.org by rg...@apache.org on 2010/05/31 21:24:10 UTC

svn commit: r949837 - in /logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core: Log4jLogEvent.java Logger.java

Author: rgoers
Date: Mon May 31 19:24:10 2010
New Revision: 949837

URL: http://svn.apache.org/viewvc?rev=949837&view=rev
Log:
Fix bug getting proper StackTraceElement. Make PrivateLoggerConfig immutable and make members private. Change comments on updateConfiguration to make sense.

Modified:
    logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/Log4jLogEvent.java
    logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/Logger.java

Modified: logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/Log4jLogEvent.java
URL: http://svn.apache.org/viewvc/logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/Log4jLogEvent.java?rev=949837&r1=949836&r2=949837&view=diff
==============================================================================
--- logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/Log4jLogEvent.java (original)
+++ logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/Log4jLogEvent.java Mon May 31 19:24:10 2010
@@ -118,23 +118,8 @@ public class Log4jLogEvent implements Lo
     }
 
     /**
-     * @doubt Not quite sure what is going on with the loop, but looks like it might
-     *     drop only the deepest call from the fully qualified class, not all of them.
-     * (RG) The loop finds the FQCN and on the next iteration returns the StackTraceElement of
-     * the caller of FQCN. Don't know what you mean by "not all of them" as it only returns
-     * a single element.
-     *
-     *   Say that FQCN is "MySpecializedLogger" and the stack trace returned from getStackTrace is:
-     *
-     *   Log4jLogEvent.getSource
-     *   MySpecializedLogger.log
-     *   MySpecializedLogger.info
-     *   ClientClass.doSomething
-     *   ClientClass.main
-     *
-     *    When walking the stack, next will be set to true  at MySpecializedLogger.log
-     *    and MySpecializerLogger.info will be returned (at least from code inspection).
-     *
+     * Return the StackTraceElement for the caller. This will be the entry that occurs right
+     * before the first occurrence of FQCN as a class name.
      */
     public StackTraceElement getSource() {
         if (fqcnOfLogger == null) {
@@ -144,11 +129,15 @@ public class Log4jLogEvent implements Lo
             StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
             boolean next = false;
             for (StackTraceElement element : stackTrace) {
+                String className = element.getClassName();
                 if (next) {
+                    if (fqcnOfLogger.equals(className)) {
+                        continue;
+                    }
                     location = element;
                     break;
                 }
-                String className = element.getClassName();
+
                 if (fqcnOfLogger.equals(className)) {
                     next = true;
                 } else if (NOT_AVAIL.equals(className)) {

Modified: logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/Logger.java
URL: http://svn.apache.org/viewvc/logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/Logger.java?rev=949837&r1=949836&r2=949837&view=diff
==============================================================================
--- logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/Logger.java (original)
+++ logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/Logger.java Mon May 31 19:24:10 2010
@@ -69,18 +69,14 @@ public class Logger extends AbstractLogg
         return new Logger(context, name);
     }
 
-    /* @Override
-    protected String getFQCN() {
-        return FQCN;
-    } */
-
     public LoggerContext getContext() {
         return context;
     }
 
     public synchronized void setLevel(Level level) {
-        config.level = level;
-        config.intLevel = level.intLevel();
+        if (level != null) {
+            config = new PrivateConfig(config, level);
+        }
     }
 
     public Level getLevel() {
@@ -162,29 +158,27 @@ public class Logger extends AbstractLogg
     }
 
     /**
-     * This method isn't synchronized to serialized updates to config. Rather, by doing this
-     * it is guaranteed that all threads will see the update without having to declare the variable
-     * volatile.
+     * There are two ways that could be used to guarantee all threads are aware of changes to
+     * config. 1. synchronize this method. Accessors don't need to be synchronized as Java wil
+     * treat all variables within a synchronized block as volatile. 2. Declare the variable
+     * volatile. Option 2 is used here as the performance cost is very low and it does a better
+     * job at documenting how it is used.
      *
      * @param config The new Configuration.
-     * @doubt lost me on the comment, this.config is declared volatile. (RG) Me too.
      */
     void updateConfiguration(Configuration config) {
         this.config = new PrivateConfig(config, this);
     }
 
     /**
-     * @doubt class is not immutable, so it should not be shared between threads. (RG) The class MUST be
-     * shared between threads. The level could be made final and a new PrivateConfig constructed when
-     * setLevel is called.
+     * The binding between a Logger and its configuration.
      */
     protected class PrivateConfig {
-	/** @doubt public member variables?  (RG) Should be changed. Did this while making the Log4j 1.2 API **/
-        public final LoggerConfig loggerConfig;
-        public final Configuration config;
-        public Level level;
-        public int intLevel;
-        public final Logger logger;
+        private final LoggerConfig loggerConfig;
+        private final Configuration config;
+        private final Level level;
+        private final int intLevel;
+        private final Logger logger;
 
         public PrivateConfig(Configuration config, Logger logger) {
             this.config = config;
@@ -194,6 +188,14 @@ public class Logger extends AbstractLogg
             this.logger = logger;
         }
 
+        public PrivateConfig(PrivateConfig pc, Level level) {
+            this.config = pc.config;
+            this.loggerConfig = pc.loggerConfig;
+            this.level = level;
+            this.intLevel = this.level.intLevel();
+            this.logger = pc.logger;
+        }
+
         public PrivateConfig(PrivateConfig pc, LoggerConfig lc) {
             this.config = pc.config;
             this.loggerConfig = lc;



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