Pretty log output

This commit is contained in:
kenshinx 2015-10-14 01:00:28 +08:00
parent 3f70c993a8
commit cd272bf769
4 changed files with 32 additions and 20 deletions

View File

@ -40,9 +40,9 @@ maxcount = 0 #If set zero. The Sum of cache itmes will be unlimit.
[hosts] [hosts]
#If set false, will not query hosts file and redis hosts record #If set false, will not query hosts file and redis hosts record
enable = false enable = true
host-file = "/etc/hosts" host-file = "/etc/hosts"
redis-enable = true redis-enable = false
redis-key = "godns:hosts" redis-key = "godns:hosts"
ttl = 600 ttl = 600

View File

@ -1,6 +1,7 @@
package main package main
import ( import (
"net"
"time" "time"
"github.com/miekg/dns" "github.com/miekg/dns"
@ -85,7 +86,13 @@ func (h *GODNSHandler) do(Net string, w dns.ResponseWriter, req *dns.Msg) {
q := req.Question[0] q := req.Question[0]
Q := Question{UnFqdn(q.Name), dns.TypeToString[q.Qtype], dns.ClassToString[q.Qclass]} Q := Question{UnFqdn(q.Name), dns.TypeToString[q.Qtype], dns.ClassToString[q.Qclass]}
Debug("Question: %s", Q.String()) var remote net.IP
if Net == "tcp" {
remote = w.RemoteAddr().(*net.TCPAddr).IP
} else {
remote = w.RemoteAddr().(*net.UDPAddr).IP
}
logger.Info("%s lookup %s", remote, Q.String())
IPQuery := h.isIPQuery(q) IPQuery := h.isIPQuery(q)
@ -117,10 +124,10 @@ func (h *GODNSHandler) do(Net string, w dns.ResponseWriter, req *dns.Msg) {
} }
w.WriteMsg(m) w.WriteMsg(m)
Debug("%s found in hosts file", Q.qname) logger.Debug("%s found in hosts file", Q.qname)
return return
} else { } else {
Debug("%s didn't found in hosts file", Q.qname) logger.Debug("%s didn't found in hosts file", Q.qname)
} }
} }
@ -130,14 +137,14 @@ func (h *GODNSHandler) do(Net string, w dns.ResponseWriter, req *dns.Msg) {
mesg, err := h.cache.Get(key) mesg, err := h.cache.Get(key)
if err != nil { if err != nil {
if mesg, err = h.negCache.Get(key); err != nil { if mesg, err = h.negCache.Get(key); err != nil {
Debug("%s didn't hit cache: %s", Q.String(), err) logger.Debug("%s didn't hit cache", Q.String())
} else { } else {
Debug("%s hit negative cache", Q.String()) logger.Debug("%s hit negative cache", Q.String())
dns.HandleFailed(w, req) dns.HandleFailed(w, req)
return return
} }
} else { } else {
Debug("%s hit cache", Q.String()) logger.Debug("%s hit cache", Q.String())
// we need this copy against concurrent modification of Id // we need this copy against concurrent modification of Id
msg := *mesg msg := *mesg
msg.Id = req.Id msg.Id = req.Id
@ -149,12 +156,12 @@ func (h *GODNSHandler) do(Net string, w dns.ResponseWriter, req *dns.Msg) {
mesg, err := h.resolver.Lookup(Net, req) mesg, err := h.resolver.Lookup(Net, req)
if err != nil { if err != nil {
Debug("%s", err) logger.Warn("Resolve query error %s", err)
dns.HandleFailed(w, req) dns.HandleFailed(w, req)
// cache the failure, too! // cache the failure, too!
if err = h.negCache.Set(key, nil); err != nil { if err = h.negCache.Set(key, nil); err != nil {
Debug("Set %s negative cache failed: %v", Q.String(), err) logger.Warn("Set %s negative cache failed: %v", Q.String(), err)
} }
return return
} }
@ -164,9 +171,9 @@ func (h *GODNSHandler) do(Net string, w dns.ResponseWriter, req *dns.Msg) {
if IPQuery > 0 && len(mesg.Answer) > 0 { if IPQuery > 0 && len(mesg.Answer) > 0 {
err = h.cache.Set(key, mesg) err = h.cache.Set(key, mesg)
if err != nil { if err != nil {
Debug("Set %s cache failed: %s", Q.String(), err.Error()) logger.Warn("Set %s cache failed: %s", Q.String(), err.Error())
} }
Debug("Insert %s into cache", Q.String()) logger.Debug("Insert %s into cache", Q.String())
} }
} }

View File

@ -90,8 +90,12 @@ func (r *RedisHosts) Set(domain, ip string) (bool, error) {
} }
func (r *RedisHosts) Refresh() { func (r *RedisHosts) Refresh() {
r.redis.Hgetall(r.key, r.hosts) err := r.redis.Hgetall(r.key, r.hosts)
Debug("update hosts records from redis") if err != nil {
logger.Warn("Update hosts records from redis failed %s", err)
} else {
logger.Debug("Update hosts records from redis")
}
} }
type FileHosts struct { type FileHosts struct {
@ -107,7 +111,8 @@ func (f *FileHosts) Get(domain string) (ip string, ok bool) {
func (f *FileHosts) Refresh() { func (f *FileHosts) Refresh() {
buf, err := os.Open(f.file) buf, err := os.Open(f.file)
if err != nil { if err != nil {
panic("Can't open " + f.file) logger.Warn("Update hosts records from file failed %s", err)
return
} }
defer buf.Close() defer buf.Close()
@ -138,7 +143,7 @@ func (f *FileHosts) Refresh() {
f.hosts[domain] = ip f.hosts[domain] = ip
} }
Debug("update hosts records from %s", f.file) logger.Debug("update hosts records from %s", f.file)
} }
func (f *FileHosts) isDomain(domain string) bool { func (f *FileHosts) isDomain(domain string) bool {

View File

@ -41,15 +41,15 @@ func (r *Resolver) Lookup(net string, req *dns.Msg) (message *dns.Msg, err error
defer wg.Done() defer wg.Done()
r, rtt, err := c.Exchange(req, nameserver) r, rtt, err := c.Exchange(req, nameserver)
if err != nil { if err != nil {
Debug("%s socket error on %s", qname, nameserver) logger.Warn("%s socket error on %s", qname, nameserver)
Debug("error:%s", err.Error()) logger.Warn("error:%s", err.Error())
return return
} }
if r != nil && r.Rcode != dns.RcodeSuccess { if r != nil && r.Rcode != dns.RcodeSuccess {
Debug("%s failed to get an valid answer on %s", qname, nameserver) logger.Warn("%s failed to get an valid answer on %s", qname, nameserver)
return return
} }
Debug("%s resolv on %s (%s) ttl: %d", UnFqdn(qname), nameserver, net, rtt) logger.Debug("%s resolv on %s (%s) ttl: %d", UnFqdn(qname), nameserver, net, rtt)
select { select {
case res <- r: case res <- r:
default: default: