`Slf4JLoggerFactory` does not initialize when using no-op logger
See original GitHub issueBelow is the full issue description. Here is a short description of why this happens:
Looking at the code, Slf4JLoggerFactory.INSTANCE
should not care whether the slf4j implementation is a NOPLoggerFactory
. The package-private INSTANCE_WITH_NOP_CHECK
has that additional check, but INSTANCE
does not. However, because the INSTANCE_WITH_NOP_CHECK
is a static field, the nop check failing leads to the entire Slf4JLoggerFactory
class failing to initialize, leading to the error below.
My suggestion would be to either revert the change that made the nop check instance a singleton (#11253), or (probably preferred) to move the singleton to a nested class so that the failure does not affect the outer class.
I can make a PR to fix this issue, however I will have to get new CLA approval since I changed employment since I last signed the CLA, which may take a few days.
Expected behavior
InternalLoggerFactory.setDefaultFactory(Slf4JLoggerFactory.INSTANCE);
should always succeed.
Actual behavior
When slf4j uses the noop logger, the code throws an exception:
Exception in thread "main" java.lang.NoClassDefFoundError: NOPLoggerFactory not supported
at io.netty.util.internal.logging.Slf4JLoggerFactory.<init>(Slf4JLoggerFactory.java:45)
at io.netty.util.internal.logging.Slf4JLoggerFactory.<clinit>(Slf4JLoggerFactory.java:33)
at Main.main(Main.java:6)
Steps to reproduce
- Create a new project with the
org.slf4j:slf4j-api:1.7.32
andio.netty:netty-all:4.1.71.Final
dependencies - Add the reproducer code from below
- Run the main class
Minimal yet complete reproducer code (or URL to code)
import io.netty.util.internal.logging.InternalLoggerFactory;
import io.netty.util.internal.logging.Slf4JLoggerFactory;
public class Main {
public static void main(String[] args) {
InternalLoggerFactory.setDefaultFactory(Slf4JLoggerFactory.INSTANCE);
}
}
Netty version
4.1.71.Final
JVM version (e.g. java -version
)
Adopt OpenJDK 11.0.12
OS version (e.g. uname -a
)
Linux yawkat-oracle 5.13.0-22-generic #22-Ubuntu SMP Fri Nov 5 13:21:36 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux
Issue Analytics
- State:
- Created 2 years ago
- Comments:5 (5 by maintainers)
Top GitHub Comments
@yawkat I created a PR: https://github.com/netty/netty/pull/11931.
Sure, just may take a bit before I get the CLA approval, and because of holidays.