Skip to content

Commit

Permalink
[#11818] Improve log4j context initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
emeroad committed Feb 3, 2025
1 parent 2dff5c3 commit b055f08
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
6 changes: 6 additions & 0 deletions agent-module/profiler-logging/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@
<groupId>com.navercorp.pinpoint</groupId>
<artifactId>pinpoint-bootstrap-core</artifactId>
</dependency>
<dependency>
<groupId>org.jspecify</groupId>
<artifactId>jspecify</artifactId>
<scope>provided</scope>
<version>1.0.0</version>
</dependency>

<!-- Logging dependencies -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.navercorp.pinpoint.profiler.logging;

import org.apache.logging.log4j.spi.NoOpThreadContextMap;
import org.apache.logging.log4j.spi.Provider;

public class Log4j2Provider extends Provider {
public Log4j2Provider() {
super(11, CURRENT_VERSION, Log4j2ContextFactory.class, NoOpThreadContextMap.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,29 @@

public class Log4jEnvExecutor {

/**
* log4j 2.24
* ref : {@value org.apache.logging.log4j.spi.Provider#PROVIDER_PROPERTY_NAME}
*/
public static final String PROVIDER_PROPERTY_NAME = "log4j.provider";
public static final String FACTORY_PROPERTY_NAME = "log4j2.loggerContextFactory";

public <V> V call(Supplier<V> supplier) {
final String factory = prepare(FACTORY_PROPERTY_NAME, Log4j2ContextFactory.class.getName());
final String provider = prepare(PROVIDER_PROPERTY_NAME, Log4j2Provider.class.getName());
final String factory = prepare(FACTORY_PROPERTY_NAME, null);
try {
return supplier.get();
} finally {
rollback(FACTORY_PROPERTY_NAME, factory);
rollback(PROVIDER_PROPERTY_NAME, provider);
}
}

private String prepare(String key, String value) {
final String backup = System.getProperty(key);
System.setProperty(key, value);
if (value != null) {
System.setProperty(key, value);
}
return backup;
}

Expand Down

0 comments on commit b055f08

Please sign in to comment.