carterkozak commented on a change in pull request #289: LOG4J2-2644 - Improve 
performance of getting location info
URL: https://github.com/apache/logging-log4j2/pull/289#discussion_r298841873
 
 

 ##########
 File path: 
log4j-api-java9/src/main/java/org/apache/logging/log4j/util/StackLocator.java
 ##########
 @@ -72,15 +78,40 @@ private StackLocator() {
     }
 
     public StackTraceElement calcLocation(final String fqcnOfLogger) {
-        return stackWalker.walk(
-                s -> s.dropWhile(f -> !f.getClassName().equals(fqcnOfLogger)) 
// drop the top frames until we reach the logger
-                        .dropWhile(f -> f.getClassName().equals(fqcnOfLogger)) 
// drop the logger frames
-                        .findFirst())
-                .get()
-                .toStackTraceElement();
+        return walker.walk(s -> 
s.filter(LOCATOR.get().setFqcn(fqcnOfLogger)).findFirst()).get().toStackTraceElement();
     }
 
     public StackTraceElement getStackTraceElement(final int depth) {
         return stackWalker.walk(s -> 
s.skip(depth).findFirst()).get().toStackTraceElement();
     }
+
+    static final class CallerLocator implements 
Predicate<StackWalker.StackFrame> {
+
+        private String fqcn;
+
+        private boolean foundFqcn = false;
+        private boolean foundLogger = false;
+
+        public CallerLocator setFqcn(String fqcn) {
+            this.fqcn = fqcn;
+            this.foundFqcn = false;
+            this.foundLogger = false;
+            return this;
+        }
+
+        @Override
+        public boolean test(StackWalker.StackFrame t) {
+            final String className = t.getClassName();
+            if (!foundLogger) {
+                if (!foundFqcn) {
+                    foundFqcn = className.equals(fqcn);
+                    return false;
+                } else if (!className.equals(fqcn)) {
+                    foundLogger = true;
+                    return true;
+                }
+            }
+            return false;
+        }
 
 Review comment:
   It took me a few reads through to understand how this works, would you mind 
adding a comment to make it easier to understand?
   
   Maybe something along these lines (if I understand correctly)
   ```java
   /**
    * States:
    * 1. foundFqcn=false, foundLogger=false Nested in logging code, we must 
first
    *     work our way up to frames from the loggers fully qualified class name.
    * 2. foundFqcn=true, foundLogger=false Once we've found the logger, the next
    *     class we encounter which does not match the loggers fully qualified 
class
    *     name is the caller.
    * 3. foundFqcn=true, foundLogger=true Terminal state, if execution 
continues after
    *     the logger is found, the caller is doing something incorrectly.
    */
   public boolean test(StackWalker.StackFrame t) {
       final String className = t.getClassName();
       if (!foundLogger) {
           if (!foundFqcn) {
               foundFqcn = className.equals(fqcn);
               return false;
           } else if (!className.equals(fqcn)) {
               foundLogger = true;
               return true;
           }
       }
       // Unexpected usage, CallerLocator.test has been invoked after the 
logger has been found.
       return false;
   }
   ```

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to