|
1 | 1 | # logger-http |
2 | | -Gol4ng logger sub package for http |
| 2 | + |
| 3 | +[](https://travis-ci.org/gol4ng/logger-http) |
| 4 | +[](https://godoc.org/github.com/gol4ng/logger-http) |
| 5 | + |
| 6 | +Gol4ng logger sub package for logging http |
| 7 | +Related package [gol4ng/logger](https://github.com/gol4ng/logger) and [gol4ng/httpware](https://github.com/gol4ng/httpware) |
| 8 | + |
| 9 | +## Installation |
| 10 | + |
| 11 | +`go get -u github.com/gol4ng/logger-http` |
| 12 | + |
| 13 | +## Quick Start |
| 14 | + |
| 15 | +You can refer at [gol4ng/httpware](https://github.com/gol4ng/httpware) documentation for the middleware/tripperware usage |
| 16 | + |
| 17 | +### Tripperware |
| 18 | + |
| 19 | +Log you're `http.Client` request |
| 20 | + |
| 21 | +``` |
| 22 | +<debug> http client gonna GET http://google.com {"http_url":"http://google.com","http_start_time":"2019-12-13T17:01:13+01:00","http_kind":"client","Correlation-Id":"yhyBI94zyl","http_header":{"Correlation-Id":["yhyBI94zyl"]},"http_method":"GET"} |
| 23 | +<info> http client GET http://google.com [status_code:301, duration:39.70726ms, content_length:219] {"Correlation-Id":"yhyBI94zyl","http_start_time":"2019-12-13T17:01:13+01:00","http_method":"GET","http_duration":0.03970726,"http_response_length":219,"http_header":{"Correlation-Id":["yhyBI94zyl"]},"http_url":"http://google.com","http_status":"301 Moved Permanently","http_status_code":301,"http_kind":"client"} |
| 24 | +<debug> http client gonna GET http://www.google.com/ {"http_kind":"client","Correlation-Id":"uQzaMO9JC0","http_header":{"Correlation-Id":["uQzaMO9JC0"],"Referer":["http://google.com"]},"http_method":"GET","http_url":"http://www.google.com/","http_start_time":"2019-12-13T17:01:13+01:00"} |
| 25 | +<info> http client GET http://www.google.com/ [status_code:200, duration:72.582736ms, content_length:-1] {"Correlation-Id":"uQzaMO9JC0","http_header":{"Correlation-Id":["uQzaMO9JC0"],"Referer":["http://google.com"]},"http_method":"GET","http_kind":"client","http_response_length":-1,"http_duration":0.072582736,"http_status_code":200,"http_url":"http://www.google.com/","http_start_time":"2019-12-13T17:01:13+01:00","http_status":"200 OK"} |
| 26 | +``` |
| 27 | + |
| 28 | +```go |
| 29 | +package main |
| 30 | + |
| 31 | +import ( |
| 32 | + "net/http" |
| 33 | + "os" |
| 34 | + |
| 35 | + "github.com/gol4ng/httpware/v2" |
| 36 | + "github.com/gol4ng/logger" |
| 37 | + "github.com/gol4ng/logger-http/tripperware" |
| 38 | + "github.com/gol4ng/logger/formatter" |
| 39 | + "github.com/gol4ng/logger/handler" |
| 40 | +) |
| 41 | + |
| 42 | +func main(){ |
| 43 | + // logger will print on STDOUT with default line format |
| 44 | + myLogger := logger.NewLogger(handler.Stream(os.Stdout, formatter.NewDefaultFormatter())) |
| 45 | + |
| 46 | + clientStack := httpware.TripperwareStack( |
| 47 | + tripperware.InjectLogger(myLogger), |
| 48 | + tripperware.CorrelationId(), |
| 49 | + tripperware.Logger(myLogger), |
| 50 | + ) |
| 51 | + |
| 52 | + c := http.Client{ |
| 53 | + Transport: clientStack.DecorateRoundTripper(http.DefaultTransport), |
| 54 | + } |
| 55 | + |
| 56 | + c.Get("http://google.com") |
| 57 | + // Will log |
| 58 | + //<info> http client GET http://google.com [status_code:301, duration:27.524999ms, content_length:219] {"http_duration":0.027524999,"http_status":"301 Moved Permanently","http_status_code":301,"http_response_length":219,"http_method":"GET","http_url":"http://google.com","http_start_time":"2019-12-03T10:47:38+01:00","http_kind":"client"} |
| 59 | + //<info> http client GET http://www.google.com/ [status_code:200, duration:51.047002ms, content_length:-1] {"http_kind":"client","http_duration":0.051047002,"http_status":"200 OK","http_status_code":200,"http_response_length":-1,"http_method":"GET","http_url":"http://www.google.com/","http_start_time":"2019-12-03T10:47:38+01:00"} |
| 60 | +} |
| 61 | +``` |
| 62 | + |
| 63 | +### Middleware |
| 64 | +Log you're incoming http server request |
| 65 | + |
| 66 | +``` |
| 67 | +<debug> http server received GET / {"http_url":"/","http_start_time":"2019-12-13T17:15:30+01:00","http_kind":"server","Correlation-Id":"SBeEdhRhUl","http_header":{"Accept-Encoding":["gzip"],"Correlation-Id":["SBeEdhRhUl"],"User-Agent":["Go-http-client/1.1"]},"http_method":"GET"} |
| 68 | +<info> handler log info {"Correlation-Id":"SBeEdhRhUl"} |
| 69 | +<info> http server GET / [status_code:200, duration:290.156µs, content_length:0] {"http_kind":"server","http_duration":0.000290156,"http_status_code":200,"Correlation-Id":"SBeEdhRhUl","http_method":"GET","http_url":"/","http_start_time":"2019-12-13T17:15:30+01:00","http_status":"OK","http_response_length":0,"http_header":{"Accept-Encoding":["gzip"],"Correlation-Id":["SBeEdhRhUl"],"User-Agent":["Go-http-client/1.1"]}} |
| 70 | +``` |
| 71 | + |
| 72 | +```go |
| 73 | +package main |
| 74 | + |
| 75 | + import ( |
| 76 | + "context" |
| 77 | + "net" |
| 78 | + "net/http" |
| 79 | + "os" |
| 80 | + |
| 81 | + "github.com/gol4ng/httpware/v2" |
| 82 | + "github.com/gol4ng/logger" |
| 83 | + "github.com/gol4ng/logger-http/middleware" |
| 84 | + "github.com/gol4ng/logger/formatter" |
| 85 | + "github.com/gol4ng/logger/handler" |
| 86 | + ) |
| 87 | + |
| 88 | + func main() { |
| 89 | + addr := ":5001" |
| 90 | + |
| 91 | + myLogger := logger.NewLogger( |
| 92 | + handler.Stream(os.Stdout, formatter.NewDefaultFormatter()), |
| 93 | + ) |
| 94 | + |
| 95 | + // we recommend to use MiddlewareStack to simplify managing all wanted middlewares |
| 96 | + // caution middleware order matters |
| 97 | + stack := httpware.MiddlewareStack( |
| 98 | + //middleware.InjectLogger(myLogger), // we recommend to use http.Server.BaseContext instead of this middleware |
| 99 | + middleware.CorrelationId(), |
| 100 | + middleware.Logger(myLogger), |
| 101 | + ) |
| 102 | + |
| 103 | + h := http.HandlerFunc(func(writer http.ResponseWriter, innerRequest *http.Request) { |
| 104 | + l := logger.FromContext(innerRequest.Context(), myLogger) |
| 105 | + l.Info("handler log info", nil) |
| 106 | + }) |
| 107 | + |
| 108 | + server := http.Server{ |
| 109 | + Addr: addr, |
| 110 | + Handler: stack.DecorateHandler(h), |
| 111 | + BaseContext: func(listener net.Listener) context.Context { |
| 112 | + return logger.InjectInContext(context.Background(), myLogger) |
| 113 | + }, |
| 114 | + } |
| 115 | + |
| 116 | + go func() { |
| 117 | + if err := server.ListenAndServe(); err != nil { |
| 118 | + panic(err) |
| 119 | + } |
| 120 | + }() |
| 121 | + |
| 122 | + http.Get("http://localhost" + addr) |
| 123 | + |
| 124 | + //<debug> http server received GET / {"Correlation-Id":"zEDWO9gmZ6","http_header":{"Accept-Encoding":["gzip"],"Correlation-Id":["zEDWO9gmZ6"],"User-Agent":["Go-http-client/1.1"]},"http_method":"GET","http_url":"/","http_start_time":"2019-12-13T17:05:53+01:00","http_kind":"server"} |
| 125 | + //<info> handler log info {"Correlation-Id":"zEDWO9gmZ6"} |
| 126 | + //<info> http server GET / [status_code:200, duration:232.491µs, content_length:0] {"Correlation-Id":"zEDWO9gmZ6","http_header":{"Accept-Encoding":["gzip"],"Correlation-Id":["zEDWO9gmZ6"],"User-Agent":["Go-http-client/1.1"]},"http_method":"GET","http_url":"/","http_start_time":"2019-12-13T17:05:53+01:00","http_duration":0.000232491,"http_status":"OK","http_status_code":200,"http_kind":"server","http_response_length":0} |
| 127 | + } |
| 128 | +``` |
0 commit comments