From e71b8fb0f3194957f6b0169754c91e906cc52fe2 Mon Sep 17 00:00:00 2001 From: Max Brosnahan Date: Wed, 4 Nov 2015 14:42:39 -0700 Subject: [PATCH] Allow logger to be configured --- handler.go | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/handler.go b/handler.go index cfa0c47..0e37759 100644 --- a/handler.go +++ b/handler.go @@ -15,8 +15,6 @@ import ( "github.com/bmizerany/pat" ) -var logger = log.New(os.Stdout, "[tusd] ", 0) - var reExtractFileID = regexp.MustCompile(`([^/]+)\/?$`) var ( @@ -64,6 +62,8 @@ type Config struct { // Initiate the CompleteUploads channel in the Handler struct in order to // be notified about complete uploads NotifyCompleteUploads bool + // Logger the logger to use internally + Logger *log.Logger } type Handler struct { @@ -73,6 +73,7 @@ type Handler struct { basePath string routeHandler http.Handler locks map[string]bool + logger *log.Logger // For each finished upload the corresponding info object will be sent using // this unbuffered channel. The NotifyCompleteUploads property in the Config @@ -82,6 +83,10 @@ type Handler struct { // Create a new handler using the given configuration. func NewHandler(config Config) (*Handler, error) { + logger := config.Logger + if logger == nil { + logger = log.New(os.Stdout, "[tusd] ", 0) + } base := config.BasePath uri, err := url.Parse(base) if err != nil { @@ -108,6 +113,7 @@ func NewHandler(config Config) (*Handler, error) { routeHandler: mux, locks: make(map[string]bool), CompleteUploads: make(chan FileInfo), + logger: logger, } mux.Post("", http.HandlerFunc(handler.postFile)) @@ -128,7 +134,7 @@ func (handler *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { r.Method = newMethod } - go logger.Println(r.Method, r.URL.Path) + go handler.logger.Println(r.Method, r.URL.Path) header := w.Header()