-
Notifications
You must be signed in to change notification settings - Fork 2
/
logHandler.go
43 lines (34 loc) · 1.34 KB
/
logHandler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package templates
import (
"context"
"log/syslog"
"net/http"
"log"
"github.com/TobiEiss/goMiddlewareChain"
"github.com/julienschmidt/httprouter"
)
// LogHandler log requests.
// Use this handler to log every call to console.
func LogHandler(response *goMiddlewareChain.Response, request *http.Request, params httprouter.Params) {
log.Println(request.RemoteAddr, "-", request.Method, "-", request.Host, "-", request.URL, "-", request.Header)
}
// LogContextHandler log requests
// Use this context-handler to log every call to console.
func LogContextHandler(ctx context.Context, response *goMiddlewareChain.Response, request *http.Request, params httprouter.Params) context.Context {
LogHandler(response, request, params)
return ctx
}
// LoggerHandler holds the logger-instance
type LoggerHandler struct {
Writer *syslog.Writer
}
// NewLoggerHandler creates a new LoggerHandler
func NewLoggerHandler(writer *syslog.Writer) *LoggerHandler {
return &LoggerHandler{Writer: writer}
}
// LoggerContextKey a key to map the logger
var LoggerContextKey = goMiddlewareChain.ContextKey{Key: ""}
// Handle is the handler for the LoggerHandler
func (handler *LoggerHandler) Handle(ctx context.Context, response *goMiddlewareChain.Response, request *http.Request, params httprouter.Params) context.Context {
return context.WithValue(ctx, LoggerContextKey, handler)
}