Replies: 6 comments
-
Is it alright if I create a suggested PR for this? I would really rather avoid the Apache "mailing list" stuff, though... |
Beta Was this translation helpful? Give feedback.
-
@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
IMHO, you're misinterpreting the docs. It says
Generally speaking, |
Beta Was this translation helpful? Give feedback.
-
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. 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. Note also that SLF4J's standard implementation (Logback) only supports a single logger context (see Remark: I chose Hibernate as an example. Pick any implementation of a Jakarta specification and you'll find similar problems. |
Beta Was this translation helpful? Give feedback.
-
Thanks for having a look. Please feel free to close, and thank you for the explanation. |
Beta Was this translation helpful? Give feedback.
-
As @vy explained, the expensive The trick of using instance loggers is to manage the cost of 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 The choice of using instance loggers implies however other limitations to your library. You can not use logging in classical "utility" classes: 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 public final class MyUtil {
public static String transform(String value, Logger log) {
log.debug("Transforming {}.", value);
} or replace static methods with instance methods: 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 |
Beta Was this translation helpful? Give feedback.
-
While I appreciate what the Logger Context is trying to accomplish, I have to say that adding such a heavy initialization time to instances is not ok, at least not for me (fyi, we are getting 2.8s extra time per instance just from the StackWalker). I could sort of justify this cost (to myself) if this logic was needed only on the very first instance being created for a particular logger class -- similar to what happens with the static approach. It is nice that there is a setting to change the logic to a single context, but then (1) the abstraction I get from using Slf4J API is meaningless and (2) it becomes awkward for a library to suggest this setting on the application using it in case it is backed by Log4J. Having said this, I unfortunately don't really have a solution to propose here that wouldn't potentially break the idea behind this, so I feel like I accidentally just came here to complain -- Sorry for that, my initial look at the code really made it seem like it was simple tweak. |
Beta Was this translation helpful? Give feedback.
-
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:
Following the change of heart on slf4j#declared_static, I expected that the following is just as valid of a use case (which works, but does not perform well):
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 (invocation coming from AbstractLoggerAdapter.java#L46 ):
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?Beta Was this translation helpful? Give feedback.
All reactions