diff --git a/go.mod b/go.mod index e0e76fe..955d4b2 100644 --- a/go.mod +++ b/go.mod @@ -1,11 +1,12 @@ module github.com/webtor-io/srt2vtt -go 1.12 +go 1.14 require ( github.com/asticode/go-astilog v1.4.0 // indirect github.com/asticode/go-astisub v0.2.0 github.com/asticode/go-astitools v1.3.0 // indirect + github.com/bakins/logrus-middleware v0.0.0-20180426214643-ce4c6f8deb07 github.com/djimenez/iconv-go v0.0.0-20160305225143-8960e66bd3da github.com/gogs/chardet v0.0.0-20150115103509-2404f7772561 github.com/joonix/log v0.0.0-20190524090622-13fe31bbdd7a diff --git a/go.sum b/go.sum index f5154b3..e2eda29 100644 --- a/go.sum +++ b/go.sum @@ -31,6 +31,8 @@ github.com/asticode/go-astits v1.2.0/go.mod h1:1Tvjdozd9RwgwOUteaxmd0Sutyo+kTO24 github.com/asticode/go-astits v1.3.0 h1:tlf2E0uNpL769FQr/2fZSvH1gEG5dqWezfaspGvfd+M= github.com/asticode/go-astits v1.3.0/go.mod h1:Dp78dEuksl+pWzlXTkpdd91U1QDX9r57db1XpLHm5Mw= github.com/asticode/go-astiws v1.0.1/go.mod h1:5F/0RkJTq1SZ0TsiheD3j/9lr9kla70c2q7w1aAG78Q= +github.com/bakins/logrus-middleware v0.0.0-20180426214643-ce4c6f8deb07 h1:YyWvJqruuX4aBN812F9ex3WXuxdqruVNd5rvww8U9ko= +github.com/bakins/logrus-middleware v0.0.0-20180426214643-ce4c6f8deb07/go.mod h1:Z+aq7HgFBRFKVDlBpL/cgrb94A9VgwMSlsFAxAJeK6s= github.com/certifi/gocertifi v0.0.0-20190506164543-d2eda7129713 h1:UNOqI3EKhvbqV8f1Vm3NIwkrhq388sGCeAH2Op7w0rc= github.com/certifi/gocertifi v0.0.0-20190506164543-d2eda7129713/go.mod h1:GJKEexRPVJrBSOjoqN5VNOIKJ5Q3RViH6eu3puDRwx4= github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= diff --git a/services/web.go b/services/web.go index a4cd2ba..3783558 100644 --- a/services/web.go +++ b/services/web.go @@ -6,6 +6,8 @@ import ( "net" "net/http" + logrusmiddleware "github.com/bakins/logrus-middleware" + joonix "github.com/joonix/log" "github.com/pkg/errors" log "github.com/sirupsen/logrus" ) @@ -48,8 +50,13 @@ func (s *Web) Serve() error { } io.WriteString(w, data) }) + logger := log.New() + logger.SetFormatter(joonix.NewFormatter()) + l := logrusmiddleware.Middleware{ + Logger: logger, + } log.Infof("Serving Web at %v", addr) - return http.Serve(ln, mux) + return http.Serve(ln, l.Handler(mux, "")) } func (s *Web) Close() { diff --git a/vendor/github.com/bakins/logrus-middleware/.gitignore b/vendor/github.com/bakins/logrus-middleware/.gitignore new file mode 100644 index 0000000..daf913b --- /dev/null +++ b/vendor/github.com/bakins/logrus-middleware/.gitignore @@ -0,0 +1,24 @@ +# Compiled Object files, Static and Dynamic libs (Shared Objects) +*.o +*.a +*.so + +# Folders +_obj +_test + +# Architecture specific extensions/prefixes +*.[568vq] +[568vq].out + +*.cgo1.go +*.cgo2.c +_cgo_defun.c +_cgo_gotypes.go +_cgo_export.* + +_testmain.go + +*.exe +*.test +*.prof diff --git a/vendor/github.com/bakins/logrus-middleware/.travis.yml b/vendor/github.com/bakins/logrus-middleware/.travis.yml new file mode 100644 index 0000000..1e94606 --- /dev/null +++ b/vendor/github.com/bakins/logrus-middleware/.travis.yml @@ -0,0 +1,15 @@ +language: go + +go: + - 1.6 + +before_install: + - go get -u golang.org/x/lint/golint + +install: + - go get -d -v ./... + - go get github.com/bakins/test-helpers + +script: + - golint ./... + - go test -v ./... diff --git a/vendor/github.com/bakins/logrus-middleware/LICENSE b/vendor/github.com/bakins/logrus-middleware/LICENSE new file mode 100644 index 0000000..030460a --- /dev/null +++ b/vendor/github.com/bakins/logrus-middleware/LICENSE @@ -0,0 +1,22 @@ +The MIT License (MIT) + +Copyright (c) 2015 Brian Akins + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + diff --git a/vendor/github.com/bakins/logrus-middleware/README.md b/vendor/github.com/bakins/logrus-middleware/README.md new file mode 100644 index 0000000..324ac36 --- /dev/null +++ b/vendor/github.com/bakins/logrus-middleware/README.md @@ -0,0 +1,14 @@ +[![GoDoc](https://godoc.org/github.com/bakins/logrus-middleware?status.svg)](https://godoc.org/github.com/bakins/logrus-middleware) + +# logrus-middleware +Simple logging middleware for Go using logrus + +**Note** This needs a new maintainer. I no longer use this, so I'd be happy to transfer ownership if you find it useful. Open an issue or contact me if you are interested. + +"Normal" net/http middleware for Go for logging. + + +See the [example](./example/main.go) + +Based on: https://github.com/meatballhat/negroni-logrus + diff --git a/vendor/github.com/bakins/logrus-middleware/middleware.go b/vendor/github.com/bakins/logrus-middleware/middleware.go new file mode 100644 index 0000000..306592d --- /dev/null +++ b/vendor/github.com/bakins/logrus-middleware/middleware.go @@ -0,0 +1,114 @@ +// Package logrusmiddleware is a simple net/http middleware for logging +// using logrus +package logrusmiddleware + +import ( + "net/http" + "time" + + "github.com/sirupsen/logrus" +) + +type ( + // Middleware is a middleware handler for HTTP logging + Middleware struct { + // Logger is the log.Logger instance used to log messages with the Logger middleware + Logger *logrus.Logger + // Name is the name of the application as recorded in latency metrics + Name string + } + + responseData struct { + status int + size int + } + + // Handler is the actual middleware that handles logging + Handler struct { + http.ResponseWriter + m *Middleware + handler http.Handler + component string + responseData *responseData + } +) + +func (h *Handler) newResponseData() *responseData { + return &responseData{ + status: 0, + size: 0, + } +} + +// Handler create a new handler. component, if set, is emitted in the log messages. +func (m *Middleware) Handler(h http.Handler, component string) *Handler { + return &Handler{ + m: m, + handler: h, + component: component, + } +} + +// Write is a wrapper for the "real" ResponseWriter.Write +func (h *Handler) Write(b []byte) (int, error) { + if h.responseData.status == 0 { + // The status will be StatusOK if WriteHeader has not been called yet + h.responseData.status = http.StatusOK + } + size, err := h.ResponseWriter.Write(b) + h.responseData.size += size + return size, err +} + +// WriteHeader is a wrapper around ResponseWriter.WriteHeader +func (h *Handler) WriteHeader(s int) { + h.ResponseWriter.WriteHeader(s) + h.responseData.status = s +} + +// Header is a wrapper around ResponseWriter.Header +func (h *Handler) Header() http.Header { + return h.ResponseWriter.Header() +} + +// ServeHTTP calls the "real" handler and logs using the logger +func (h *Handler) ServeHTTP(rw http.ResponseWriter, r *http.Request) { + start := time.Now() + + h = h.m.Handler(h.handler, h.component) + h.ResponseWriter = rw + h.responseData = h.newResponseData() + h.handler.ServeHTTP(h, r) + + latency := time.Since(start) + + status := h.responseData.status + if status == 0 { + status = 200 + } + + fields := logrus.Fields{ + "status": status, + "method": r.Method, + "request": r.RequestURI, + "remote": r.RemoteAddr, + "duration": float64(latency.Nanoseconds()) / float64(1000), + "size": h.responseData.size, + "referer": r.Referer(), + "user-agent": r.UserAgent(), + } + + if h.m.Name != "" { + fields["name"] = h.m.Name + } + + if h.component != "" { + fields["component"] = h.component + } + + if l := h.m.Logger; l != nil { + l.WithFields(fields).Info("completed handling request") + } else { + logrus.WithFields(fields).Info("completed handling request") + } +} diff --git a/vendor/modules.txt b/vendor/modules.txt index 87e29f8..2d386bc 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -1,12 +1,22 @@ # github.com/asticode/go-astikit v0.2.0 github.com/asticode/go-astikit +# github.com/asticode/go-astilog v1.4.0 +## explicit # github.com/asticode/go-astisub v0.2.0 +## explicit github.com/asticode/go-astisub +# github.com/asticode/go-astitools v1.3.0 +## explicit # github.com/asticode/go-astits v1.3.0 github.com/asticode/go-astits +# github.com/bakins/logrus-middleware v0.0.0-20180426214643-ce4c6f8deb07 +## explicit +github.com/bakins/logrus-middleware # github.com/djimenez/iconv-go v0.0.0-20160305225143-8960e66bd3da +## explicit github.com/djimenez/iconv-go # github.com/gogs/chardet v0.0.0-20150115103509-2404f7772561 +## explicit github.com/gogs/chardet # github.com/golang/protobuf v1.3.1 github.com/golang/protobuf/proto @@ -16,14 +26,18 @@ github.com/golang/protobuf/ptypes/any github.com/golang/protobuf/ptypes/duration github.com/golang/protobuf/ptypes/timestamp # github.com/joonix/log v0.0.0-20190524090622-13fe31bbdd7a +## explicit github.com/joonix/log # github.com/konsorten/go-windows-terminal-sequences v1.0.2 github.com/konsorten/go-windows-terminal-sequences # github.com/pkg/errors v0.8.1 +## explicit github.com/pkg/errors # github.com/sirupsen/logrus v1.4.2 +## explicit github.com/sirupsen/logrus # github.com/urfave/cli v1.21.0 +## explicit github.com/urfave/cli # golang.org/x/sys v0.0.0-20191204072324-ce4227a45e2e golang.org/x/sys/unix