Skip to content

Commit

Permalink
Issue #80: Access logging
Browse files Browse the repository at this point in the history
Documentation, cleanup and polish.
  • Loading branch information
magiconair committed Apr 4, 2017
1 parent 005240a commit 14826e3
Show file tree
Hide file tree
Showing 6 changed files with 163 additions and 153 deletions.
73 changes: 37 additions & 36 deletions fabio.properties
Original file line number Diff line number Diff line change
Expand Up @@ -373,46 +373,47 @@

# log.access.format configures the format of the access log.
#
# Options are 'common' or 'combined' for either the common
# or combined access log file format. The are defined as follows:
# If the value is either 'common' or 'combined' then the logs are written in
# the Common Log Format or the Combined Log Format as defined below:
#
# 'common': $remote_host - - [$time_common] "$request" $response_status $response_body_size
# 'combined': $remote_host - - [$time_common] "$request" $response_status $response_body_size "$header.Referer" "$header.User-Agent"
#
# Alternatively, a custom format can be specified with
# the following parameters:
#
# header.<name> - request http header (name: [a-zA-Z0-9-]+)
# remote_addr - host:port of remote client
# remote_host - host of remote client
# remote_port - port of remote client
# request - request <method> <uri> <proto>
# request_args - request query parameters
# request_host - request host header (aka server name)
# request_method - request method
# request_scheme - request scheme
# request_uri - request URI
# request_url - request URL
# request_proto - request protocol
# response_body_size - response body size in bytes
# response_status - response status code
# response_time_ms - response time in S.sss format
# response_time_us - response time in S.ssssss format
# response_time_ns - response time in S.sssssssss format
# time_rfc3339 - log timestamp in YYYY-MM-DDTHH:MM:SSZ format
# time_rfc3339_ms - log timestamp in YYYY-MM-DDTHH:MM:SS.sssZ format
# time_rfc3339_us - log timestamp in YYYY-MM-DDTHH:MM:SS.ssssssZ format
# time_rfc3339_ns - log timestamp in YYYY-MM-DDTHH:MM:SS.sssssssssZ format
# time_unix_ms - log timestamp in unix epoch ms
# time_unix_us - log timestamp in unix epoch us
# time_unix_ns - log timestamp in unix epoch ns
# time_common - log timestamp in DD/MMM/YYYY:HH:MM:SS -ZZZZ
# upstream_addr - host:port of upstream server
# upstream_host - host of upstream server
# upstream_port - port of upstream server
# upstream_request_scheme - upstream request scheme
# upstream_request_uri - upstream request URI
# upstream_request_url - upstream request URL
# Otherwise, the value is interpreted as a custom log format which is defined
# with these parameters. Providing an empty format when logging is enabled is
# an error. To disable logging leave the log.access.target value empty.
#
# $header.<name> - request http header (name: [a-zA-Z0-9-]+)
# $remote_addr - host:port of remote client
# $remote_host - host of remote client
# $remote_port - port of remote client
# $request - request <method> <uri> <proto>
# $request_args - request query parameters
# $request_host - request host header (aka server name)
# $request_method - request method
# $request_scheme - request scheme
# $request_uri - request URI
# $request_url - request URL
# $request_proto - request protocol
# $response_body_size - response body size in bytes
# $response_status - response status code
# $response_time_ms - response time in S.sss format
# $response_time_us - response time in S.ssssss format
# $response_time_ns - response time in S.sssssssss format
# $time_rfc3339 - log timestamp in YYYY-MM-DDTHH:MM:SSZ format
# $time_rfc3339_ms - log timestamp in YYYY-MM-DDTHH:MM:SS.sssZ format
# $time_rfc3339_us - log timestamp in YYYY-MM-DDTHH:MM:SS.ssssssZ format
# $time_rfc3339_ns - log timestamp in YYYY-MM-DDTHH:MM:SS.sssssssssZ format
# $time_unix_ms - log timestamp in unix epoch ms
# $time_unix_us - log timestamp in unix epoch us
# $time_unix_ns - log timestamp in unix epoch ns
# $time_common - log timestamp in DD/MMM/YYYY:HH:MM:SS -ZZZZ
# $upstream_addr - host:port of upstream server
# $upstream_host - host of upstream server
# $upstream_port - port of upstream server
# $upstream_request_scheme - upstream request scheme
# $upstream_request_uri - upstream request URI
# $upstream_request_url - upstream request URL
#
# The default is
#
Expand Down
87 changes: 43 additions & 44 deletions logger/logger.go
Original file line number Diff line number Diff line change
@@ -1,39 +1,41 @@
// Package logger implements a configurable access logger.
//
// The access log format is defined through a format string which
// expands to a log line per request.
// The access log format is defined through a format string which expands to a
// log line per request. The values are taken as is and no quoting or escaping
// takes place. Text between two fields is printed verbatim. See the common
// log file formats for an example.
//
// header.<name> - request http header (name: [a-zA-Z0-9-]+)
// remote_addr - host:port of remote client
// remote_host - host of remote client
// remote_port - port of remote client
// request - request <method> <uri> <proto>
// request_args - request query parameters
// request_host - request host header (aka server name)
// request_method - request method
// request_scheme - request scheme
// request_uri - request URI
// request_url - request URL
// request_proto - request protocol
// response_body_size - response body size in bytes
// response_status - response status code
// response_time_ms - response time in S.sss format
// response_time_us - response time in S.ssssss format
// response_time_ns - response time in S.sssssssss format
// time_rfc3339 - log timestamp in YYYY-MM-DDTHH:MM:SSZ format
// time_rfc3339_ms - log timestamp in YYYY-MM-DDTHH:MM:SS.sssZ format
// time_rfc3339_us - log timestamp in YYYY-MM-DDTHH:MM:SS.ssssssZ format
// time_rfc3339_ns - log timestamp in YYYY-MM-DDTHH:MM:SS.sssssssssZ format
// time_unix_ms - log timestamp in unix epoch ms
// time_unix_us - log timestamp in unix epoch us
// time_unix_ns - log timestamp in unix epoch ns
// time_common - log timestamp in DD/MMM/YYYY:HH:MM:SS -ZZZZ
// upstream_addr - host:port of upstream server
// upstream_host - host of upstream server
// upstream_port - port of upstream server
// upstream_request_scheme - upstream request scheme
// upstream_request_uri - upstream request URI
// upstream_request_url - upstream request URL
// $header.<name> - request http header (name: [a-zA-Z0-9-]+)
// $remote_addr - host:port of remote client
// $remote_host - host of remote client
// $remote_port - port of remote client
// $request - request <method> <uri> <proto>
// $request_args - request query parameters
// $request_host - request host header (aka server name)
// $request_method - request method
// $request_scheme - request scheme
// $request_uri - request URI
// $request_url - request URL
// $request_proto - request protocol
// $response_body_size - response body size in bytes
// $response_status - response status code
// $response_time_ms - response time in S.sss format
// $response_time_us - response time in S.ssssss format
// $response_time_ns - response time in S.sssssssss format
// $time_rfc3339 - log timestamp in YYYY-MM-DDTHH:MM:SSZ format
// $time_rfc3339_ms - log timestamp in YYYY-MM-DDTHH:MM:SS.sssZ format
// $time_rfc3339_us - log timestamp in YYYY-MM-DDTHH:MM:SS.ssssssZ format
// $time_rfc3339_ns - log timestamp in YYYY-MM-DDTHH:MM:SS.sssssssssZ format
// $time_unix_ms - log timestamp in unix epoch ms
// $time_unix_us - log timestamp in unix epoch us
// $time_unix_ns - log timestamp in unix epoch ns
// $time_common - log timestamp in DD/MMM/YYYY:HH:MM:SS -ZZZZ
// $upstream_addr - host:port of upstream server
// $upstream_host - host of upstream server
// $upstream_port - port of upstream server
// $upstream_request_scheme - upstream request scheme
// $upstream_request_uri - upstream request URI
// $upstream_request_url - upstream request URL
//
package logger

