-
Notifications
You must be signed in to change notification settings - Fork 619
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue #80: Add access logging support
Original PR #131 by [email protected] This patch adds support for configurable access logging which is compatible with nginx config parameters.
- Loading branch information
1 parent
9e292c5
commit b52ced3
Showing
8 changed files
with
255 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
package logger | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"strings" | ||
"sync" | ||
"time" | ||
) | ||
|
||
const BufferSize = 1024 | ||
|
||
type Pattern func(w io.Writer, t time.Time, r *http.Request) | ||
|
||
var ( | ||
pool = sync.Pool{ | ||
New: func() interface{} { | ||
return bytes.NewBuffer(make([]byte, 0, BufferSize)) | ||
}, | ||
} | ||
patterns = map[string]Pattern{ | ||
"remote_addr": func(w io.Writer, t time.Time, r *http.Request) { | ||
io.WriteString(w, r.RemoteAddr[:strings.Index(r.RemoteAddr, ":")]) | ||
}, | ||
"time": func(w io.Writer, t time.Time, r *http.Request) { | ||
io.WriteString(w, t.Format(time.RFC3339)) | ||
}, | ||
"request": func(w io.Writer, t time.Time, r *http.Request) { | ||
io.WriteString(w, "\""+r.Method+" "+r.URL.Path+" "+r.Proto+"\"") | ||
}, | ||
"body_bytes_sent": func(w io.Writer, t time.Time, r *http.Request) { | ||
io.WriteString(w, fmt.Sprintf("%d", uint64(r.ContentLength))) | ||
}, | ||
"http_referer": func(w io.Writer, t time.Time, r *http.Request) { | ||
io.WriteString(w, r.Referer()) | ||
}, | ||
"http_user_agent": func(w io.Writer, t time.Time, r *http.Request) { | ||
io.WriteString(w, r.UserAgent()) | ||
}, | ||
"http_x_forwarded_for": func(w io.Writer, t time.Time, r *http.Request) { | ||
io.WriteString(w, r.Header.Get("X-Forwarded-For")) | ||
}, | ||
"server_name": func(w io.Writer, t time.Time, r *http.Request) { | ||
io.WriteString(w, r.Host) | ||
}, | ||
"proxy_endpoint": func(w io.Writer, t time.Time, r *http.Request) { | ||
io.WriteString(w, r.URL.Host) | ||
}, | ||
"response_time": func(w io.Writer, t time.Time, r *http.Request) { | ||
io.WriteString(w, fmt.Sprintf("%.4f", time.Since(t).Seconds())) | ||
}, | ||
"request_args": func(w io.Writer, t time.Time, r *http.Request) { | ||
io.WriteString(w, r.URL.RawQuery) | ||
}, | ||
} | ||
) | ||
|
||
type Logger struct { | ||
p []Pattern | ||
|
||
// w is the log destination | ||
w io.Writer | ||
|
||
// mu guards w | ||
mu sync.Mutex | ||
} | ||
|
||
func New(w io.Writer, format string) (*Logger, error) { | ||
p, err := parse(format) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
//format can't empty | ||
if p == nil || len(p) == 0 { | ||
return nil, fmt.Errorf("Invalid Logger format %s", format) | ||
} | ||
|
||
return &Logger{w: w, p: p}, nil | ||
} | ||
|
||
func parse(format string) ([]Pattern, error) { | ||
var pp []Pattern | ||
|
||
for _, f := range strings.Fields(format) { | ||
p := patterns[f] | ||
if p == nil { | ||
return nil, fmt.Errorf("Invalid log field \"%s\"", f) | ||
} | ||
pp = append(pp, p) | ||
|
||
} | ||
|
||
return pp, nil | ||
} | ||
|
||
func (l *Logger) Log(t time.Time, r *http.Request) { | ||
b := pool.Get().(*bytes.Buffer) | ||
b.Reset() | ||
|
||
for _, p := range l.p { | ||
p(b, t, r) | ||
b.WriteRune(' ') | ||
} | ||
b.Truncate(b.Len() - 1) //drop last space | ||
b.WriteRune('\n') | ||
|
||
l.mu.Lock() | ||
l.w.Write(b.Bytes()) | ||
l.mu.Unlock() | ||
pool.Put(b) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package logger | ||
|
||
import ( | ||
"bytes" | ||
"net/http" | ||
"net/url" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestLog(t *testing.T) { | ||
ts := time.Date(2016, 1, 1, 0, 0, 0, 0, time.UTC) | ||
|
||
tests := []struct { | ||
req *http.Request | ||
format string | ||
out string | ||
}{ | ||
{ | ||
&http.Request{ | ||
RequestURI: "/", | ||
Header: http.Header{"X-Forwarded-For": {"3.3.3.3"}}, | ||
RemoteAddr: "2.2.2.2:666", | ||
URL: &url.URL{}, | ||
Method: "GET", | ||
Proto: "HTTP/1.1", | ||
}, | ||
"remote_addr time request body_bytes_sent http_x_forwarded_for", | ||
"2.2.2.2 2016-01-01T00:00:00Z \"GET HTTP/1.1\" 0 3.3.3.3\n", | ||
}, | ||
} | ||
|
||
for i, tt := range tests { | ||
b := new(bytes.Buffer) | ||
l, err := New(b, tt.format) | ||
|
||
if err != nil { | ||
t.Fatalf("%d: got %v want nil", i, err) | ||
} | ||
|
||
l.Log(ts, tt.req) | ||
|
||
if got, want := string(b.Bytes()), tt.out; got != want { | ||
t.Errorf("%d: got %q want %q", i, got, want) | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package proxy | ||
|
||
import ( | ||
"fmt" | ||
"github.com/eBay/fabio/logger" | ||
"io" | ||
"log" | ||
"os" | ||
) | ||
|
||
func newLogger(target string, format string) (*logger.Logger, error) { | ||
var w io.Writer | ||
|
||
switch target { | ||
case "stdout": | ||
log.Printf("[INFO] Output logger to stdout") | ||
w = os.Stdout | ||
case "": | ||
log.Printf("[INFO] Logger disabled") | ||
return nil, nil | ||
default: | ||
return nil, fmt.Errorf("Invalid Logger target %s", target) | ||
} | ||
|
||
return logger.New(w, format) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters