ramanathan1504 commented on code in PR #4470:
URL: https://github.com/apache/logging-log4j2/pull/4470#discussion_r4534560049


##########
log4j-core-test/src/test/java/org/apache/logging/log4j/core/async/AsyncLoggerQueueFullLocationTest.java:
##########
@@ -0,0 +1,136 @@
+/*
+ * 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.logging.log4j.core.async;
+
+import static java.util.concurrent.TimeUnit.SECONDS;
+import static org.assertj.core.api.Assertions.assertThat;
+
+import com.lmax.disruptor.RingBuffer;
+import java.util.LinkedHashMap;
+import java.util.Map;
+import java.util.concurrent.CountDownLatch;
+import org.apache.logging.log4j.Level;
+import org.apache.logging.log4j.core.LogEvent;
+import org.apache.logging.log4j.core.LoggerContext;
+import org.apache.logging.log4j.core.appender.AbstractAppender;
+import org.apache.logging.log4j.core.config.Configuration;
+import org.apache.logging.log4j.core.config.Property;
+import org.apache.logging.log4j.core.test.junit.LoggerContextSource;
+import org.apache.logging.log4j.core.test.junit.Tags;
+import org.apache.logging.log4j.core.util.Constants;
+import org.apache.logging.log4j.test.junit.SetTestProperty;
+import org.junit.jupiter.api.Tag;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.Timeout;
+import org.junit.jupiter.api.Timeout.ThreadMode;
+
+/**
+ * Checks the location of events that {@link AsyncLogger} logs in the current 
thread, because the ring buffer is full.
+ */
+@SetTestProperty(
+        key = Constants.LOG4J_CONTEXT_SELECTOR,
+        value = 
"org.apache.logging.log4j.core.async.AsyncLoggerContextSelector")
+@SetTestProperty(key = "log4j2.asyncLoggerRingBufferSize", value = "128")
+@SetTestProperty(
+        key = "log4j2.asyncQueueFullPolicy",
+        value = 
"org.apache.logging.log4j.core.async.AsyncLoggerQueueFullLocationTest$SynchronousQueueFullPolicy")
+@Tag(Tags.ASYNC_LOGGERS)
+@Timeout(value = 10, unit = SECONDS, threadMode = ThreadMode.SEPARATE_THREAD)
+class AsyncLoggerQueueFullLocationTest {
+
+    private static final StackTraceElement EXPLICIT_LOCATION =
+            new StackTraceElement("com.example.Caller", "explicit", 
"Caller.java", 42);
+
+    /**
+     * Logs all events in the current thread, when the ring buffer is full.
+     */
+    public static final class SynchronousQueueFullPolicy implements 
AsyncQueueFullPolicy {
+
+        @Override
+        public EventRoute getRoute(final long backgroundThreadId, final Level 
level) {
+            return EventRoute.SYNCHRONOUS;
+        }
+    }
+
+    @Test
+    @LoggerContextSource("AsyncLoggerQueueFullLocationTest.xml")
+    void threadLocalTranslator(final LoggerContext context) throws 
InterruptedException {
+        assertLocationsWithFullRingBuffer(context, true);
+    }
+
+    @Test
+    @LoggerContextSource("AsyncLoggerQueueFullLocationTest.xml")
+    void varargTranslator(final LoggerContext context) throws 
InterruptedException {
+        assertLocationsWithFullRingBuffer(context, false);
+    }
+
+    private static void assertLocationsWithFullRingBuffer(final LoggerContext 
context, final boolean useThreadLocals)
+            throws InterruptedException {
+        final Thread testThread = Thread.currentThread();
+        final CountDownLatch release = new CountDownLatch(1);
+        // Locations of the events logged in the test thread, by message
+        final Map<String, StackTraceElement> locations = new LinkedHashMap<>();
+        final AbstractAppender appender = new AbstractAppender("Recording", 
null, null, true, Property.EMPTY_ARRAY) {
+            @Override
+            public void append(final LogEvent event) {
+                if (Thread.currentThread() == testThread) {
+                    locations.put(event.getMessage().getFormattedMessage(), 
event.getSource());

Review Comment:
   The recursion branch in both `handleRingBufferFull` methods passes the 
location too, but no test reaches it. A nested call from this appender while 
the buffer is full goes through that branch:
   
   ```java
                       locations.put(event.getMessage().getFormattedMessage(), 
event.getSource());
                       if 
("explicit".equals(event.getMessage().getFormattedMessage())) {
                           
context.getLogger(AsyncLoggerQueueFullLocationTest.class)
                                   .atInfo()
                                   .withLocation(NESTED_LOCATION)
                                   .log("nested");
                       }
   ```
   
   plus a `NESTED_LOCATION` constant and `"nested"` in `containsOnlyKeys` with 
its own assertion.



##########
log4j-core/src/main/java/org/apache/logging/log4j/core/async/AsyncLogger.java:
##########
@@ -134,7 +133,7 @@ private RingBufferLogEventTranslator getCachedTranslator() {
     @Override
     public void logMessage(
             final String fqcn, final Level level, final Marker marker, final 
Message message, final Throwable thrown) {
-        getTranslatorType().log(fqcn, level, marker, message, thrown);
+        getTranslatorType().log(fqcn, calcLocationIfRequested(fqcn), level, 
marker, message, thrown);

Review Comment:
   `ExtendedLoggerWrapper` reaches this entry, because its `requiresLocation()` 
is `false`. The async location tests all go through `log(…, location, …)`, so 
this line has no test. Can `AsyncLoggerNullLocationTest` get a case that logs 
through an `ExtendedLoggerWrapper` and checks the source on the enqueue route?



##########
src/changelog/.2.x.x/4470_async_logger_queue_full_location.xml:
##########
@@ -0,0 +1,12 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<entry xmlns="https://logging.apache.org/xml/ns";
+       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
+       xsi:schemaLocation="
+           https://logging.apache.org/xml/ns
+           https://logging.apache.org/xml/ns/log4j-changelog-0.xsd";
+       type="fixed">
+    <issue id="4470" 
link="https://github.com/apache/logging-log4j2/pull/4470"/>
+    <description format="asciidoc">
+        Keep the caller location of events that asynchronous loggers log 
synchronously, because the ring buffer is full.

Review Comment:
   Shorter line for the release notes:
   
   ```suggestion
           Keep the async logger location when the ring buffer is full.
   ```



-- 
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.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to