Merge pull request #28 from gingermusketeer/allow_logger_to_be_configured

Allow logger to be configured
This commit is contained in:
Marius 2015-11-09 21:53:10 +01:00
commit 376b73ae4b
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 (
@ -66,6 +64,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 {
@ -75,6 +75,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
@ -84,6 +85,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 {
@ -110,6 +115,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))
@ -130,7 +136,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()