Added request/response logger to main for expansion
This commit is contained in:
committed by
=Michael Hohn
parent
d57f9a855f
commit
2a80ff0bc2
38
main.go
38
main.go
@@ -22,10 +22,46 @@ THE SOFTWARE.
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/GitHubSecurityLab/gh-mrva/cmd"
|
||||
_ "github.com/motemen/go-loghttp/global"
|
||||
|
||||
"github.com/motemen/go-loghttp"
|
||||
"github.com/motemen/go-nuts/roundtime"
|
||||
)
|
||||
|
||||
func main() {
|
||||
var transport = &loghttp.Transport{
|
||||
Transport: http.DefaultTransport,
|
||||
LogRequest: LogRequestDump,
|
||||
LogResponse: LogResponseDump,
|
||||
}
|
||||
|
||||
http.DefaultTransport = transport
|
||||
|
||||
cmd.Execute()
|
||||
}
|
||||
|
||||
// Used if transport.LogRequest is not set.
|
||||
func LogRequestDump(req *http.Request) {
|
||||
log.Printf(">> %s %s", req.Method, req.URL)
|
||||
}
|
||||
|
||||
type contextKey struct {
|
||||
name string
|
||||
}
|
||||
|
||||
var ContextKeyRequestStart = &contextKey{"RequestStart"}
|
||||
|
||||
// Used if transport.LogResponse is not set.
|
||||
func LogResponseDump(resp *http.Response) {
|
||||
ctx := resp.Request.Context()
|
||||
if start, ok := ctx.Value(ContextKeyRequestStart).(time.Time); ok {
|
||||
log.Printf("<< %d %s (%s)", resp.StatusCode, resp.Request.URL,
|
||||
roundtime.Duration(time.Since(start), 2))
|
||||
} else {
|
||||
log.Printf("<< %d %s", resp.StatusCode, resp.Request.URL)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user