kalayciburak commented on code in PR #4340: URL: https://github.com/apache/logging-log4j2/pull/4340#discussion_r4081828428
########## log4j-core-test/src/test/java/org/apache/logging/log4j/core/async/AsyncLoggerNullLocationTest.java: ########## @@ -0,0 +1,85 @@ +/* + * 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 org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +import org.apache.logging.log4j.Level; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.core.LogEvent; +import org.apache.logging.log4j.core.LoggerContext; +import org.apache.logging.log4j.core.config.ConfigurationFactory; +import org.apache.logging.log4j.core.test.appender.ListAppender; +import org.apache.logging.log4j.core.test.junit.Tags; +import org.apache.logging.log4j.core.util.Constants; +import org.apache.logging.log4j.message.SimpleMessage; +import org.apache.logging.log4j.spi.AbstractLogger; +import org.apache.logging.log4j.util.Strings; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Tag; +import org.junit.jupiter.api.Test; + +/** + * AsyncLogger must compute caller location when {@code includeLocation} is enabled and the + * caller passes a null location, matching {@code logMessage(fqcn, level, marker, message, thrown)}. + */ +@Tag(Tags.ASYNC_LOGGERS) +class AsyncLoggerNullLocationTest { + + @BeforeAll + static void beforeClass() { + System.setProperty(Constants.LOG4J_CONTEXT_SELECTOR, AsyncLoggerContextSelector.class.getName()); + System.setProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY, "AsyncLoggerNullLocationTest.xml"); + } + + @AfterAll + static void afterClass() { + System.setProperty(Constants.LOG4J_CONTEXT_SELECTOR, Strings.EMPTY); + System.clearProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY); + } + + @Test + void nullLocationIsComputedWhenIncludeLocationIsEnabled() { + final LoggerContext context = (LoggerContext) LogManager.getContext(false); + final ListAppender app = context.getConfiguration().getAppender("List"); + assertNotNull(app); + final AsyncLogger logger = (AsyncLogger) context.getLogger("com.foo.Bar"); + final String fqcn = AbstractLogger.class.getName(); + + logger.logMessage(Level.INFO, null, fqcn, null, new SimpleMessage("null-location"), null); Review Comment: yes, first event goes through atInfo().log now ########## src/changelog/.2.x.x/1458_async_logger_null_location.xml: ########## @@ -0,0 +1,10 @@ +<?xml version="1.0" encoding="UTF-8"?> +<entry xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xmlns="https://logging.apache.org/xml/ns" + xsi:schemaLocation="https://logging.apache.org/xml/ns https://logging.apache.org/xml/ns/log4j-changelog-0.xsd" + type="fixed"> + <issue id="1458" link="https://github.com/apache/logging-log4j2/issues/1458"/> Review Comment: added 4340 ########## log4j-core/src/main/java/org/apache/logging/log4j/core/async/AsyncLogger.java: ########## @@ -145,7 +145,10 @@ public void log( final StackTraceElement location, final Message message, final Throwable throwable) { - getTranslatorType().log(fqcn, location, level, marker, message, throwable); + // A null location means "not yet computed", not "caller opted out". + // logMessage(fqcn, ...) already fills this in; the location-bearing entry point must too. Review Comment: removed -- 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]
