From e05751d26927eff183c0cc3bd07927e1b88128bb Mon Sep 17 00:00:00 2001 From: Nick Krichevsky Date: Sun, 10 Mar 2019 15:08:08 -0400 Subject: [PATCH] Add logger to webserver --- web/server.go | 5 +++++ web/wire_gen.go | 25 +++++++++++++++++++------ web/wire_stubs.go | 21 ++++++++++++++++----- 3 files changed, 40 insertions(+), 11 deletions(-) diff --git a/web/server.go b/web/server.go index adcbda7..aed3679 100644 --- a/web/server.go +++ b/web/server.go @@ -1,16 +1,20 @@ package web import ( + "io" "net/http" "github.com/go-chi/chi" "github.com/ollien/updown/handler/paste" + "github.com/sirupsen/logrus" ) // Webserver is the main websever type Webserver struct { httpServer *http.Server router chi.Router + logger *logrus.Logger + logWriter *io.PipeWriter // used for httpServer.ErrorLog. Must be stored separately so we can close it. pasteService *paste.Service } @@ -24,6 +28,7 @@ func NewWebserver() *Webserver { // Start starts the webserver func (server *Webserver) Start() error { + defer server.logWriter.Close() return server.httpServer.ListenAndServe() } diff --git a/web/wire_gen.go b/web/wire_gen.go index 50dc8cc..ec3f79e 100644 --- a/web/wire_gen.go +++ b/web/wire_gen.go @@ -8,7 +8,11 @@ package web import ( "github.com/go-chi/chi" "github.com/ollien/updown/config" + "github.com/ollien/updown/handler/log" "github.com/ollien/updown/handler/paste" + "github.com/sirupsen/logrus" + "io" + log2 "log" "net/http" "strconv" ) @@ -18,25 +22,30 @@ import ( func initWebserver() *Webserver { configConfig := config.Get() router := provideRouter() - server := provideHTTPServer(configConfig, router) + logger := log.Get() + pipeWriter := provideLogWriter(logger) + server := provideHTTPServer(configConfig, router, pipeWriter) service := paste.NewService() - webserver := provideWebserver(server, router, service) + webserver := provideWebserver(server, router, service, logger, pipeWriter) return webserver } // wire_stubs.go: -func provideHTTPServer(appConfig config.Config, router chi.Router) *http.Server { +func provideHTTPServer(appConfig config.Config, router chi.Router, logWriter *io.PipeWriter) *http.Server { return &http.Server{ - Addr: appConfig.Server.ListenAddr + ":" + strconv.Itoa(appConfig.Server.Port), - Handler: router, + Addr: appConfig.Server.ListenAddr + ":" + strconv.Itoa(appConfig.Server.Port), + Handler: router, + ErrorLog: log2.New(logWriter, "", 0), } } -func provideWebserver(httpServer *http.Server, router chi.Router, pasteService *paste.Service) *Webserver { +func provideWebserver(httpServer *http.Server, router chi.Router, pasteService *paste.Service, logger *logrus.Logger, logWriter *io.PipeWriter) *Webserver { webserver := Webserver{ httpServer: httpServer, router: router, + logger: logger, + logWriter: logWriter, pasteService: pasteService, } @@ -47,3 +56,7 @@ func provideWebserver(httpServer *http.Server, router chi.Router, pasteService * func provideRouter() chi.Router { return chi.NewRouter() } + +func provideLogWriter(logger *logrus.Logger) *io.PipeWriter { + return logger.Writer() +} diff --git a/web/wire_stubs.go b/web/wire_stubs.go index 1fb5c13..44b6032 100644 --- a/web/wire_stubs.go +++ b/web/wire_stubs.go @@ -3,32 +3,39 @@ package web import ( + "io" + "log" "net/http" "strconv" "github.com/go-chi/chi" "github.com/google/wire" "github.com/ollien/updown/config" + logHandler "github.com/ollien/updown/handler/log" "github.com/ollien/updown/handler/paste" + "github.com/sirupsen/logrus" ) // initWebserver is generated by wire. Please see wire_gen.go for the generated implementation func initWebserver() *Webserver { - wire.Build(provideWebserver, provideHTTPServer, provideRouter, config.Get, paste.NewService) + wire.Build(provideWebserver, provideHTTPServer, provideRouter, config.Get, paste.NewService, logHandler.Get, provideLogWriter) return &Webserver{} } -func provideHTTPServer(appConfig config.Config, router chi.Router) *http.Server { +func provideHTTPServer(appConfig config.Config, router chi.Router, logWriter *io.PipeWriter) *http.Server { return &http.Server{ - Addr: appConfig.Server.ListenAddr + ":" + strconv.Itoa(appConfig.Server.Port), - Handler: router, + Addr: appConfig.Server.ListenAddr + ":" + strconv.Itoa(appConfig.Server.Port), + Handler: router, + ErrorLog: log.New(logWriter, "", 0), } } -func provideWebserver(httpServer *http.Server, router chi.Router, pasteService *paste.Service) *Webserver { +func provideWebserver(httpServer *http.Server, router chi.Router, pasteService *paste.Service, logger *logrus.Logger, logWriter *io.PipeWriter) *Webserver { webserver := Webserver{ httpServer: httpServer, router: router, + logger: logger, + logWriter: logWriter, pasteService: pasteService, } @@ -39,3 +46,7 @@ func provideWebserver(httpServer *http.Server, router chi.Router, pasteService * func provideRouter() chi.Router { return chi.NewRouter() } + +func provideLogWriter(logger *logrus.Logger) *io.PipeWriter { + return logger.Writer() +}