You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@logging.apache.org by "blaghed (via GitHub)" <gi...@apache.org> on 2024/07/08 14:11:02 UTC
[I] Cache LoggerContext in log4j-slf4j-impl (logging-log4j2)
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: notifications-unsubscribe@logging.apache.org.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [I] Cache LoggerContext in log4j-slf4j-impl (logging-log4j2)
Posted by "vy (via GitHub)" <gi...@apache.org>.
vy commented on issue #2720:
URL: https://github.com/apache/logging-log4j2/issues/2720#issuecomment-2215111164
@blaghed, good news is, you don't need to deal with _the mailing list stuff_. We would be more than happy to assist you here.
Can you explain what kind of a cache you see others implement (references please) and `log4j-slf4j-impl` is missing? That is, to create a `Logger`, we need a `LoggerContext`. To locate that, we need to know the enclosing class of the `getLogger()` call, and this is where `StackLocatorUtil.getCallerClass()` comes in.
> Following the change of heart on [slf4j#declared_static](https://slf4j.org/faq.html#declared_static)...
IMHO, you're misinterpreting the docs. It says `instance variables`. Your example is not an instance variable, but a `method variable`. This is an instance variable:
```
public class MyApp {
// Note the missing `static` modifier
private final String foo = "bar";
}
```
Generally speaking, `Logger`s are not cheap constructs, they should ideally be reused and kept in a sufficiently long-living scope.
--
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: notifications-unsubscribe@logging.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [I] Cache LoggerContext in log4j-slf4j-impl (logging-log4j2)
Posted by "vy (via GitHub)" <gi...@apache.org>.
vy closed issue #2720: Cache LoggerContext in log4j-slf4j-impl
URL: https://github.com/apache/logging-log4j2/issues/2720
--
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: notifications-unsubscribe@logging.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [I] Cache LoggerContext in log4j-slf4j-impl (logging-log4j2)
Posted by "ppkarwasz (via GitHub)" <gi...@apache.org>.
ppkarwasz commented on issue #2720:
URL: https://github.com/apache/logging-log4j2/issues/2720#issuecomment-2215258488
The idea behind instance loggers is a nice one: if an application server shares your library between applications, instance loggers should allow you to log messages to the correct application's log file.
Unfortunately **many** (most?) libraries don't use instance loggers and the list also includes libraries, which are **meant to be shared** between applications.
For example Hibernate's [`EntityManagerImpl`](https://github.com/manuelbernhardt/hibernate-core/blob/master/hibernate-entitymanager/src/main/java/org/hibernate/ejb/EntityManagerImpl.java) and [`SessionFactoryImpl`](https://github.com/manuelbernhardt/hibernate-core/blob/master/hibernate-core/src/main/java/org/hibernate/impl/SessionFactoryImpl.java), which are objects specific to each application, use **static** loggers and send the logs of all applications to server's global logger context.
Since Hibernate is the JPA implementation in many application servers, system administrators are used to the fact that logs specific to a given application are scattered among multiple log files.
Therefore they have no reason to configure per-application logger contexts and might as well use a single logger context.
Note also that SLF4J's standard implementation (Logback) only supports a single logger context (see [`LoggerContext`](https://github.com/qos-ch/logback/blob/master/logback-classic/src/main/java/ch/qos/logback/classic/LoggerContext.java)).
**Remark**: I chose Hibernate as an example. Pick any implementation of a Jakarta specification and you'll find similar problems.
--
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: notifications-unsubscribe@logging.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [I] Cache LoggerContext in log4j-slf4j-impl (logging-log4j2)
Posted by "ppkarwasz (via GitHub)" <gi...@apache.org>.
ppkarwasz commented on issue #2720:
URL: https://github.com/apache/logging-log4j2/issues/2720#issuecomment-2217954069
@blaghed,
> After receiving your feedback and going again through the mentioned code, I can't really find a nice way to "cache" the `LoggerContext` in order to prevent the expensive `StackWalker` to always be called, at least not without some hacky approach. Indeed, logback doesn't have this issue, so I should simply use that one instead.
As @vy explained, the expensive `StackWalker` call is necessary to return a different `Logger` instance depending on the caller of `LoggerFactory#getLogger()`. If you eliminate the call (e.g. by switching to Logback) you lose **all advantages** that instance loggers have over static loggers.
The trick of using instance loggers is to manage the cost of `Logger` creation by assigning the logger to a field:
```java
public class MyService {
private final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(MyApp.class);
public void doSomething() {
log.info("Hello World!");
}
}
```
This way if both application `A` and application `B` instantiate `MyService`, they could get different `Logger` instances, configured to log to different appenders.
The choice of using instance loggers implies however other limitations to your library. You can not use logging in classical "utility" classes:
```java
public final class MyUtil {
private static final Logger log = LoggerFactory.getLogger(MyUtil.class);
public static String transform(String value) {
log.debug("Transforming {}.", value);
}
```
You need to either to pass `Logger` as parameter to the utility class:
```java
public final class MyUtil {
public static String transform(String value, Logger log) {
log.debug("Transforming {}.", value);
}
```
or replace static methods with instance methods:
```java
public final class MyUtil {
private final Logger log = LoggerFactory.getLogger(MyUtil.class);
public String transform(String value) {
log.debug("Transforming {}.", value);
}
```
**Remark**: If you want to emulate Logback's behavior, you can also set the [`log4j2.contextSelector` configuration property](https://logging.staged.apache.org/log4j/2.x/manual/systemproperties.html#log4j2.contextSelector) to `org.apache.logging.log4j.core.selector.BasicContextSelector`. There will be a single logger context for the entire application, so the caller lookup will not be necessary.
--
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: notifications-unsubscribe@logging.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [I] Cache LoggerContext in log4j-slf4j-impl (logging-log4j2)
Posted by "blaghed (via GitHub)" <gi...@apache.org>.
blaghed commented on issue #2720:
URL: https://github.com/apache/logging-log4j2/issues/2720#issuecomment-2217835213
Thanks for having a look.
After receiving your feedback and going again through the mentioned code, I can't really find a nice way to "cache" the `LoggerContext` in order to prevent the expensive `StackWalker` to always be called, at least not without some hacky approach.
Indeed, logback doesn't have this issue, so I should simply use that one instead.
Please feel free to close, and thank you for the explanation.
--
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: notifications-unsubscribe@logging.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [I] Cache LoggerContext in log4j-slf4j-impl (logging-log4j2)
Posted by "blaghed (via GitHub)" <gi...@apache.org>.
blaghed commented on issue #2720:
URL: https://github.com/apache/logging-log4j2/issues/2720#issuecomment-2214291396
Is it alright if I create a suggested PR for this? I would really rather avoid the Apache "mailing list" stuff, though...
Worth noting that some of the other integrations on this project follow the same pattern, so would be nice to get a confirmation for this idea, or if Caching here is somehow against some other idea.
--
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: notifications-unsubscribe@logging.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org