Skip to content
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

Use the SLF4J logging API directly #13285

Merged
merged 11 commits into from
Mar 27, 2023
Merged

Use the SLF4J logging API directly #13285

merged 11 commits into from
Mar 27, 2023

Conversation

chrisvest
Copy link
Contributor

Motivation:
SLF4J has been the defacto standard logging API in the Java world for a while. Normally we don't like to expose vendor APIs, but I think we can make an exception in this case. By using it directly, we can get rid of our own logging API code that isn't inherently relevant to what Netty does. It also means we can more easily make use of more advanced logging features like MDC and LoggingEventBuilders.

Modification:
Remove our internal logging API and use SLF4J everywhere. Switch back from log4j to using logback for our testing.

Result:
Less code to maintain, and everyone gets to use the same logging API.

Motivation:
SLF4J has been the defacto standard logging API in the Java world for a while.
Normally we don't like to expose vendor APIs, but I think we can make an exception in this case.
By using it directly, we can get rid of our own logging API code that isn't inherently relevant to what Netty does.
It also means we can more easily make use of more advanced logging features like MDC and LoggingEventBuilders.

Modification:
Remove our internal logging API and use SLF4J everywhere.
Switch back from log4j to using logback for our testing.

Result:
Less code to maintain, and everyone gets to use the same logging API.
@chrisvest
Copy link
Contributor Author

Draft because the Graal meta-data needs to be updated as well.

@jchrys
Copy link
Contributor

jchrys commented Mar 20, 2023

Thank you for this PR! I love this change.
while reviewing the changes, I noticed that some license-related files might need to be modified, as it seems that we're no longer dependent on log4j.
Thanks very much!

example/src/main/resources/logback.xml Show resolved Hide resolved
resolver-dns/src/test/resources/logback-test.xml Outdated Show resolved Hide resolved
@@ -81,7 +80,7 @@ public class EpollReuseAddrTest {
BUGFIX = 0;
}
} else {
LOGGER.log(InternalLogLevel.INFO, "Unable to parse kernel version: " + kernelVersion);
LOGGER.info("Unable to parse kernel version: {}", kernelVersion);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do you sometimes use atInfo()... and sometimes the above pattern ? Imho we should be consistent. I suspect the pattern above creates less GC as there is no extra builder created.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The atLevel() method is the only way to dynamically specify the log level in their 2.0 API. There were two places where I used atWarn(), and I'll fix those.

This is not the same as regenerating the reflection configuration, which needs to happen, but let's see what the build says about it first.
@chrisvest
Copy link
Contributor Author

Hmm… the h2spec testsuite runs inside the Maven process itself, and Maven depends on slf4j 1.7, with no apparent way override this in the plugin classloader. That's why that module fails with "no such method error".

…he correct classpath

The h2spec-maven-plugin runs the server with the Maven-internal classloader as the parent classloader, which means we cannot override the slf4j version that Maven otherwise uses.

By dropping the parent classloader, and only relying on our own URLClassLoader, we get the correct test-classpath that we want.
@chrisvest chrisvest marked this pull request as ready for review March 23, 2023 00:26
@chrisvest
Copy link
Contributor Author

Hmm… the h2spec testsuite runs inside the Maven process itself, and Maven depends on slf4j 1.7, with no apparent way override this in the plugin classloader. That's why that module fails with "no such method error".

Managed to work around this with a bit of class loader magic.


/**
* Maps the regular {@link LogLevel}s with the {@link InternalLogLevel} ones.
* Logging level for configuring {@link LoggingHandler}.
*/
public enum LogLevel {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But shouldn't we make it package-private and hide it from the public API?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was the whole point to make this public and so "hide" slf4j from the public API .

@@ -46,38 +46,38 @@ public void queryWritten(InetSocketAddress dnsServerAddress, Future<Void> future
@Override
public void queryCancelled(int queriesRemaining) {
if (dnsServerAddress != null) {
logger.log(level, "from {} : {} cancelled with {} queries remaining", dnsServerAddress, question,
logger.atLevel(level).log("from {} : {} cancelled with {} queries remaining", dnsServerAddress, question,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could extract logger.atLevel(level) into a member field so we don't have to call it every time. I guess this could be potentially creating a new instance for each log message.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The API is meant to build a new LogEvent every time. Otherwise you'll carry over data from previous log messages. For instance, the arguments (dnsServerAddress, etc.) given here will accumulate.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we cache the builder we will also not be told about runtime log level changes.

testsuite-http2/pom.xml Show resolved Hide resolved
@trustin
Copy link
Member

trustin commented Mar 23, 2023 via email

@normanmaurer
Copy link
Member

Hmm? Why hide SLF4J from the public API? We pull it in as a non-shaded
dependency and it's already the de facto standard logging facade in Java ecosystem.

imho it is far better to just have it as an internal dependency then leaking it to the public api.

@chrisvest
Copy link
Contributor Author

@normanmaurer @trustin I've addressed your comments. Please take a look.

@chrisvest chrisvest merged commit 795db4a into netty:main Mar 27, 2023
@chrisvest chrisvest deleted the 5x-slf4j branch March 27, 2023 16:26
@violetagg
Copy link
Member

@chrisvest @normanmaurer There is a bit of inconsistency now in the API

The LoggingHandler works with LogLevel and thus the slf4j is not exposed to the public API
But for example Http2FrameLogger and LoggingDnsQueryLifeCycleObserverFactory are exposing directly slf4j

Are you going at some point to expose slf4j everywhere and remove LogLevel?

pderop added a commit to reactor/reactor-netty that referenced this pull request Mar 28, 2023
Adaptations made for the Netty5 SLF4J netty/netty#13285 PR.

socks-proxy and codec-multipart netty contrib projects have also been updated, because io.netty5.util.internal.logging.InternalLoggerFactory is not supported anymore.
@chrisvest
Copy link
Contributor Author

@violetagg Oops, looks like we didn't revert all the usages. I'll make a PR.

chrisvest added a commit to chrisvest/netty that referenced this pull request Mar 28, 2023
Motivation:
The is _a lot_ going on in netty#13285, and we missed that we exposed the SLF4J level in a couple of places.
We also _somehow_ missed that we broke compilation of EchoIT in the buffer-memory-segment module.

Modification:
Replace the remaining places that exposed SLF4J Level to the public API, and fix the compilation.

Result:
No more left-overs to clean up after netty#13285
@chrisvest
Copy link
Contributor Author

This should fix it: #13304

chrisvest added a commit that referenced this pull request Mar 29, 2023
Motivation:
The is _a lot_ going on in #13285, and we missed that we exposed the SLF4J level in a couple of places.
We also _somehow_ missed that we broke compilation of EchoIT in the buffer-memory-segment module.

Modification:
Replace the remaining places that exposed SLF4J Level to the public API, and fix the compilation.

Result:
No more left-overs to clean up after #13285

Co-authored-by: Norman Maurer <norman_maurer@apple.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants