change log library

main
fangdingjun 7 years ago
parent a19ee5a6dc
commit bed437d038

@ -44,10 +44,6 @@ func loadConfig(f string) (*conf, error) {
return nil, err
}
if c.Debug {
logLevel = DEBUG
}
if c.blacklist == nil {
c.blacklist = item{}
}

@ -6,6 +6,7 @@ import (
"strings"
"time"
"github.com/fangdingjun/go-log"
"github.com/miekg/dns"
)
@ -69,28 +70,28 @@ func (h *dnsHandler) queryUpstream(r *dns.Msg, srv addr, ch chan *dns.Msg) {
switch srv.Network {
case "tcp":
info("query %s IN %s, forward to %s:%d through tcp",
log.Debugf("query %s IN %s, forward to %s:%d through tcp",
r.Question[0].Name,
dns.TypeToString[r.Question[0].Qtype],
srv.Host,
srv.Port)
m, _, err = h.tcpclient.Exchange(r, fmt.Sprintf("%s:%d", srv.Host, srv.Port))
case "udp":
info("query %s IN %s, forward to %s:%d through udp",
log.Debugf("query %s IN %s, forward to %s:%d through udp",
r.Question[0].Name,
dns.TypeToString[r.Question[0].Qtype],
srv.Host,
srv.Port)
m, _, err = h.udpclient.Exchange(r, fmt.Sprintf("%s:%d", srv.Host, srv.Port))
case "https_google":
info("query %s IN %s, forward to %s:%d through google https",
log.Debugf("query %s IN %s, forward to %s:%d through google https",
r.Question[0].Name,
dns.TypeToString[r.Question[0].Qtype],
srv.Host,
srv.Port)
m, _, err = h.httpsgoogle.Exchange(r, fmt.Sprintf("%s:%d", srv.Host, srv.Port))
case "https_cloudflare":
info("query %s IN %s, forward to %s:%d through cloudflare https",
log.Debugf("query %s IN %s, forward to %s:%d through cloudflare https",
r.Question[0].Name,
dns.TypeToString[r.Question[0].Qtype],
srv.Host,
@ -106,7 +107,7 @@ func (h *dnsHandler) queryUpstream(r *dns.Msg, srv addr, ch chan *dns.Msg) {
default:
}
} else {
errorlog("%s", err)
log.Errorf("%s", err)
}
}
@ -130,7 +131,7 @@ func (h *dnsHandler) getAnswerFromUpstream(r *dns.Msg, servers []addr) (*dns.Msg
if savedErr != nil {
return savedErr, nil
}
info("query %s IN %s, timeout", r.Question[0].Name, dns.TypeToString[r.Question[0].Qtype])
log.Debugf("query %s IN %s, timeout", r.Question[0].Name, dns.TypeToString[r.Question[0].Qtype])
return nil, errors.New("timeout")
}
}
@ -147,7 +148,7 @@ func (h *dnsHandler) inBlacklist(m *dns.Msg) bool {
ip = ""
}
if ip != "" && h.cfg.blacklist.exists(ip) {
info("%s in blacklist", ip)
log.Debugf("%s in blacklist", ip)
return true
}
}
@ -168,7 +169,7 @@ func (h *dnsHandler) answerFromHosts(w dns.ResponseWriter, r *dns.Msg) bool {
msg.SetReply(r)
msg.Answer = append(msg.Answer, rr)
w.WriteMsg(msg)
info("query %s IN %s, reply from hosts", domain, dns.TypeToString[t])
log.Debugf("query %s IN %s, reply from hosts", domain, dns.TypeToString[t])
return true
}
return false

@ -1,31 +0,0 @@
package main
import (
"log"
)
const (
_ = iota
FATAL
ERROR
WARN
NOTICE
INFO
DEBUG
)
var logLevel = WARN
func logMsg(l int, fmt string, args ...interface{}) {
if l <= logLevel {
log.Printf(fmt, args...)
}
}
func info(fmt string, args ...interface{}) {
logMsg(INFO, fmt, args...)
}
func errorlog(fmt string, args ...interface{}) {
logMsg(ERROR, fmt, args...)
}

@ -3,26 +3,59 @@ package main
import (
"flag"
"fmt"
"log"
"os"
"github.com/fangdingjun/go-log"
"github.com/fangdingjun/go-log/formatters"
"github.com/fangdingjun/go-log/writers"
"github.com/miekg/dns"
)
func main() {
var configfile string
var logfile string
var loglevel string
var logFileSize int64
var logFileCount int
flag.StringVar(&configfile, "c", "config.yaml", "config file")
flag.StringVar(&logfile, "log_file", "", "log file, default stdout")
flag.IntVar(&logFileCount, "log_count", 10, "max count of log to keep")
flag.Int64Var(&logFileSize, "log_size", 10, "max log file size MB")
flag.StringVar(&loglevel, "log_level", "INFO", "log level, values:\nOFF, FATAL, PANIC, ERROR, WARN, INFO, DEBUG")
flag.Parse()
if logfile != "" {
log.Default.Out = &writers.FixedSizeFileWriter{
MaxCount: logFileCount,
Name: logfile,
MaxSize: logFileSize * 1024 * 1024,
}
}
if loglevel != "" {
lv, err := log.ParseLevel(loglevel)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
log.Default.Level = lv
}
log.Default.Formatter = &formatters.TextFormatter{TimeFormat: "2006-01-02 15:04:05.000"}
config, err := loadConfig(configfile)
if err != nil {
log.Fatal(err)
}
log.Debugf("config: %+v", config)
h := newDNSHandler(config)
for _, l := range config.Listen {
go func(l addr) {
log.Infof("listen on %s %s:%d", l.Network, l.Host, l.Port)
if err := dns.ListenAndServe(
fmt.Sprintf("%s:%d", l.Host, l.Port), l.Network, h); err != nil {
log.Fatal(err)

Loading…
Cancel
Save