1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty.resolver.dns;
17
18 import io.netty.channel.ChannelFuture;
19 import io.netty.handler.codec.dns.DnsQuestion;
20 import io.netty.handler.codec.dns.DnsResponseCode;
21 import io.netty.util.internal.logging.InternalLogLevel;
22 import io.netty.util.internal.logging.InternalLogger;
23
24 import java.net.InetSocketAddress;
25 import java.util.List;
26
27 import static io.netty.util.internal.ObjectUtil.checkNotNull;
28
29 final class LoggingDnsQueryLifecycleObserver implements DnsQueryLifecycleObserver {
30 private final InternalLogger logger;
31 private final InternalLogLevel level;
32 private final DnsQuestion question;
33 private InetSocketAddress dnsServerAddress;
34
35 LoggingDnsQueryLifecycleObserver(DnsQuestion question, InternalLogger logger, InternalLogLevel level) {
36 this.question = checkNotNull(question, "question");
37 this.logger = checkNotNull(logger, "logger");
38 this.level = checkNotNull(level, "level");
39 }
40
41 @Override
42 public void queryWritten(InetSocketAddress dnsServerAddress, ChannelFuture future) {
43 this.dnsServerAddress = dnsServerAddress;
44 }
45
46 @Override
47 public void queryCancelled(int queriesRemaining) {
48 if (dnsServerAddress != null) {
49 logger.log(level, "from {} : {} cancelled with {} queries remaining", dnsServerAddress, question,
50 queriesRemaining);
51 } else {
52 logger.log(level, "{} query never written and cancelled with {} queries remaining", question,
53 queriesRemaining);
54 }
55 }
56
57 @Override
58 public DnsQueryLifecycleObserver queryRedirected(List<InetSocketAddress> nameServers) {
59 logger.log(level, "from {} : {} redirected", dnsServerAddress, question);
60 return this;
61 }
62
63 @Override
64 public DnsQueryLifecycleObserver queryCNAMEd(DnsQuestion cnameQuestion) {
65 logger.log(level, "from {} : {} CNAME question {}", dnsServerAddress, question, cnameQuestion);
66 return this;
67 }
68
69 @Override
70 public DnsQueryLifecycleObserver queryNoAnswer(DnsResponseCode code) {
71 logger.log(level, "from {} : {} no answer {}", dnsServerAddress, question, code);
72 return this;
73 }
74
75 @Override
76 public void queryFailed(Throwable cause) {
77 if (dnsServerAddress != null) {
78 logger.log(level, "from {} : {} failure", dnsServerAddress, question, cause);
79 } else {
80 logger.log(level, "{} query never written and failed", question, cause);
81 }
82 }
83
84 @Override
85 public void querySucceed() {
86 }
87 }