Expand All @@ -48,12 +50,10 @@ import (
"time"
)

// Common log file formats.
const (
// Common defines the Apache Common Log file format.
Common = `$remote_host - - [$time_common] "$request" $response_status $response_body_size`

// Combined defines the Apache Combined Log file format.
Combined = `$remote_host - - [$time_common] "$request" $response_status $response_body_size "$header.Referer" "$header.User-Agent"`
CommonFormat = `$remote_host - - [$time_common] "$request" $response_status $response_body_size`
CombinedFormat = `$remote_host - - [$time_common] "$request" $response_status $response_body_size "$header.Referer" "$header.User-Agent"`
)

// Event defines the elements of a loggable event.
Expand Down Expand Up @@ -86,11 +86,10 @@ type Logger interface {
Log(*Event)
}

// New creates a new logger that writes log events in the
// given format to the provided writer. If the format
// is empty or invalid an error is returned.
// New creates a new logger that writes log events in the given format to the
// provided writer. If the format is empty or invalid an error is returned.
func New(w io.Writer, format string) (Logger, error) {
p, err := parse(format, Fields)
p, err := parse(format, fields)
if err != nil {
return nil, err
}
Expand All @@ -111,12 +110,12 @@ type logger struct {
}

// BufSize defines the default size of the log buffers.
const BufSize = 1024
const bufSize = 1024

// pool provides a reusable set of log buffers.
var pool = sync.Pool{
New: func() interface{} {
return bytes.NewBuffer(make([]byte, 0, BufSize))
return bytes.NewBuffer(make([]byte, 0, bufSize))
},
}

Expand Down
4 changes: 2 additions & 2 deletions logger/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ func BenchmarkLog(b *testing.B) {
// BenchmarkLog/go_text/template-8 100000 19026 ns/op 848 B/op 76 allocs/op
b.Run("custom parser", func(b *testing.B) {
var keys []string
for k := range Fields {
for k := range fields {
keys = append(keys, k)
}
sort.Strings(keys)
Expand All @@ -234,7 +234,7 @@ func BenchmarkLog(b *testing.B) {
// the same number of fields as for the other parser
// but using the same value.
tmpl := ""
for i := 0; i < len(Fields); i++ {
for i := 0; i < len(fields); i++ {
tmpl += "{{.Req.RemoteAddr}}"
}
t := template.Must(template.New("log").Parse(tmpl))
Expand Down
Loading

0 comments on commit 14826e3

Please sign in to comment.