forked from bitmovin/bitmovin-api-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.go
62 lines (50 loc) · 1.22 KB
/
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package log
import (
"encoding/json"
"fmt"
"net/http"
)
type RequestLogInfo struct {
URL string
Method string
RawBody []byte
Headers http.Header
}
type ResponseLogInfo struct {
URL string
Method string
RawBody []byte
Headers http.Header
StatusCode int
}
type Logger interface {
LogRequest(reqInfo RequestLogInfo)
LogResponse(respInfo ResponseLogInfo)
}
type StdOutLogger struct {
}
func (c *StdOutLogger) LogRequest(reqInfo RequestLogInfo) {
headerStr, _ := json.Marshal(reqInfo.Headers)
logStr := fmt.Sprintf("\n#### REQUEST ####\n"+
"Url: %s - %s\n"+
"Headers: %s\n",
reqInfo.Method, reqInfo.URL, headerStr)
if len(reqInfo.RawBody) > 0 {
logStr += fmt.Sprintf("Payload: %s\n", reqInfo.RawBody)
}
logStr += "#### REQUEST END ####\n"
fmt.Print(logStr)
}
func (c *StdOutLogger) LogResponse(respInfo ResponseLogInfo) {
headerStr, _ := json.Marshal(respInfo.Headers)
logStr := fmt.Sprintf("\n#### RESPONSE ####\n"+
"Url: %s - %s\n"+
"Status: %d\n"+
"Headers: %s\n",
respInfo.Method, respInfo.URL, respInfo.StatusCode, headerStr)
if len(respInfo.RawBody) > 0 {
logStr += fmt.Sprintf("Payload: %s\n", respInfo.RawBody)
}
logStr += "#### RESPONSE END ####\n"
fmt.Print(logStr)
}