-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
) This is a port of #3199 to the 2.24.x branch. Removes weak references to `Logger`s in `LoggerRepository`. The usage of weak references in `LoggerRepository` might cause `null` to be returned by `LogManager.getLogger()` of all Log4j Core versions up to `2.24.1`. Versions of Log4j API up to `2.24.0` did hold **hard** references to all the registered loggers, so the change will not alter the previous behavior. This PR also inverts the order of the `String` and `MessageFactory` keys to the `LoggerRepository` multi-map to limit the number of internal maps. The external map is a `WeakHashMap` to allow logger-specific message factories to be GC-ed. Closes #3143. Co-authored-by: Volkan Yazıcı <[email protected]>
- Loading branch information
Showing
7 changed files
with
310 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
log4j-api-test/src/test/java/org/apache/logging/log4j/spi/LoggerRegistryTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* 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.spi; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import java.lang.ref.WeakReference; | ||
import java.util.stream.Stream; | ||
import org.apache.logging.log4j.message.MessageFactory; | ||
import org.apache.logging.log4j.message.ParameterizedMessageFactory; | ||
import org.apache.logging.log4j.message.ReusableMessageFactory; | ||
import org.apache.logging.log4j.test.TestLogger; | ||
import org.jspecify.annotations.Nullable; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
|
||
class LoggerRegistryTest { | ||
|
||
private static final String LOGGER_NAME = LoggerRegistryTest.class.getName(); | ||
|
||
static Stream<@Nullable MessageFactory> doesNotLoseLoggerReferences() { | ||
return Stream.of( | ||
ParameterizedMessageFactory.INSTANCE, | ||
ReusableMessageFactory.INSTANCE, | ||
new ParameterizedMessageFactory(), | ||
null); | ||
} | ||
|
||
/** | ||
* @see <a href="https://github.com/apache/logging-log4j2/issues/3143>Issue #3143</a> | ||
*/ | ||
@ParameterizedTest | ||
@MethodSource | ||
void doesNotLoseLoggerReferences(@Nullable MessageFactory messageFactory) { | ||
LoggerRegistry<TestLogger> loggerRegistry = new LoggerRegistry<>(); | ||
TestLogger logger = new TestLogger(LOGGER_NAME, messageFactory); | ||
WeakReference<TestLogger> loggerRef = new WeakReference<>(logger); | ||
// Register logger | ||
loggerRegistry.putIfAbsent(LOGGER_NAME, messageFactory, logger); | ||
// The JIT compiler/optimizer might figure out by himself the `logger` and `messageFactory` are no longer used: | ||
// https://shipilev.net/jvm/anatomy-quarks/8-local-var-reachability/ | ||
// We help him with the task though. | ||
logger = null; | ||
// Trigger a GC run | ||
System.gc(); | ||
// Check if the logger is still there | ||
assertThat(loggerRef.get()).isNotNull(); | ||
assertThat(loggerRegistry.getLogger(LOGGER_NAME, messageFactory)).isInstanceOf(TestLogger.class); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.