logger.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. // Licensed to Elasticsearch B.V. under one or more contributor
  2. // license agreements. See the NOTICE file distributed with
  3. // this work for additional information regarding copyright
  4. // ownership. Elasticsearch B.V. licenses this file to you under
  5. // the Apache License, Version 2.0 (the "License"); you may
  6. // not use this file except in compliance with the License.
  7. // You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing,
  12. // software distributed under the License is distributed on an
  13. // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. // KIND, either express or implied. See the License for the
  15. // specific language governing permissions and limitations
  16. // under the License.
  17. package estransport
  18. import (
  19. "bufio"
  20. "bytes"
  21. "encoding/json"
  22. "fmt"
  23. "io"
  24. "io/ioutil"
  25. "net/http"
  26. "net/url"
  27. "strconv"
  28. "strings"
  29. "time"
  30. )
  31. var debugLogger DebuggingLogger
  32. // Logger defines an interface for logging request and response.
  33. //
  34. type Logger interface {
  35. // LogRoundTrip should not modify the request or response, except for consuming and closing the body.
  36. // Implementations have to check for nil values in request and response.
  37. LogRoundTrip(*http.Request, *http.Response, error, time.Time, time.Duration) error
  38. // RequestBodyEnabled makes the client pass a copy of request body to the logger.
  39. RequestBodyEnabled() bool
  40. // ResponseBodyEnabled makes the client pass a copy of response body to the logger.
  41. ResponseBodyEnabled() bool
  42. }
  43. // DebuggingLogger defines the interface for a debugging logger.
  44. //
  45. type DebuggingLogger interface {
  46. Log(a ...interface{}) error
  47. Logf(format string, a ...interface{}) error
  48. }
  49. // TextLogger prints the log message in plain text.
  50. //
  51. type TextLogger struct {
  52. Output io.Writer
  53. EnableRequestBody bool
  54. EnableResponseBody bool
  55. }
  56. // ColorLogger prints the log message in a terminal-optimized plain text.
  57. //
  58. type ColorLogger struct {
  59. Output io.Writer
  60. EnableRequestBody bool
  61. EnableResponseBody bool
  62. }
  63. // CurlLogger prints the log message as a runnable curl command.
  64. //
  65. type CurlLogger struct {
  66. Output io.Writer
  67. EnableRequestBody bool
  68. EnableResponseBody bool
  69. }
  70. // JSONLogger prints the log message as JSON.
  71. //
  72. type JSONLogger struct {
  73. Output io.Writer
  74. EnableRequestBody bool
  75. EnableResponseBody bool
  76. }
  77. // debuggingLogger prints debug messages as plain text.
  78. //
  79. type debuggingLogger struct {
  80. Output io.Writer
  81. }
  82. // LogRoundTrip prints the information about request and response.
  83. //
  84. func (l *TextLogger) LogRoundTrip(req *http.Request, res *http.Response, err error, start time.Time, dur time.Duration) error {
  85. fmt.Fprintf(l.Output, "%s %s %s [status:%d request:%s]\n",
  86. start.Format(time.RFC3339),
  87. req.Method,
  88. req.URL.String(),
  89. resStatusCode(res),
  90. dur.Truncate(time.Millisecond),
  91. )
  92. if l.RequestBodyEnabled() && req != nil && req.Body != nil && req.Body != http.NoBody {
  93. var buf bytes.Buffer
  94. if req.GetBody != nil {
  95. b, _ := req.GetBody()
  96. buf.ReadFrom(b)
  97. } else {
  98. buf.ReadFrom(req.Body)
  99. }
  100. logBodyAsText(l.Output, &buf, ">")
  101. }
  102. if l.ResponseBodyEnabled() && res != nil && res.Body != nil && res.Body != http.NoBody {
  103. defer res.Body.Close()
  104. var buf bytes.Buffer
  105. buf.ReadFrom(res.Body)
  106. logBodyAsText(l.Output, &buf, "<")
  107. }
  108. if err != nil {
  109. fmt.Fprintf(l.Output, "! ERROR: %v\n", err)
  110. }
  111. return nil
  112. }
  113. // RequestBodyEnabled returns true when the request body should be logged.
  114. func (l *TextLogger) RequestBodyEnabled() bool { return l.EnableRequestBody }
  115. // ResponseBodyEnabled returns true when the response body should be logged.
  116. func (l *TextLogger) ResponseBodyEnabled() bool { return l.EnableResponseBody }
  117. // LogRoundTrip prints the information about request and response.
  118. //
  119. func (l *ColorLogger) LogRoundTrip(req *http.Request, res *http.Response, err error, start time.Time, dur time.Duration) error {
  120. query, _ := url.QueryUnescape(req.URL.RawQuery)
  121. if query != "" {
  122. query = "?" + query
  123. }
  124. var (
  125. status string
  126. color string
  127. )
  128. status = res.Status
  129. switch {
  130. case res.StatusCode > 0 && res.StatusCode < 300:
  131. color = "\x1b[32m"
  132. case res.StatusCode > 299 && res.StatusCode < 500:
  133. color = "\x1b[33m"
  134. case res.StatusCode > 499:
  135. color = "\x1b[31m"
  136. default:
  137. status = "ERROR"
  138. color = "\x1b[31;4m"
  139. }
  140. fmt.Fprintf(l.Output, "%6s \x1b[1;4m%s://%s%s\x1b[0m%s %s%s\x1b[0m \x1b[2m%s\x1b[0m\n",
  141. req.Method,
  142. req.URL.Scheme,
  143. req.URL.Host,
  144. req.URL.Path,
  145. query,
  146. color,
  147. status,
  148. dur.Truncate(time.Millisecond),
  149. )
  150. if l.RequestBodyEnabled() && req != nil && req.Body != nil && req.Body != http.NoBody {
  151. var buf bytes.Buffer
  152. if req.GetBody != nil {
  153. b, _ := req.GetBody()
  154. buf.ReadFrom(b)
  155. } else {
  156. buf.ReadFrom(req.Body)
  157. }
  158. fmt.Fprint(l.Output, "\x1b[2m")
  159. logBodyAsText(l.Output, &buf, " »")
  160. fmt.Fprint(l.Output, "\x1b[0m")
  161. }
  162. if l.ResponseBodyEnabled() && res != nil && res.Body != nil && res.Body != http.NoBody {
  163. defer res.Body.Close()
  164. var buf bytes.Buffer
  165. buf.ReadFrom(res.Body)
  166. fmt.Fprint(l.Output, "\x1b[2m")
  167. logBodyAsText(l.Output, &buf, " «")
  168. fmt.Fprint(l.Output, "\x1b[0m")
  169. }
  170. if err != nil {
  171. fmt.Fprintf(l.Output, "\x1b[31;1m» ERROR \x1b[31m%v\x1b[0m\n", err)
  172. }
  173. if l.RequestBodyEnabled() || l.ResponseBodyEnabled() {
  174. fmt.Fprintf(l.Output, "\x1b[2m%s\x1b[0m\n", strings.Repeat("─", 80))
  175. }
  176. return nil
  177. }
  178. // RequestBodyEnabled returns true when the request body should be logged.
  179. func (l *ColorLogger) RequestBodyEnabled() bool { return l.EnableRequestBody }
  180. // ResponseBodyEnabled returns true when the response body should be logged.
  181. func (l *ColorLogger) ResponseBodyEnabled() bool { return l.EnableResponseBody }
  182. // LogRoundTrip prints the information about request and response.
  183. //
  184. func (l *CurlLogger) LogRoundTrip(req *http.Request, res *http.Response, err error, start time.Time, dur time.Duration) error {
  185. var b bytes.Buffer
  186. var query string
  187. qvalues := url.Values{}
  188. for k, v := range req.URL.Query() {
  189. if k == "pretty" {
  190. continue
  191. }
  192. for _, qv := range v {
  193. qvalues.Add(k, qv)
  194. }
  195. }
  196. if len(qvalues) > 0 {
  197. query = qvalues.Encode()
  198. }
  199. b.WriteString(`curl`)
  200. if req.Method == "HEAD" {
  201. b.WriteString(" --head")
  202. } else {
  203. fmt.Fprintf(&b, " -X %s", req.Method)
  204. }
  205. if len(req.Header) > 0 {
  206. for k, vv := range req.Header {
  207. if k == "Authorization" || k == "User-Agent" {
  208. continue
  209. }
  210. v := strings.Join(vv, ",")
  211. b.WriteString(fmt.Sprintf(" -H '%s: %s'", k, v))
  212. }
  213. }
  214. b.WriteString(" '")
  215. b.WriteString(req.URL.Scheme)
  216. b.WriteString("://")
  217. b.WriteString(req.URL.Host)
  218. b.WriteString(req.URL.Path)
  219. b.WriteString("?pretty")
  220. if query != "" {
  221. fmt.Fprintf(&b, "&%s", query)
  222. }
  223. b.WriteString("'")
  224. if req != nil && req.Body != nil && req.Body != http.NoBody {
  225. var buf bytes.Buffer
  226. if req.GetBody != nil {
  227. b, _ := req.GetBody()
  228. buf.ReadFrom(b)
  229. } else {
  230. buf.ReadFrom(req.Body)
  231. }
  232. b.Grow(buf.Len())
  233. b.WriteString(" -d \\\n'")
  234. json.Indent(&b, buf.Bytes(), "", " ")
  235. b.WriteString("'")
  236. }
  237. b.WriteRune('\n')
  238. var status string
  239. status = res.Status
  240. fmt.Fprintf(&b, "# => %s [%s] %s\n", start.UTC().Format(time.RFC3339), status, dur.Truncate(time.Millisecond))
  241. if l.ResponseBodyEnabled() && res != nil && res.Body != nil && res.Body != http.NoBody {
  242. var buf bytes.Buffer
  243. buf.ReadFrom(res.Body)
  244. b.Grow(buf.Len())
  245. b.WriteString("# ")
  246. json.Indent(&b, buf.Bytes(), "# ", " ")
  247. }
  248. b.WriteString("\n")
  249. if l.ResponseBodyEnabled() && res != nil && res.Body != nil && res.Body != http.NoBody {
  250. b.WriteString("\n")
  251. }
  252. b.WriteTo(l.Output)
  253. return nil
  254. }
  255. // RequestBodyEnabled returns true when the request body should be logged.
  256. func (l *CurlLogger) RequestBodyEnabled() bool { return l.EnableRequestBody }
  257. // ResponseBodyEnabled returns true when the response body should be logged.
  258. func (l *CurlLogger) ResponseBodyEnabled() bool { return l.EnableResponseBody }
  259. // LogRoundTrip prints the information about request and response.
  260. //
  261. func (l *JSONLogger) LogRoundTrip(req *http.Request, res *http.Response, err error, start time.Time, dur time.Duration) error {
  262. // https://github.com/elastic/ecs/blob/master/schemas/http.yml
  263. //
  264. // TODO(karmi): Research performance optimization of using sync.Pool
  265. bsize := 200
  266. var b = bytes.NewBuffer(make([]byte, 0, bsize))
  267. var v = make([]byte, 0, bsize)
  268. appendTime := func(t time.Time) {
  269. v = v[:0]
  270. v = t.AppendFormat(v, time.RFC3339)
  271. b.Write(v)
  272. }
  273. appendQuote := func(s string) {
  274. v = v[:0]
  275. v = strconv.AppendQuote(v, s)
  276. b.Write(v)
  277. }
  278. appendInt := func(i int64) {
  279. v = v[:0]
  280. v = strconv.AppendInt(v, i, 10)
  281. b.Write(v)
  282. }
  283. port := req.URL.Port()
  284. b.WriteRune('{')
  285. // -- Timestamp
  286. b.WriteString(`"@timestamp":"`)
  287. appendTime(start.UTC())
  288. b.WriteRune('"')
  289. // -- Event
  290. b.WriteString(`,"event":{`)
  291. b.WriteString(`"duration":`)
  292. appendInt(dur.Nanoseconds())
  293. b.WriteRune('}')
  294. // -- URL
  295. b.WriteString(`,"url":{`)
  296. b.WriteString(`"scheme":`)
  297. appendQuote(req.URL.Scheme)
  298. b.WriteString(`,"domain":`)
  299. appendQuote(req.URL.Hostname())
  300. if port != "" {
  301. b.WriteString(`,"port":`)
  302. b.WriteString(port)
  303. }
  304. b.WriteString(`,"path":`)
  305. appendQuote(req.URL.Path)
  306. b.WriteString(`,"query":`)
  307. appendQuote(req.URL.RawQuery)
  308. b.WriteRune('}') // Close "url"
  309. // -- HTTP
  310. b.WriteString(`,"http":`)
  311. // ---- Request
  312. b.WriteString(`{"request":{`)
  313. b.WriteString(`"method":`)
  314. appendQuote(req.Method)
  315. if l.RequestBodyEnabled() && req != nil && req.Body != nil && req.Body != http.NoBody {
  316. var buf bytes.Buffer
  317. if req.GetBody != nil {
  318. b, _ := req.GetBody()
  319. buf.ReadFrom(b)
  320. } else {
  321. buf.ReadFrom(req.Body)
  322. }
  323. b.Grow(buf.Len() + 8)
  324. b.WriteString(`,"body":`)
  325. appendQuote(buf.String())
  326. }
  327. b.WriteRune('}') // Close "http.request"
  328. // ---- Response
  329. b.WriteString(`,"response":{`)
  330. b.WriteString(`"status_code":`)
  331. appendInt(int64(resStatusCode(res)))
  332. if l.ResponseBodyEnabled() && res != nil && res.Body != nil && res.Body != http.NoBody {
  333. defer res.Body.Close()
  334. var buf bytes.Buffer
  335. buf.ReadFrom(res.Body)
  336. b.Grow(buf.Len() + 8)
  337. b.WriteString(`,"body":`)
  338. appendQuote(buf.String())
  339. }
  340. b.WriteRune('}') // Close "http.response"
  341. b.WriteRune('}') // Close "http"
  342. // -- Error
  343. if err != nil {
  344. b.WriteString(`,"error":{"message":`)
  345. appendQuote(err.Error())
  346. b.WriteRune('}') // Close "error"
  347. }
  348. b.WriteRune('}')
  349. b.WriteRune('\n')
  350. b.WriteTo(l.Output)
  351. return nil
  352. }
  353. // RequestBodyEnabled returns true when the request body should be logged.
  354. func (l *JSONLogger) RequestBodyEnabled() bool { return l.EnableRequestBody }
  355. // ResponseBodyEnabled returns true when the response body should be logged.
  356. func (l *JSONLogger) ResponseBodyEnabled() bool { return l.EnableResponseBody }
  357. // Log prints the arguments to output in default format.
  358. //
  359. func (l *debuggingLogger) Log(a ...interface{}) error {
  360. _, err := fmt.Fprint(l.Output, a...)
  361. return err
  362. }
  363. // Logf prints formats the arguments and prints them to output.
  364. //
  365. func (l *debuggingLogger) Logf(format string, a ...interface{}) error {
  366. _, err := fmt.Fprintf(l.Output, format, a...)
  367. return err
  368. }
  369. func logBodyAsText(dst io.Writer, body io.Reader, prefix string) {
  370. scanner := bufio.NewScanner(body)
  371. for scanner.Scan() {
  372. s := scanner.Text()
  373. if s != "" {
  374. fmt.Fprintf(dst, "%s %s\n", prefix, s)
  375. }
  376. }
  377. }
  378. func duplicateBody(body io.ReadCloser) (io.ReadCloser, io.ReadCloser, error) {
  379. var (
  380. b1 bytes.Buffer
  381. b2 bytes.Buffer
  382. tr = io.TeeReader(body, &b2)
  383. )
  384. _, err := b1.ReadFrom(tr)
  385. if err != nil {
  386. return ioutil.NopCloser(io.MultiReader(&b1, errorReader{err: err})), ioutil.NopCloser(io.MultiReader(&b2, errorReader{err: err})), err
  387. }
  388. defer func() { body.Close() }()
  389. return ioutil.NopCloser(&b1), ioutil.NopCloser(&b2), nil
  390. }
  391. func resStatusCode(res *http.Response) int {
  392. if res == nil {
  393. return -1
  394. }
  395. return res.StatusCode
  396. }
  397. type errorReader struct{ err error }
  398. func (r errorReader) Read(p []byte) (int, error) { return 0, r.err }