查看本类的 API文档回源码主页即时通讯网 - 即时通讯开发者社区!
1   /*
2    * Copyright 2017 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.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  }