Blame view

middleware_log.go 646 Bytes
feb3471e7   Marko Tikvić   refactored loggin...
1
2
3
4
5
6
7
8
  package webutility
  
  import (
  	"net/http"
  	"time"
  
  	"git.to-net.rs/marko.tikvic/gologger"
  )
f38e87cf4   Marko Tikvić   status recorder
9
  var httpLogger *gologger.Logger
feb3471e7   Marko Tikvić   refactored loggin...
10
11
  
  func EnableLogging(log string) (err error) {
f38e87cf4   Marko Tikvić   status recorder
12
  	httpLogger, err = gologger.New(log, gologger.MaxLogSize5MB)
feb3471e7   Marko Tikvić   refactored loggin...
13
14
15
16
17
18
19
20
  	return err
  }
  
  func Log(h http.HandlerFunc) http.HandlerFunc {
  	return func(w http.ResponseWriter, req *http.Request) {
  		t1 := time.Now()
  
  		claims, _ := GetTokenClaims(req)
f38e87cf4   Marko Tikvić   status recorder
21
  		in := httpLogger.LogHTTPRequest(req, claims.Username)
feb3471e7   Marko Tikvić   refactored loggin...
22

f38e87cf4   Marko Tikvić   status recorder
23
  		rec := NewStatusRecorder(w)
feb3471e7   Marko Tikvić   refactored loggin...
24

f38e87cf4   Marko Tikvić   status recorder
25
  		h(rec, req)
feb3471e7   Marko Tikvić   refactored loggin...
26
27
  
  		t2 := time.Now()
f38e87cf4   Marko Tikvić   status recorder
28
  		out := httpLogger.LogHTTPResponse(rec.Status(), t2.Sub(t1), rec.Size())
feb3471e7   Marko Tikvić   refactored loggin...
29

f38e87cf4   Marko Tikvić   status recorder
30
  		httpLogger.CombineHTTPLogs(in, out)
feb3471e7   Marko Tikvić   refactored loggin...
31
32
  	}
  }