-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.go
41 lines (36 loc) · 807 Bytes
/
logger.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
package middlewares
import (
"log"
"net/http"
"os"
"time"
"grapi/core"
)
// Logger : Prints logs of the server
func Logger(inner http.Handler, name string, config core.Config) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
start := time.Now()
inner.ServeHTTP(w, r)
log.SetOutput(os.Stdout)
log.Printf("%s\t%s\t%s\t%s\t%s",
r.RemoteAddr,
r.Method,
r.RequestURI,
name,
time.Since(start))
if config.LogFile != "" {
f, err := os.OpenFile(config.LogFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
log.Printf("Error opening file: %v", err)
}
defer f.Close()
log.SetOutput(f)
log.Printf("%s\t%s\t%s\t%s\t%s",
r.RemoteAddr,
r.Method,
r.RequestURI,
name,
time.Since(start))
}
})
}