1 /*
2 * Copyright 2020 The Netty Project
3 *
4 * The Netty Project licenses this file to you under the Apache License,
5 * version 2.0 (the "License"); you may not use this file except in compliance
6 * with the License. You may obtain a copy of the License at:
7 *
8 * https://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations
14 * under the License.
15 */
16 package io.netty.resolver.dns;
17
18 import io.netty.handler.codec.dns.DnsQuestion;
19 import io.netty.handler.logging.LogLevel;
20 import io.netty.util.internal.logging.InternalLogLevel;
21 import io.netty.util.internal.logging.InternalLogger;
22 import io.netty.util.internal.logging.InternalLoggerFactory;
23
24 import static io.netty.util.internal.ObjectUtil.checkNotNull;
25
26 /**
27 * A {@link DnsQueryLifecycleObserverFactory} that enables detailed logging in the {@link DnsNameResolver}.
28 * <p>
29 * When {@linkplain DnsNameResolverBuilder#dnsQueryLifecycleObserverFactory(DnsQueryLifecycleObserverFactory)
30 * configured on the resolver}, detailed trace information will be generated so that it is easier to understand the
31 * cause of resolution failure.
32 */
33 public final class LoggingDnsQueryLifeCycleObserverFactory implements DnsQueryLifecycleObserverFactory {
34 private static final InternalLogger DEFAULT_LOGGER =
35 InternalLoggerFactory.getInstance(LoggingDnsQueryLifeCycleObserverFactory.class);
36 private final InternalLogger logger;
37 private final InternalLogLevel level;
38
39 /**
40 * Create {@link DnsQueryLifecycleObserver} instances that log events at the default {@link LogLevel#DEBUG} level.
41 */
42 public LoggingDnsQueryLifeCycleObserverFactory() {
43 this(LogLevel.DEBUG);
44 }
45
46 /**
47 * Create {@link DnsQueryLifecycleObserver} instances that log events at the given log level.
48 * @param level The log level to use for logging resolver events.
49 */
50 public LoggingDnsQueryLifeCycleObserverFactory(LogLevel level) {
51 this.level = checkAndConvertLevel(level);
52 logger = DEFAULT_LOGGER;
53 }
54
55 /**
56 * Create {@link DnsQueryLifecycleObserver} instances that log events to a logger with the given class context,
57 * at the given log level.
58 * @param classContext The class context for the logger to use.
59 * @param level The log level to use for logging resolver events.
60 */
61 public LoggingDnsQueryLifeCycleObserverFactory(Class<?> classContext, LogLevel level) {
62 this.level = checkAndConvertLevel(level);
63 logger = InternalLoggerFactory.getInstance(checkNotNull(classContext, "classContext"));
64 }
65
66 /**
67 * Create {@link DnsQueryLifecycleObserver} instances that log events to a logger with the given name context,
68 * at the given log level.
69 * @param name The name for the logger to use.
70 * @param level The log level to use for logging resolver events.
71 */
72 public LoggingDnsQueryLifeCycleObserverFactory(String name, LogLevel level) {
73 this.level = checkAndConvertLevel(level);
74 logger = InternalLoggerFactory.getInstance(checkNotNull(name, "name"));
75 }
76
77 private static InternalLogLevel checkAndConvertLevel(LogLevel level) {
78 return checkNotNull(level, "level").toInternalLevel();
79 }
80
81 @Override
82 public DnsQueryLifecycleObserver newDnsQueryLifecycleObserver(DnsQuestion question) {
83 return new LoggingDnsQueryLifecycleObserver(question, logger, level);
84 }
85 }