Allow logger to be configured

This commit is contained in:
Max Brosnahan 2015-11-04 14:42:39 -07:00
parent b9ba10cca2
commit e71b8fb0f3
1 changed files with 9 additions and 3 deletions

View File

@ -15,8 +15,6 @@ import (
"github.com/bmizerany/pat" "github.com/bmizerany/pat"
) )
var logger = log.New(os.Stdout, "[tusd] ", 0)
var reExtractFileID = regexp.MustCompile(`([^/]+)\/?$`) var reExtractFileID = regexp.MustCompile(`([^/]+)\/?$`)
var ( var (
@ -64,6 +62,8 @@ type Config struct {
// Initiate the CompleteUploads channel in the Handler struct in order to // Initiate the CompleteUploads channel in the Handler struct in order to
// be notified about complete uploads // be notified about complete uploads
NotifyCompleteUploads bool NotifyCompleteUploads bool
// Logger the logger to use internally
Logger *log.Logger
} }
type Handler struct { type Handler struct {
@ -73,6 +73,7 @@ type Handler struct {
basePath string basePath string
routeHandler http.Handler routeHandler http.Handler
locks map[string]bool locks map[string]bool
logger *log.Logger
// For each finished upload the corresponding info object will be sent using // For each finished upload the corresponding info object will be sent using
// this unbuffered channel. The NotifyCompleteUploads property in the Config // this unbuffered channel. The NotifyCompleteUploads property in the Config
@ -82,6 +83,10 @@ type Handler struct {
// Create a new handler using the given configuration. // Create a new handler using the given configuration.
func NewHandler(config Config) (*Handler, error) { func NewHandler(config Config) (*Handler, error) {
logger := config.Logger
if logger == nil {
logger = log.New(os.Stdout, "[tusd] ", 0)
}
base := config.BasePath base := config.BasePath
uri, err := url.Parse(base) uri, err := url.Parse(base)
if err != nil { if err != nil {
@ -108,6 +113,7 @@ func NewHandler(config Config) (*Handler, error) {
routeHandler: mux, routeHandler: mux,
locks: make(map[string]bool), locks: make(map[string]bool),
CompleteUploads: make(chan FileInfo), CompleteUploads: make(chan FileInfo),
logger: logger,
} }
mux.Post("", http.HandlerFunc(handler.postFile)) mux.Post("", http.HandlerFunc(handler.postFile))
@ -128,7 +134,7 @@ func (handler *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
r.Method = newMethod r.Method = newMethod
} }
go logger.Println(r.Method, r.URL.Path) go handler.logger.Println(r.Method, r.URL.Path)
header := w.Header() header := w.Header()