From 0bbe4ce9fd2c531a777bdfa3e0d8bfcd1e93768a Mon Sep 17 00:00:00 2001 From: Chris Vest Date: Wed, 19 Aug 2020 20:22:38 +0200 Subject: [PATCH] Expose a LoggingDnsQueryLifeCycleObserverFactory (#10490) Expose a LoggingDnsQueryLifeCycleObserverFactory Motivation: There is a use case for having logging in the DnsNameResolver, similar to the LoggingHandler. Previously, one could set `traceEnabled` on the DnsNameResolverBuilder, but this is not very configurable. Specifically, the log level and the logger context cannot be changed. Modification: Expose a LoggingDnsQueryLifeCycleObserverFactory, that permit changing the log-level and logger context. Result: It is now possible to get logging in the DnsNameResolver at a custom log level and logger, without very much effort. Fixes #10485 --- resolver-dns/pom.xml | 5 ++ .../netty/resolver/dns/DnsNameResolver.java | 4 +- .../resolver/dns/DnsNameResolverBuilder.java | 3 + ...ggingDnsQueryLifeCycleObserverFactory.java | 85 +++++++++++++++++++ ... => LoggingDnsQueryLifecycleObserver.java} | 4 +- ...TraceDnsQueryLifeCycleObserverFactory.java | 45 ---------- 6 files changed, 97 insertions(+), 49 deletions(-) create mode 100644 resolver-dns/src/main/java/io/netty/resolver/dns/LoggingDnsQueryLifeCycleObserverFactory.java rename resolver-dns/src/main/java/io/netty/resolver/dns/{TraceDnsQueryLifecycleObserver.java => LoggingDnsQueryLifecycleObserver.java} (93%) delete mode 100644 resolver-dns/src/main/java/io/netty/resolver/dns/TraceDnsQueryLifeCycleObserverFactory.java diff --git a/resolver-dns/pom.xml b/resolver-dns/pom.xml index 0925c9f47d..84e1733a79 100644 --- a/resolver-dns/pom.xml +++ b/resolver-dns/pom.xml @@ -63,6 +63,11 @@ netty-codec-dns ${project.version} + + ${project.groupId} + netty-handler + ${project.version} + org.apache.directory.server apacheds-protocol-dns diff --git a/resolver-dns/src/main/java/io/netty/resolver/dns/DnsNameResolver.java b/resolver-dns/src/main/java/io/netty/resolver/dns/DnsNameResolver.java index 344be375c6..eb3b8046c6 100644 --- a/resolver-dns/src/main/java/io/netty/resolver/dns/DnsNameResolver.java +++ b/resolver-dns/src/main/java/io/netty/resolver/dns/DnsNameResolver.java @@ -399,8 +399,8 @@ public class DnsNameResolver extends InetNameResolver { this.cnameCache = checkNotNull(cnameCache, "cnameCache"); this.dnsQueryLifecycleObserverFactory = traceEnabled ? dnsQueryLifecycleObserverFactory instanceof NoopDnsQueryLifecycleObserverFactory ? - new TraceDnsQueryLifeCycleObserverFactory() : - new BiDnsQueryLifecycleObserverFactory(new TraceDnsQueryLifeCycleObserverFactory(), + new LoggingDnsQueryLifeCycleObserverFactory() : + new BiDnsQueryLifecycleObserverFactory(new LoggingDnsQueryLifeCycleObserverFactory(), dnsQueryLifecycleObserverFactory) : checkNotNull(dnsQueryLifecycleObserverFactory, "dnsQueryLifecycleObserverFactory"); this.searchDomains = searchDomains != null ? searchDomains.clone() : DEFAULT_SEARCH_DOMAINS; diff --git a/resolver-dns/src/main/java/io/netty/resolver/dns/DnsNameResolverBuilder.java b/resolver-dns/src/main/java/io/netty/resolver/dns/DnsNameResolverBuilder.java index 2afaf1c68f..3ec59ff290 100644 --- a/resolver-dns/src/main/java/io/netty/resolver/dns/DnsNameResolverBuilder.java +++ b/resolver-dns/src/main/java/io/netty/resolver/dns/DnsNameResolverBuilder.java @@ -324,7 +324,10 @@ public final class DnsNameResolverBuilder { * * @param traceEnabled true if trace is enabled * @return {@code this} + * @deprecated Prefer to {@linkplain #dnsQueryLifecycleObserverFactory(DnsQueryLifecycleObserverFactory) configure} + * a {@link LoggingDnsQueryLifeCycleObserverFactory} instead. */ + @Deprecated public DnsNameResolverBuilder traceEnabled(boolean traceEnabled) { this.traceEnabled = traceEnabled; return this; diff --git a/resolver-dns/src/main/java/io/netty/resolver/dns/LoggingDnsQueryLifeCycleObserverFactory.java b/resolver-dns/src/main/java/io/netty/resolver/dns/LoggingDnsQueryLifeCycleObserverFactory.java new file mode 100644 index 0000000000..d33a47e9b7 --- /dev/null +++ b/resolver-dns/src/main/java/io/netty/resolver/dns/LoggingDnsQueryLifeCycleObserverFactory.java @@ -0,0 +1,85 @@ +/* + * Copyright 2020 The Netty Project + * + * The Netty Project licenses this file to you under the Apache License, + * version 2.0 (the "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ +package io.netty.resolver.dns; + +import io.netty.handler.codec.dns.DnsQuestion; +import io.netty.handler.logging.LogLevel; +import io.netty.util.internal.logging.InternalLogLevel; +import io.netty.util.internal.logging.InternalLogger; +import io.netty.util.internal.logging.InternalLoggerFactory; + +import static io.netty.util.internal.ObjectUtil.checkNotNull; + +/** + * A {@link DnsQueryLifecycleObserverFactory} that enables detailed logging in the {@link DnsNameResolver}. + *

+ * When {@linkplain DnsNameResolverBuilder#dnsQueryLifecycleObserverFactory(DnsQueryLifecycleObserverFactory) + * configured on the resolver}, detailed trace information will be generated so that it is easier to understand the + * cause of resolution failure. + */ +public final class LoggingDnsQueryLifeCycleObserverFactory implements DnsQueryLifecycleObserverFactory { + private static final InternalLogger DEFAULT_LOGGER = + InternalLoggerFactory.getInstance(LoggingDnsQueryLifeCycleObserverFactory.class); + private final InternalLogger logger; + private final InternalLogLevel level; + + /** + * Create {@link DnsQueryLifecycleObserver} instances that log events at the default {@link LogLevel#DEBUG} level. + */ + public LoggingDnsQueryLifeCycleObserverFactory() { + this(LogLevel.DEBUG); + } + + /** + * Create {@link DnsQueryLifecycleObserver} instances that log events at the given log level. + * @param level The log level to use for logging resolver events. + */ + public LoggingDnsQueryLifeCycleObserverFactory(LogLevel level) { + this.level = checkAndConvertLevel(level); + logger = DEFAULT_LOGGER; + } + + /** + * Create {@link DnsQueryLifecycleObserver} instances that log events to a logger with the given class context, + * at the given log level. + * @param classContext The class context for the logger to use. + * @param level The log level to use for logging resolver events. + */ + public LoggingDnsQueryLifeCycleObserverFactory(Class classContext, LogLevel level) { + this.level = checkAndConvertLevel(level); + logger = InternalLoggerFactory.getInstance(checkNotNull(classContext, "classContext")); + } + + /** + * Create {@link DnsQueryLifecycleObserver} instances that log events to a logger with the given name context, + * at the given log level. + * @param name The name for the logger to use. + * @param level The log level to use for logging resolver events. + */ + public LoggingDnsQueryLifeCycleObserverFactory(String name, LogLevel level) { + this.level = checkAndConvertLevel(level); + logger = InternalLoggerFactory.getInstance(checkNotNull(name, "name")); + } + + private static InternalLogLevel checkAndConvertLevel(LogLevel level) { + return checkNotNull(level, "level").toInternalLevel(); + } + + @Override + public DnsQueryLifecycleObserver newDnsQueryLifecycleObserver(DnsQuestion question) { + return new LoggingDnsQueryLifecycleObserver(question, logger, level); + } +} diff --git a/resolver-dns/src/main/java/io/netty/resolver/dns/TraceDnsQueryLifecycleObserver.java b/resolver-dns/src/main/java/io/netty/resolver/dns/LoggingDnsQueryLifecycleObserver.java similarity index 93% rename from resolver-dns/src/main/java/io/netty/resolver/dns/TraceDnsQueryLifecycleObserver.java rename to resolver-dns/src/main/java/io/netty/resolver/dns/LoggingDnsQueryLifecycleObserver.java index 8166ff2c30..6f1aa62968 100644 --- a/resolver-dns/src/main/java/io/netty/resolver/dns/TraceDnsQueryLifecycleObserver.java +++ b/resolver-dns/src/main/java/io/netty/resolver/dns/LoggingDnsQueryLifecycleObserver.java @@ -26,13 +26,13 @@ import java.util.List; import static io.netty.util.internal.ObjectUtil.checkNotNull; -final class TraceDnsQueryLifecycleObserver implements DnsQueryLifecycleObserver { +final class LoggingDnsQueryLifecycleObserver implements DnsQueryLifecycleObserver { private final InternalLogger logger; private final InternalLogLevel level; private final DnsQuestion question; private InetSocketAddress dnsServerAddress; - TraceDnsQueryLifecycleObserver(DnsQuestion question, InternalLogger logger, InternalLogLevel level) { + LoggingDnsQueryLifecycleObserver(DnsQuestion question, InternalLogger logger, InternalLogLevel level) { this.question = checkNotNull(question, "question"); this.logger = checkNotNull(logger, "logger"); this.level = checkNotNull(level, "level"); diff --git a/resolver-dns/src/main/java/io/netty/resolver/dns/TraceDnsQueryLifeCycleObserverFactory.java b/resolver-dns/src/main/java/io/netty/resolver/dns/TraceDnsQueryLifeCycleObserverFactory.java deleted file mode 100644 index dc7572bca1..0000000000 --- a/resolver-dns/src/main/java/io/netty/resolver/dns/TraceDnsQueryLifeCycleObserverFactory.java +++ /dev/null @@ -1,45 +0,0 @@ -/* - * Copyright 2017 The Netty Project - * - * The Netty Project licenses this file to you under the Apache License, - * version 2.0 (the "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at: - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations - * under the License. - */ -package io.netty.resolver.dns; - -import io.netty.handler.codec.dns.DnsQuestion; -import io.netty.util.internal.logging.InternalLogLevel; -import io.netty.util.internal.logging.InternalLogger; -import io.netty.util.internal.logging.InternalLoggerFactory; - -import static io.netty.util.internal.ObjectUtil.checkNotNull; - -final class TraceDnsQueryLifeCycleObserverFactory implements DnsQueryLifecycleObserverFactory { - private static final InternalLogger DEFAULT_LOGGER = - InternalLoggerFactory.getInstance(TraceDnsQueryLifeCycleObserverFactory.class); - private static final InternalLogLevel DEFAULT_LEVEL = InternalLogLevel.DEBUG; - private final InternalLogger logger; - private final InternalLogLevel level; - - TraceDnsQueryLifeCycleObserverFactory() { - this(DEFAULT_LOGGER, DEFAULT_LEVEL); - } - - TraceDnsQueryLifeCycleObserverFactory(InternalLogger logger, InternalLogLevel level) { - this.logger = checkNotNull(logger, "logger"); - this.level = checkNotNull(level, "level"); - } - - @Override - public DnsQueryLifecycleObserver newDnsQueryLifecycleObserver(DnsQuestion question) { - return new TraceDnsQueryLifecycleObserver(question, logger, level); - } -}