rmannibucau commented on PR #793:
URL: https://github.com/apache/maven/pull/793#issuecomment-1631926675
@gnodet let's ignore a second the classloading errors and take a simple
functional case: you want logger X to be logged particularly and the app knows
the provider it uses so can configure it directly - this is a common case as we
discussed on slack. I'll use JUL binding for the example but it is not rare it
is logback or log4j2 too.
Do we want to say to users "no, will never work until you implement another
classloading abstraction to isolate yourself from plugin ecosystem"? Not on my
side.
Here is a simple mojo showing it:
```pom.xml
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.36</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-jdk14</artifactId>
<version>1.7.36</version>
</dependency>
```
```java
@Mojo(name = "demo", defaultPhase = NONE)
public class MyMojo extends AbstractMojo {
@Override
public void execute() {
run();
}
private void run() {
final var loggerName = getClass().getName();
final var logger = Logger.getLogger(loggerName);
final var delegate = Logger.getLogger("");
logger.addHandler(new Handler() {
@Override
public void publish(final LogRecord record) { // just a hack to
get console logger but not close it
delegate.getHandlers()[0].publish(record);
}
@Override
public void flush() {
// no-op
}
@Override
public void close() throws SecurityException {
// no-op
}
});
LoggerFactory.getLogger(loggerName)
.info("I am here");
}
}
```
Indeed you have to assume the logging configuration is not part of the
execute method but a transitive one - a bit like spring logging system.
Running this mojo we get:
```
[INFO] --- demo:0.0.1-SNAPSHOT:demo (run) @ consumer ---
[INFO] I am here
```
Which is not really the expectation:
```
juil. 12, 2023 8:24:12 AM com.github.rmannibucau.maven.reproducer.Main main
INFO: I am in xml
juil. 12, 2023 8:24:12 AM com.github.rmannibucau.maven.reproducer.Main main
INFO: I am in xml
```
So we can see that the format is wrong - which can be an issue for downside
parsing, the plugin will not enable to test grokking for ex, but the setup is
not even respected in maven (the duplicated log is an intentional coding bug
but does not surface with maven). Worse, maven ignores
`logger.setUseParentHandlers(false);` code - which is intended to fix the
duplicated line.
So maven does not enable to do any log related work there.
So I really think we should give the control of that to mojo:
* slf4j can be imported or not in the limit of the strict API - not the SPI
* JUL should likely be isolated like in tomcat per mojo - default = inherit
but a toggle would enable to let the plugin use its own config/custom logging
manager if the system prop is set there (by delegation we can do it)
The last trick we'll see - since we spoke of slf4j 2 - is that it does not
use its classloading custom lookup anymore for the logger factory but a plain
ServiceLoader JPMS based so ClassRealm is not ready for that at all. Guess
we'll need to work on that at some point so I don't think we'll need a
workaround in our SLF4J impl to delegate to plugin impl if any but means we
will still have some issues with slf4j.
Hope it helps
--
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]