question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`Slf4JLoggerFactory` does not initialize when using no-op logger

See original GitHub issue

Below 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

  1. Create a new project with the org.slf4j:slf4j-api:1.7.32 and io.netty:netty-all:4.1.71.Final dependencies
  2. Add the reproducer code from below
  3. 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:closed
  • Created 2 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
laosijikaichelecommented, Dec 16, 2021
1reaction
yawkatcommented, Dec 14, 2021

Sure, just may take a bit before I get the CLA approval, and because of holidays.

Read more comments on GitHub >

github_iconTop Results From Across the Web

SLF4J Error Codes
This IllegalStateException is thrown post-initialization and informs the user that initialization of LoggerFactory has failed. Note It is important to realize ...
Read more >
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder"
slf4j.impl.StaticLoggerBinder class could not be loaded into memory. This happens when no appropriate SLF4J binding could be found on the class ...
Read more >
How to Configure SLF4J with Different Logger Implementations
bash> java deng.Hello SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder". SLF4J: Defaulting to no-operation (NOP) logger ...
Read more >
Logging in Spring Boot with SLF4J - Stack Abuse
To enable logging in Spring, import Logger and LoggerFactory from the org.slf4j API library: import org.slf4j.Logger; import org.slf4j.
Read more >
AWS Lambda function logging in Java
SLF4J is a facade library for logging in Java code. In your function code, you use the SLF4J logger factory to retrieve a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found