From 5b49dc98ff9c92eeb68f8975d2e3aadb5d0c57ab Mon Sep 17 00:00:00 2001 From: Maksim N Date: Thu, 16 Feb 2017 23:22:40 +0100 Subject: [PATCH] Expose logger (#34) This closes #13 and gives ability to set your own logger with your custom prefixes and other logic. Done: - New function `SetLogger` added to be able to set logger - Flag `gracehttp.log` removed just not to confuse people with logs from nowhere --- gracehttp/http.go | 20 ++++++++++++-------- gracehttp/testbin_test.go | 7 +------ 2 files changed, 13 insertions(+), 14 deletions(-) diff --git a/gracehttp/http.go b/gracehttp/http.go index 4ba8d28..fa3ac88 100644 --- a/gracehttp/http.go +++ b/gracehttp/http.go @@ -5,7 +5,6 @@ package gracehttp import ( "bytes" "crypto/tls" - "flag" "fmt" "log" "net" @@ -20,7 +19,7 @@ import ( ) var ( - verbose = flag.Bool("gracehttp.log", true, "Enable logging.") + logger *log.Logger didInherit = os.Getenv("LISTEN_FDS") != "" ppid = os.Getppid() ) @@ -129,17 +128,17 @@ func Serve(servers ...*http.Server) error { } // Some useful logging. - if *verbose { + if logger != nil { if didInherit { if ppid == 1 { - log.Printf("Listening on init activated %s", pprintAddr(a.listeners)) + logger.Printf("Listening on init activated %s", pprintAddr(a.listeners)) } else { const msg = "Graceful handoff of %s with new pid %d and old pid %d" - log.Printf(msg, pprintAddr(a.listeners), os.Getpid(), ppid) + logger.Printf(msg, pprintAddr(a.listeners), os.Getpid(), ppid) } } else { const msg = "Serving %s with pid %d" - log.Printf(msg, pprintAddr(a.listeners), os.Getpid()) + logger.Printf(msg, pprintAddr(a.listeners), os.Getpid()) } } @@ -166,8 +165,8 @@ func Serve(servers ...*http.Server) error { } return err case <-waitdone: - if *verbose { - log.Printf("Exiting pid %d.", os.Getpid()) + if logger != nil { + logger.Printf("Exiting pid %d.", os.Getpid()) } return nil } @@ -184,3 +183,8 @@ func pprintAddr(listeners []net.Listener) []byte { } return out.Bytes() } + +// SetLogger sets logger to be able to grab some useful logs +func SetLogger(l *log.Logger) { + logger = l +} diff --git a/gracehttp/testbin_test.go b/gracehttp/testbin_test.go index b92ec2d..1b2b40b 100644 --- a/gracehttp/testbin_test.go +++ b/gracehttp/testbin_test.go @@ -113,11 +113,6 @@ func testbinMain() { }, } - err := flag.Set("gracehttp.log", "false") - if err != nil { - log.Fatalf("Error setting gracehttp.log: %s", err) - } - // print json to stderr once we can successfully connect to all three // addresses. the ensures we only print the line once we're ready to serve. go func() { @@ -133,7 +128,7 @@ func testbinMain() { } }() - err = gracehttp.Serve( + err := gracehttp.Serve( &http.Server{Addr: httpAddr, Handler: newHandler()}, httpsServer(httpsAddr), )