Skip to content

Commit e1bc069

Browse files
committed
Update readme with example
1 parent ebe6d68 commit e1bc069

File tree

1 file changed

+85
-2
lines changed

1 file changed

+85
-2
lines changed

README.md

Lines changed: 85 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# aaronvb/logrequest
2-
Package `aaronvb/logrequest` is a Go middleware log output inspired by the Ruby on Rails log out for requests. Example output:
2+
Package `aaronvb/logrequest` is a Go middleware log output inspired by the Ruby on Rails log output for requests. Example output:
33

44
```sh
55
Started GET "/" 127.0.0.1:12345 HTTP/1.1
@@ -11,4 +11,87 @@ The output can directly sent to `log.Logger` or to a map[string]string with the
1111
## Install
1212
```sh
1313
go get -u github.com/aaronvb/logrequest
14-
```
14+
```
15+
16+
## Middleware Example (using [gorilla/mux](https://github.com/gorilla/mux))
17+
```go
18+
package main
19+
20+
import (
21+
"fmt"
22+
"log"
23+
"net/http"
24+
"os"
25+
"time"
26+
27+
"github.com/aaronvb/logrequest"
28+
29+
"github.com/gorilla/mux"
30+
)
31+
32+
type application struct {
33+
errorLog *log.Logger
34+
infoLog *log.Logger
35+
}
36+
37+
func main() {
38+
infoLog := log.New(os.Stdout, "INFO\t", log.Ldate|log.Ltime)
39+
errorLog := log.New(os.Stderr, "ERROR\t", log.Ldate|log.Ltime|log.Lshortfile)
40+
41+
app := &application{
42+
errorLog: errorLog,
43+
infoLog: infoLog,
44+
}
45+
46+
srv := &http.Server{
47+
Addr: ":8080",
48+
ErrorLog: errorLog,
49+
Handler: app.routes(),
50+
}
51+
52+
infoLog.Printf("Starting server on %s", ":8080")
53+
err := srv.ListenAndServe()
54+
errorLog.Fatal(err)
55+
}
56+
57+
func (app *application) routes() http.Handler {
58+
r := mux.NewRouter()
59+
r.HandleFunc("/foobar", app.foobar).Methods("GET")
60+
61+
// Middleware
62+
r.Use(app.logRequest)
63+
64+
return r
65+
}
66+
67+
func (app *application) foobar(w http.ResponseWriter, r *http.Request) {
68+
time.Sleep(300 * time.Millisecond)
69+
fmt.Fprintln(w, "Hello world")
70+
}
71+
72+
// Middleware
73+
74+
func (app *application) logRequest(next http.Handler) http.Handler {
75+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
76+
lr := logrequest.LogRequest{Request: r, Writer: w, Handler: next}
77+
lr.ToLogger(app.infoLog)
78+
})
79+
}
80+
```
81+
82+
```sh
83+
> go run main.go
84+
INFO 2020/03/31 22:40:09 Starting server on :8080
85+
INFO 2020/03/31 22:40:13 Started GET "/foobar" [::1]:55044 HTTP/1.1
86+
INFO 2020/03/31 22:40:13 Completed 200 in 300.131639ms
87+
INFO 2020/03/31 22:40:18 Started GET "/foobar" [::1]:55044 HTTP/1.1
88+
INFO 2020/03/31 22:40:18 Completed 200 in 302.047625ms
89+
```
90+
91+
## Showing Parameters
92+
```sh
93+
INFO 2020/03/31 22:40:13 Started GET "/foobar" [::1]:55044 HTTP/1.1
94+
INFO 2020/03/31 22:40:13 Parameters: {"foo" => "bar"}
95+
INFO 2020/03/31 22:40:13 Completed 200 in 300.131639ms
96+
```
97+
Check out my other middleware package to output incoming parameters, which is also influenced by the Ruby on Rails logger: [https://github.com/aaronvb/logparams](https://github.com/aaronvb/logparams)

0 commit comments

Comments
 (0)