-
Notifications
You must be signed in to change notification settings - Fork 2.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
What about being able to inject a Logger? #13510
Comments
FYI I did a little experiment with simplifying logging using bytecode transformation. Basically, transform The experiment is here: https://github.com/Ladicek/quarkus/commits/logging-experiment It's crude, because it scans all classes in the application (!) and the bytecode transformation only works properly for methods with up to 2 arguments. But it works :-) I guess it could be done properly (perhaps limit the transformation to classes with some annotation?), but I didn't want to spend too much time on it. |
Ah now I know where I've seen this idea! I think @FroMage proposed basically the same thing a while ago? |
Haha, yes!!! And it depends on smallrye/jandex#85 to be efficient. |
It's #10929 but from what you describe it's the same thing, so +1 |
I found a constant pool scanning utility in the bytecode transformation code, so I used it, but agree it would be even better to have that info in Jandex. |
So here's a complete solution for "injectable logger": https://github.com/mkouba/quarkus/tree/injectable-logger @ApplicationScoped
class SimpleBean {
@Inject
Logger log; // org.jboss.logging.Logger.getLogger(SimpleBean.class)
@LoggerName("shared")
Logger shared; // org.jboss.logging.Logger.getLogger("shared")
} The impl is very straightforward (and lacks any transformation magic) but of course there are some drawbacks (mentioned below). pros:
cons:
I'm not sure whether it would make sense to provide both the CDI-based and the static-transformation solutions... |
@mkouba what about having both (injection and used in static method) ? If we change the naming so it "looks" the same, we could have:
Of course it's complitely different things, but because |
Maybe. I can update my branch to reflect this proposal. |
Updated. You can do @ApplicationScoped
class SimpleBean {
private final Logger log; // org.jboss.logging.Logger.getLogger(SimpleBean.class)
SimpleBean(Logger log) {
this.log = log;
}
} |
Beautiful ! |
@Ladicek It's not really scanning in the sense that you have to scan every class to find the ones that have the constant, which is why I did this PR. If we can merge it, we can implement this by not scanning every class so it's reasonably efficient. Otherwise +1 on having an annotation to get a logger injected, but @agoncal we can't use the same class name for the annotation and the logger utility with static methods. Which means we need to find two names that go hand in hand. @Ladicek we should continue discussion of the static methods on #10929 |
Yea I agree we need to figure out nice names. Because I totally want the "simplified" calls to be |
That's a good point. Hm, |
Indeed Can we assume that the named variant is a lot less common? All the examples I've seen use the current class name for the category. If it is indeed less common, then |
Well, it's very like a lot less common if you use FQCN-based loggers everywhere. However, if you e.g. use interface-based loggers it's common to define certain parts of your app and group the log messages (but then you would probably not need an injected logger ;-).
This woks for qualifiers out of the box in quarkus ;-). |
I quite like the As for the other choice, maybe that's an "English" bias, but for me "log" is the trace in it self, as "logger" is the all thing. So for me |
I think I did wonder about using |
Ok, then |
- resolves quarkusio#13510
Description
Loggers are everywhere and Quarkus even supports several ones. So why not being able to inject a logger?
This issue was created from a discussion in Zulip.
Implementation ideas
Today we use static loggers:
But we could also be able to inject a logger:
Or even use a qualifier and remove the
@Inject
(using the same style of@ConfigProperty
) and have:As @mkouba mentions on Zulip, we could have a producer but "the problem with this approach is that the bean must be @dependent in order to be able to access the injection point metadata:
There is also a performance impact as injection happens at runtime. So "we could cache the loggers to avoid the perf penalty"
The text was updated successfully, but these errors were encountered: