git » dnss » commit 2958a6a

Replace log with glog, and increase the level of payload logging

author Alberto Bertogli
2015-09-11 00:13:39 UTC
committer Alberto Bertogli
2015-09-11 00:14:18 UTC
parent 9eb23e08a8b4f5ba6302cb84965d9951e767f8ab

Replace log with glog, and increase the level of payload logging

dnss.go +22 -5
dnstogrpc/dnstogrpc.go +13 -14
grpctodns/grpctodns.go +13 -10

diff --git a/dnss.go b/dnss.go
index a551b6c..e477d1c 100644
--- a/dnss.go
+++ b/dnss.go
@@ -2,12 +2,16 @@ package main
 
 import (
 	"flag"
-	"log"
 	"sync"
+	"time"
+
+	"github.com/golang/glog"
+
+	// Make GRPC log to glog.
+	_ "google.golang.org/grpc/grpclog/glogger"
 
 	"blitiri.com.ar/go/dnss/dnstogrpc"
 	"blitiri.com.ar/go/dnss/grpctodns"
-	"blitiri.com.ar/go/logconfig"
 	"blitiri.com.ar/go/profile"
 )
 
@@ -32,17 +36,30 @@ var (
 		"certificate file for the GRPC server")
 	grpcKey = flag.String("grpc_key", "",
 		"key file for the GRPC server")
+
+	logFlushEvery = flag.Duration("log_flush_every", 30*time.Second,
+		"how often to flush logs")
 )
 
+func flushLogs() {
+	c := time.Tick(*logFlushEvery)
+	for range c {
+		glog.Flush()
+	}
+}
+
 func main() {
+	defer glog.Flush()
+
 	flag.Parse()
 
-	logconfig.Init("dnss")
 	profile.Init()
 
+	go flushLogs()
+
 	if !*enableDNStoGRPC && !*enableGRPCtoDNS {
-		log.Fatalf(
-			"ERROR: pass --enable_dns_to_grpc or --enable_grpc_to_dns\n")
+		glog.Fatal(
+			"Error: pass --enable_dns_to_grpc or --enable_grpc_to_dns")
 	}
 
 	var wg sync.WaitGroup
diff --git a/dnstogrpc/dnstogrpc.go b/dnstogrpc/dnstogrpc.go
index e2c24d4..5cc8497 100644
--- a/dnstogrpc/dnstogrpc.go
+++ b/dnstogrpc/dnstogrpc.go
@@ -4,12 +4,12 @@ package dnstogrpc
 
 import (
 	"fmt"
-	"log"
 	"sync"
 	"time"
 
 	pb "blitiri.com.ar/go/dnss/proto"
 	"blitiri.com.ar/go/dnss/util"
+	"github.com/golang/glog"
 	"github.com/miekg/dns"
 	"golang.org/x/net/context"
 	"google.golang.org/grpc"
@@ -80,28 +80,28 @@ func New(addr, upstream, caFile string) *Server {
 	}
 }
 
-func p(w dns.ResponseWriter, r *dns.Msg) string {
-	return fmt.Sprintf("%v %v", w.RemoteAddr(), r.Id)
-}
-
 func (s *Server) Handler(w dns.ResponseWriter, r *dns.Msg) {
-	log.Printf("DNS  %v %v\n", p(w, r), util.QuestionsToString(r.Question))
+	prefix := ""
+	if glog.V(3) {
+		prefix = fmt.Sprintf("%v %v", w.RemoteAddr(), r.Id)
+		glog.Infof("DNS  %v %v", prefix, util.QuestionsToString(r.Question))
+	}
 
 	// TODO: we should create our own IDs, in case different users pick the
 	// same id and we pass that upstream.
 
 	from_up, err := s.client.Query(r)
 	if err != nil {
-		log.Printf("DNS  %v  ERR: %v\n", p(w, r), err)
+		glog.V(3).Infof("DNS  %v  ERR: %v", prefix, err)
 	}
 
 	if from_up != nil {
 		if from_up.Rcode != dns.RcodeSuccess {
 			rcode := dns.RcodeToString[from_up.Rcode]
-			log.Printf("DNS  %v  !->  %v\n", p(w, r), rcode)
+			glog.V(3).Infof("DNS  %v  !->  %v", prefix, rcode)
 		}
 		for _, rr := range from_up.Answer {
-			log.Printf("DNS  %v  ->  %v\n", p(w, r), rr)
+			glog.V(3).Infof("DNS  %v  ->  %v", prefix, rr)
 		}
 		w.WriteMsg(from_up)
 	}
@@ -110,26 +110,25 @@ func (s *Server) Handler(w dns.ResponseWriter, r *dns.Msg) {
 func (s *Server) ListenAndServe() {
 	err := s.client.Connect()
 	if err != nil {
-		// TODO: handle errors and reconnect.
-		log.Printf("Error creating GRPC client: %v\n", err)
+		glog.Errorf("Error creating GRPC client: %v", err)
 		return
 	}
 
-	log.Printf("DNS listening on %s\n", s.Addr)
+	glog.Infof("DNS listening on %s", s.Addr)
 
 	var wg sync.WaitGroup
 	wg.Add(1)
 	go func() {
 		defer wg.Done()
 		err := dns.ListenAndServe(s.Addr, "udp", dns.HandlerFunc(s.Handler))
-		log.Printf("Exiting UDP: %v\n", err)
+		glog.Errorf("Exiting UDP: %v", err)
 	}()
 
 	wg.Add(1)
 	go func() {
 		defer wg.Done()
 		err := dns.ListenAndServe(s.Addr, "tcp", dns.HandlerFunc(s.Handler))
-		log.Printf("Exiting TCP: %v\n", err)
+		glog.Errorf("Exiting TCP: %v", err)
 	}()
 
 	wg.Wait()
diff --git a/grpctodns/grpctodns.go b/grpctodns/grpctodns.go
index ff95272..98d4f0f 100644
--- a/grpctodns/grpctodns.go
+++ b/grpctodns/grpctodns.go
@@ -4,12 +4,12 @@ package grpctodns
 
 import (
 	"fmt"
-	"log"
 	"net"
 	"strings"
 
 	pb "blitiri.com.ar/go/dnss/proto"
 	"blitiri.com.ar/go/dnss/util"
+	"github.com/golang/glog"
 	"github.com/miekg/dns"
 	"golang.org/x/net/context"
 	"google.golang.org/grpc"
@@ -48,13 +48,15 @@ func (s *Server) Query(ctx context.Context, in *pb.RawMsg) (*pb.RawMsg, error) {
 		return nil, err
 	}
 
-	log.Printf("GRPC %v\n", util.QuestionsToString(r.Question))
+	if glog.V(3) {
+		glog.Infof("GRPC %v", util.QuestionsToString(r.Question))
+	}
 
 	// TODO: we should create our own IDs, in case different users pick the
 	// same id and we pass that upstream.
 	from_up, err := dns.Exchange(r, s.Upstream)
 	if err != nil {
-		log.Printf("GRPC   ERR: %v\n", err)
+		glog.V(3).Infof("GRPC   ERR: %v", err)
 		return nil, err
 	}
 
@@ -64,15 +66,15 @@ func (s *Server) Query(ctx context.Context, in *pb.RawMsg) (*pb.RawMsg, error) {
 
 	if from_up.Rcode != dns.RcodeSuccess {
 		rcode := dns.RcodeToString[from_up.Rcode]
-		log.Printf("GPRC   !->  %v\n", rcode)
+		glog.V(3).Infof("GPRC   !->  %v", rcode)
 	}
 	for _, rr := range from_up.Answer {
-		log.Printf("GRPC   ->  %v\n", rr)
+		glog.V(3).Infof("GRPC   ->  %v", rr)
 	}
 
 	buf, err := from_up.Pack()
 	if err != nil {
-		log.Printf("GRPC   ERR: %v\n", err)
+		glog.V(3).Infof("GRPC   ERR: %v", err)
 		return nil, err
 	}
 
@@ -82,19 +84,20 @@ func (s *Server) Query(ctx context.Context, in *pb.RawMsg) (*pb.RawMsg, error) {
 func (s *Server) ListenAndServe() {
 	lis, err := net.Listen("tcp", s.Addr)
 	if err != nil {
-		log.Printf("failed to listen: %v", err)
+		glog.Errorf("failed to listen: %v", err)
 		return
 	}
 
 	ta, err := credentials.NewServerTLSFromFile(s.CertFile, s.KeyFile)
 	if err != nil {
-		log.Printf("failed to create TLS transport auth: %v", err)
+		glog.Errorf("failed to create TLS transport auth: %v", err)
 		return
 	}
 
 	grpcServer := grpc.NewServer(grpc.Creds(ta))
 	pb.RegisterDNSServiceServer(grpcServer, s)
 
-	log.Printf("GRPC listening on %s\n", s.Addr)
-	grpcServer.Serve(lis)
+	glog.Errorf("GRPC listening on %s", s.Addr)
+	err = grpcServer.Serve(lis)
+	glog.Errorf("GRPC exiting: %s", err)
 }