perkeep/website/logging.go

127 lines
2.8 KiB
Go
Raw Normal View History

2011-01-29 19:53:22 +00:00
package main
import (
"fmt"
2011-01-29 20:31:35 +00:00
"log"
"net/http"
2011-01-29 19:53:22 +00:00
"os"
"strings"
2011-01-29 19:53:22 +00:00
"time"
)
type logRecord struct {
http.ResponseWriter
time time.Time
2011-01-29 19:53:22 +00:00
ip, method, rawpath string
responseBytes int64
responseStatus int
userAgent, referer string
proto string // "HTTP/1.1"
2011-01-29 19:53:22 +00:00
}
type logHandler struct {
ch chan *logRecord
handler http.Handler
2011-01-29 20:31:35 +00:00
dir string // or "" to not log
stdout bool
2011-01-29 19:53:22 +00:00
}
2011-01-29 20:31:35 +00:00
func NewLoggingHandler(handler http.Handler, dir string, writeStdout bool) http.Handler {
2011-01-29 19:53:22 +00:00
h := &logHandler{
ch: make(chan *logRecord, 1000),
2011-01-29 19:53:22 +00:00
dir: dir,
handler: handler,
2011-01-29 20:31:35 +00:00
stdout: writeStdout,
2011-01-29 19:53:22 +00:00
}
go h.logFromChannel()
return h
}
func (h *logHandler) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
// Strip port number from address
addr := r.RemoteAddr
if colon := strings.LastIndex(addr, ":"); colon != -1 {
addr = addr[:colon]
}
2011-01-29 19:53:22 +00:00
lr := &logRecord{
time: time.Now().UTC(),
ip: addr,
2011-01-29 19:53:22 +00:00
method: r.Method,
rawpath: r.URL.RequestURI(),
2011-06-17 01:38:21 +00:00
userAgent: r.UserAgent(),
referer: r.Referer(),
2011-01-29 19:53:22 +00:00
responseStatus: http.StatusOK,
proto: r.Proto,
ResponseWriter: rw,
2011-01-29 19:53:22 +00:00
}
h.handler.ServeHTTP(lr, r)
h.ch <- lr
}
var monthAbbr = [12]string{"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}
2011-01-29 19:53:22 +00:00
func (h *logHandler) logFromChannel() {
2011-01-29 20:31:35 +00:00
lastFileName := ""
var logFile *os.File
2011-01-29 19:53:22 +00:00
for {
lr := <-h.ch
// [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())
2011-01-29 20:31:35 +00:00
if h.dir != "" {
fileName := fmt.Sprintf("%s/%04d-%02d-%02d%s%02d.log", h.dir,
lr.time.Year(), lr.time.Month(), lr.time.Day(), "h", lr.time.Hour())
2011-01-29 20:31:35 +00:00
if fileName > lastFileName {
if logFile != nil {
logFile.Close()
}
var err error
2011-04-07 17:58:29 +00:00
logFile, err = os.OpenFile(fileName, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0644)
2011-01-29 20:31:35 +00:00
if err != nil {
log.Printf("Error opening %q: %v", fileName, err)
continue
}
lastFileName = fileName
}
}
// 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,
)
2011-01-29 20:31:35 +00:00
if h.stdout {
2011-01-29 19:53:22 +00:00
os.Stdout.WriteString(logLine)
}
2011-01-29 20:31:35 +00:00
if logFile != nil {
logFile.WriteString(logLine)
}
2011-01-29 19:53:22 +00:00
}
}
func (lr *logRecord) Write(p []byte) (int, error) {
written, err := lr.ResponseWriter.Write(p)
2011-01-29 19:53:22 +00:00
lr.responseBytes += int64(written)
return written, err
}
func (lr *logRecord) WriteHeader(status int) {
lr.responseStatus = status
lr.ResponseWriter.WriteHeader(status)
2011-01-29 19:53:22 +00:00
}