mirror of
https://github.com/muety/wakapi.git
synced 2023-08-10 21:12:56 +03:00
chore: change logging middleware to use different output
This commit is contained in:
parent
708863fd33
commit
759e8e4dfd
5
main.go
5
main.go
@ -171,10 +171,7 @@ func main() {
|
|||||||
|
|
||||||
// Globally used middlewares
|
// Globally used middlewares
|
||||||
recoveryMiddleware := handlers.RecoveryHandler()
|
recoveryMiddleware := handlers.RecoveryHandler()
|
||||||
loggingMiddleware := middlewares.NewLoggingMiddleware(
|
loggingMiddleware := middlewares.NewLoggingMiddleware(logbuch.Info, []string{"/assets"})
|
||||||
log.New(os.Stdout, "", log.LstdFlags),
|
|
||||||
[]string{"/assets"},
|
|
||||||
)
|
|
||||||
|
|
||||||
// Router configs
|
// Router configs
|
||||||
router.Use(loggingMiddleware, recoveryMiddleware)
|
router.Use(loggingMiddleware, recoveryMiddleware)
|
||||||
|
@ -4,23 +4,24 @@ package middlewares
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"io"
|
"io"
|
||||||
"log"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type logFunc func(string, ...interface{})
|
||||||
|
|
||||||
type LoggingMiddleware struct {
|
type LoggingMiddleware struct {
|
||||||
handler http.Handler
|
handler http.Handler
|
||||||
output *log.Logger
|
logFunc logFunc
|
||||||
excludePrefixes []string
|
excludePrefixes []string
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewLoggingMiddleware(output *log.Logger, excludePrefixes []string) func(http.Handler) http.Handler {
|
func NewLoggingMiddleware(logFunc logFunc, excludePrefixes []string) func(http.Handler) http.Handler {
|
||||||
return func(h http.Handler) http.Handler {
|
return func(h http.Handler) http.Handler {
|
||||||
return &LoggingMiddleware{
|
return &LoggingMiddleware{
|
||||||
handler: h,
|
handler: h,
|
||||||
output: output,
|
logFunc: logFunc,
|
||||||
excludePrefixes: excludePrefixes,
|
excludePrefixes: excludePrefixes,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -41,9 +42,8 @@ func (lg *LoggingMiddleware) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
lg.output.Printf(
|
lg.logFunc(
|
||||||
"%v status=%d, method=%s, uri=%s, duration=%v, bytes=%d, addr=%s\n",
|
"[request] status=%d, method=%s, uri=%s, duration=%v, bytes=%d, addr=%s, user=%s\n",
|
||||||
time.Now().Format(time.RFC3339Nano),
|
|
||||||
ww.Status(),
|
ww.Status(),
|
||||||
r.Method,
|
r.Method,
|
||||||
r.URL.String(),
|
r.URL.String(),
|
||||||
|
@ -1 +1 @@
|
|||||||
1.24.1
|
1.24.2
|
Loading…
Reference in New Issue
Block a user