Use glog levelled logging to decrease announce overhead

This commit is contained in:
Justin Li 2014-07-16 11:52:59 -04:00
parent df5ad919f9
commit f3f58c024c
2 changed files with 21 additions and 17 deletions

View file

@ -48,18 +48,21 @@ type ResponseHandler func(http.ResponseWriter, *http.Request, httprouter.Params)
func makeHandler(handler ResponseHandler) httprouter.Handle { func makeHandler(handler ResponseHandler) httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) { return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
start := time.Now() start := time.Now()
code, err := handler(w, r, p)
httpCode, err := handler(w, r, p)
if err != nil { if err != nil {
http.Error(w, err.Error(), code) http.Error(w, err.Error(), httpCode)
} }
glog.Infof( if glog.V(2) {
"Completed %v %s %s in %v", glog.Infof(
code, "Completed %v %s %s in %v",
http.StatusText(code), httpCode,
r.URL.Path, http.StatusText(httpCode),
time.Since(start), r.URL.Path,
) time.Since(start),
)
}
} }
} }
@ -96,6 +99,7 @@ func Serve(cfg *config.Config) {
glog.Fatal("New: ", err) glog.Fatal("New: ", err)
} }
glog.V(0).Info("Starting on ", cfg.Addr)
graceful.Run(cfg.Addr, cfg.RequestTimeout.Duration, NewRouter(t, cfg)) graceful.Run(cfg.Addr, cfg.RequestTimeout.Duration, NewRouter(t, cfg))
} }

16
main.go
View file

@ -38,36 +38,36 @@ func Boot() {
flag.Parse() flag.Parse()
runtime.GOMAXPROCS(maxprocs) runtime.GOMAXPROCS(maxprocs)
glog.Info("set max threads to ", maxprocs) glog.V(1).Info("Set max threads to ", maxprocs)
if profile != "" { if profile != "" {
f, err := os.Create(profile) f, err := os.Create(profile)
if err != nil { if err != nil {
glog.Fatalf("failed to create profile file: %s\n", err) glog.Fatalf("Failed to create profile file: %s\n", err)
} }
defer f.Close() defer f.Close()
pprof.StartCPUProfile(f) pprof.StartCPUProfile(f)
glog.Info("started profiling") glog.Info("Started profiling")
defer func() { defer func() {
pprof.StopCPUProfile() pprof.StopCPUProfile()
glog.Info("stopped profiling") glog.Info("Stopped profiling")
}() }()
} }
cfg, err := config.Open(configPath) cfg, err := config.Open(configPath)
if err != nil { if err != nil {
glog.Fatalf("failed to parse configuration file: %s\n", err) glog.Fatalf("Failed to parse configuration file: %s\n", err)
} }
if cfg == &config.DefaultConfig { if cfg == &config.DefaultConfig {
glog.Info("using default config") glog.V(1).Info("Using default config")
} else { } else {
glog.Infof("loaded config file: %s", configPath) glog.V(1).Infof("Loaded config file: %s", configPath)
} }
http.Serve(cfg) http.Serve(cfg)
glog.Info("gracefully shutdown") glog.Info("Gracefully shut down")
} }
func main() { func main() {