log.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. // Copyright (c) 2020 Tailscale Inc & AUTHORS All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package tsweb
  5. import (
  6. "encoding/json"
  7. "strings"
  8. "time"
  9. )
  10. // AccessLogRecord is a record of one HTTP request served.
  11. type AccessLogRecord struct {
  12. // Timestamp at which request processing started.
  13. When time.Time `json:"when"`
  14. // Time it took to finish processing the request. It does not
  15. // include the entire lifetime of the underlying connection in
  16. // cases like connection hijacking, only the lifetime of the HTTP
  17. // request handler.
  18. Seconds float64 `json:"duration"`
  19. // The client's ip:port.
  20. RemoteAddr string `json:"remote_addr"`
  21. // The HTTP protocol version, usually "HTTP/1.1 or HTTP/2".
  22. Proto string `json:"proto"`
  23. // Whether the request was received over TLS.
  24. TLS bool `json:"tls"`
  25. // The target hostname in the request.
  26. Host string `json:"host"`
  27. // The HTTP method invoked.
  28. Method string `json:"method"`
  29. // The unescaped request URI, including query parameters.
  30. RequestURI string `json:"request_uri"`
  31. // The client's user-agent
  32. UserAgent string `json:"user_agent"`
  33. // Where the client was before making this request.
  34. Referer string `json:"referer"`
  35. // The HTTP response code sent to the client.
  36. Code int `json:"code"`
  37. // Number of bytes sent in response body to client. If the request
  38. // was hijacked, only includes bytes sent up to the point of
  39. // hijacking.
  40. Bytes int `json:"bytes"`
  41. // Error encountered during request processing.
  42. Err string `json:"err"`
  43. }
  44. // String returns m as a JSON string.
  45. func (m AccessLogRecord) String() string {
  46. if m.When.IsZero() {
  47. m.When = time.Now()
  48. }
  49. var buf strings.Builder
  50. json.NewEncoder(&buf).Encode(m)
  51. return strings.TrimRight(buf.String(), "\n")
  52. }