blaghed opened a new issue, #2720:
URL: https://github.com/apache/logging-log4j2/issues/2720
When integrating Slf4J with Log4J, I make use of the `log4j-slf4j-impl`
library.
Then, typically, when logging something this will be the classical approach
used:
```java
public class MyApp {
private static final org.slf4j.Logger log =
org.slf4j.LoggerFactory.getLogger(MyApp.class);
public void doSomething() {
log.info("Hello World!");
}
}
```
Following the change of heart on
[slf4j#declared_static](https://slf4j.org/faq.html#declared_static), I expected
that the following is just as valid of a use case (which _works_, but does not
perform well):
```java
public class MyApp {
public void doSomething(String ctx) {
final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(ctx);
log.info("Hello World!");
}
}
```
Checking the integration code on Log4J side, it seems like there are
appropriate caches on all expected places to deal with this, except in
[Log4jLoggerFactory.java#L53-L59](https://github.com/apache/logging-log4j2/blob/rel/2.23.1/log4j-slf4j-impl/src/main/java/org/apache/logging/slf4j/Log4jLoggerFactory.java#L52-L59)
(invocation coming from
[AbstractLoggerAdapter.java#L46](https://github.com/apache/logging-log4j2/blob/rel/2.23.1/log4j-api/src/main/java/org/apache/logging/log4j/spi/AbstractLoggerAdapter.java#L46)
):
```java
@Override
protected LoggerContext getContext() {
final Class<?> anchor =
LogManager.getFactory().isClassLoaderDependent()
? StackLocatorUtil.getCallerClass(Log4jLoggerFactory.class,
CALLER_PREDICATE)
: null;
LOGGER.trace("Log4jLoggerFactory.getContext() found anchor {}",
anchor);
return anchor == null ? LogManager.getContext(false) :
getContext(anchor);
}
```
In particular, the `StackLocatorUtil.getCallerClass` call is what causes the
performance degradation.
Would it be feasible to add a cache here as well, so the `anchor` is
remembered between calls?
--
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]