mirror of https://github.com/perkeep/perkeep.git
camweb: use Apache's Combined Log Format
This commit is contained in:
parent
71e5d719d5
commit
876266f680
|
@ -6,15 +6,17 @@ import (
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"http"
|
"http"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type logRecord struct {
|
type logRecord struct {
|
||||||
timeEpochNs int64
|
time *time.Time
|
||||||
ip, method, rawpath string
|
ip, method, rawpath string
|
||||||
responseBytes int64
|
responseBytes int64
|
||||||
responseStatus int
|
responseStatus int
|
||||||
userAgent, referer string
|
userAgent, referer string
|
||||||
|
proto string // "HTTP/1.1"
|
||||||
|
|
||||||
rw http.ResponseWriter
|
rw http.ResponseWriter
|
||||||
}
|
}
|
||||||
|
@ -27,7 +29,7 @@ type logHandler struct {
|
||||||
|
|
||||||
func NewLoggingHandler(handler http.Handler, dir string) http.Handler {
|
func NewLoggingHandler(handler http.Handler, dir string) http.Handler {
|
||||||
h := &logHandler{
|
h := &logHandler{
|
||||||
ch: make(chan *logRecord),
|
ch: make(chan *logRecord, 1000),
|
||||||
dir: dir,
|
dir: dir,
|
||||||
handler: handler,
|
handler: handler,
|
||||||
}
|
}
|
||||||
|
@ -36,25 +38,53 @@ func NewLoggingHandler(handler http.Handler, dir string) http.Handler {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *logHandler) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
|
func (h *logHandler) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
|
||||||
|
// Strip port number from address
|
||||||
|
addr := rw.RemoteAddr()
|
||||||
|
if colon := strings.LastIndex(addr, ":"); colon != -1 {
|
||||||
|
addr = addr[:colon]
|
||||||
|
}
|
||||||
|
|
||||||
lr := &logRecord{
|
lr := &logRecord{
|
||||||
timeEpochNs: time.Nanoseconds(),
|
time: time.UTC(),
|
||||||
ip: rw.RemoteAddr(),
|
ip: addr,
|
||||||
method: r.Method,
|
method: r.Method,
|
||||||
rawpath: r.URL.RawPath,
|
rawpath: r.URL.RawPath,
|
||||||
userAgent: r.UserAgent,
|
userAgent: r.UserAgent,
|
||||||
referer: r.Referer,
|
referer: r.Referer,
|
||||||
responseStatus: http.StatusOK,
|
responseStatus: http.StatusOK,
|
||||||
|
proto: r.Proto,
|
||||||
rw: rw,
|
rw: rw,
|
||||||
}
|
}
|
||||||
h.handler.ServeHTTP(lr, r)
|
h.handler.ServeHTTP(lr, r)
|
||||||
h.ch <- lr
|
h.ch <- lr
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var monthAbbr = [12]string{"Jan", "Feb", "Mar", "Apr", "May", "Jun",
|
||||||
|
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}
|
||||||
|
|
||||||
func (h *logHandler) logFromChannel() {
|
func (h *logHandler) logFromChannel() {
|
||||||
for {
|
for {
|
||||||
lr := <-h.ch
|
lr := <-h.ch
|
||||||
lr.rw = nil
|
lr.rw = nil
|
||||||
logLine := fmt.Sprintf("Request: %#v\n", lr)
|
|
||||||
|
// [10/Oct/2000:13:55:36 -0700]
|
||||||
|
dateString := fmt.Sprintf("%02d/%s/%04d:%02d:%02d:%02d -0000",
|
||||||
|
lr.time.Day,
|
||||||
|
monthAbbr[lr.time.Month-1],
|
||||||
|
lr.time.Year,
|
||||||
|
lr.time.Hour, lr.time.Minute, lr.time.Second)
|
||||||
|
|
||||||
|
// Combined Log Format
|
||||||
|
// http://httpd.apache.org/docs/1.3/logs.html#combined
|
||||||
|
logLine := fmt.Sprintf("%s - - [%s] %q %d %d %q %q\n",
|
||||||
|
lr.ip,
|
||||||
|
dateString,
|
||||||
|
lr.method+" "+lr.rawpath+" "+lr.proto,
|
||||||
|
lr.responseStatus,
|
||||||
|
lr.responseBytes,
|
||||||
|
lr.referer,
|
||||||
|
lr.userAgent,
|
||||||
|
)
|
||||||
if h.dir == "-" {
|
if h.dir == "-" {
|
||||||
os.Stdout.WriteString(logLine)
|
os.Stdout.WriteString(logLine)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue