1
0

trafficlog.go 1.0 KB

1234567891011121314151617181920212223242526272829303132
  1. package logger
  2. /*
  3. Traffic Log
  4. This script log the traffic of HTTP requests
  5. */
  6. import (
  7. "net/http"
  8. "strconv"
  9. "time"
  10. "imuslab.com/zoraxy/mod/netutils"
  11. )
  12. // Log HTTP request. Note that this must run in go routine to prevent any blocking
  13. // in reverse proxy router
  14. func (l *Logger) LogHTTPRequest(r *http.Request, reqclass string, statusCode int) {
  15. go func() {
  16. l.ValidateAndUpdateLogFilepath()
  17. if l.logger == nil || l.file == nil {
  18. //logger is not initiated. Do not log http request
  19. return
  20. }
  21. clientIP := netutils.GetRequesterIP(r)
  22. requestURI := r.RequestURI
  23. statusCodeString := strconv.Itoa(statusCode)
  24. //fmt.Println("[" + time.Now().Format("2006-01-02 15:04:05.000000") + "] [router:" + reqclass + "] [client " + clientIP + "] " + r.Method + " " + requestURI + " " + statusCodeString)
  25. l.logger.Println("[" + time.Now().Format("2006-01-02 15:04:05.000000") + "] [router:" + reqclass + "] [origin:" + r.URL.Hostname() + "] [client " + clientIP + "] " + r.Method + " " + requestURI + " " + statusCodeString)
  26. }()
  27. }