2011-01-29 19:53:22 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2011-01-29 20:31:35 +00:00
|
|
|
"log"
|
2012-02-21 10:11:28 +00:00
|
|
|
"net/http"
|
2011-01-29 19:53:22 +00:00
|
|
|
"os"
|
2011-01-29 20:17:59 +00:00
|
|
|
"strings"
|
2011-01-29 19:53:22 +00:00
|
|
|
"time"
|
2015-08-16 16:39:10 +00:00
|
|
|
|
2016-09-23 23:38:12 +00:00
|
|
|
"cloud.google.com/go/logging"
|
2011-01-29 19:53:22 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type logRecord struct {
|
2011-02-08 21:29:53 +00:00
|
|
|
http.ResponseWriter
|
|
|
|
|
2012-02-21 10:11:28 +00:00
|
|
|
time time.Time
|
2011-01-29 19:53:22 +00:00
|
|
|
ip, method, rawpath string
|
|
|
|
responseBytes int64
|
|
|
|
responseStatus int
|
|
|
|
userAgent, referer string
|
2011-01-29 20:17:59 +00:00
|
|
|
proto string // "HTTP/1.1"
|
2011-01-29 19:53:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type logHandler struct {
|
|
|
|
handler http.Handler
|
2015-08-16 12:30:09 +00:00
|
|
|
log WebLogger
|
2011-01-29 20:31:35 +00:00
|
|
|
|
2015-08-16 12:30:09 +00:00
|
|
|
ch chan *logRecord
|
|
|
|
}
|
|
|
|
|
|
|
|
type WebLogger interface {
|
|
|
|
// LogEvent is called serially from the same goroutine.
|
|
|
|
LogEvent(*logRecord)
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewApacheLogger(dir string, writeStdout bool) WebLogger {
|
|
|
|
return &apacheLogger{
|
|
|
|
dir: dir,
|
|
|
|
stdout: writeStdout,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type apacheLogger struct {
|
2011-01-29 20:31:35 +00:00
|
|
|
dir string // or "" to not log
|
|
|
|
stdout bool
|
2015-08-16 12:30:09 +00:00
|
|
|
|
|
|
|
// stateful parts:
|
|
|
|
lastFileName string
|
|
|
|
logFile *os.File
|
2011-01-29 19:53:22 +00:00
|
|
|
}
|
|
|
|
|
2015-08-16 12:30:09 +00:00
|
|
|
func (al *apacheLogger) LogEvent(lr *logRecord) {
|
|
|
|
// [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())
|
|
|
|
|
|
|
|
if al.dir != "" {
|
|
|
|
fileName := fmt.Sprintf("%s/%04d-%02d-%02d%s%02d.log", al.dir,
|
|
|
|
lr.time.Year(), lr.time.Month(), lr.time.Day(), "h", lr.time.Hour())
|
|
|
|
if fileName > al.lastFileName {
|
|
|
|
if al.logFile != nil {
|
|
|
|
al.logFile.Close()
|
|
|
|
}
|
|
|
|
var err error
|
|
|
|
al.logFile, err = os.OpenFile(fileName, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0644)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Error opening %q: %v", fileName, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
al.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,
|
|
|
|
)
|
|
|
|
if al.stdout {
|
|
|
|
os.Stdout.WriteString(logLine)
|
|
|
|
}
|
|
|
|
if al.logFile != nil {
|
|
|
|
al.logFile.WriteString(logLine)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewLoggingHandler(h http.Handler, wl WebLogger) http.Handler {
|
|
|
|
lh := &logHandler{
|
2011-01-29 20:17:59 +00:00
|
|
|
ch: make(chan *logRecord, 1000),
|
2015-08-16 12:30:09 +00:00
|
|
|
handler: h,
|
|
|
|
log: wl,
|
2011-01-29 19:53:22 +00:00
|
|
|
}
|
2015-08-16 12:30:09 +00:00
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
lh.log.LogEvent(<-lh.ch)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
return lh
|
2011-01-29 19:53:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *logHandler) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
|
2011-01-29 20:17:59 +00:00
|
|
|
// Strip port number from address
|
2015-08-16 12:30:09 +00:00
|
|
|
// TODO(bradfitz): IPv6 stuff
|
2011-03-12 20:32:40 +00:00
|
|
|
addr := r.RemoteAddr
|
2011-01-29 20:17:59 +00:00
|
|
|
if colon := strings.LastIndex(addr, ":"); colon != -1 {
|
|
|
|
addr = addr[:colon]
|
|
|
|
}
|
|
|
|
|
2011-01-29 19:53:22 +00:00
|
|
|
lr := &logRecord{
|
2012-02-21 10:11:28 +00:00
|
|
|
time: time.Now().UTC(),
|
2011-01-29 20:17:59 +00:00
|
|
|
ip: addr,
|
2011-01-29 19:53:22 +00:00
|
|
|
method: r.Method,
|
2012-02-21 10:11:28 +00:00
|
|
|
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,
|
2011-01-29 20:17:59 +00:00
|
|
|
proto: r.Proto,
|
2011-02-08 21:29:53 +00:00
|
|
|
ResponseWriter: rw,
|
2011-01-29 19:53:22 +00:00
|
|
|
}
|
|
|
|
h.handler.ServeHTTP(lr, r)
|
|
|
|
h.ch <- lr
|
|
|
|
}
|
|
|
|
|
2015-08-16 12:30:09 +00:00
|
|
|
var monthAbbr = [12]string{
|
|
|
|
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
|
|
|
|
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
|
2011-01-29 19:53:22 +00:00
|
|
|
}
|
|
|
|
|
2012-02-21 10:11:28 +00:00
|
|
|
func (lr *logRecord) Write(p []byte) (int, error) {
|
2011-02-08 21:29:53 +00:00
|
|
|
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
|
2011-02-08 21:29:53 +00:00
|
|
|
lr.ResponseWriter.WriteHeader(status)
|
2011-01-29 19:53:22 +00:00
|
|
|
}
|
2015-08-16 16:39:10 +00:00
|
|
|
|
|
|
|
type gceLogger struct {
|
2017-01-30 23:06:28 +00:00
|
|
|
c *logging.Logger
|
2015-08-16 16:39:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (lg gceLogger) LogEvent(lr *logRecord) {
|
|
|
|
lg.c.Log(logging.Entry{
|
2017-01-30 23:06:28 +00:00
|
|
|
Timestamp: lr.time,
|
2015-08-16 16:39:10 +00:00
|
|
|
Payload: map[string]interface{}{
|
|
|
|
"ip": lr.ip,
|
|
|
|
"path": lr.rawpath,
|
|
|
|
"method": lr.method,
|
|
|
|
"responseBytes": lr.responseBytes,
|
|
|
|
"status": lr.responseStatus,
|
|
|
|
"userAgent": lr.userAgent,
|
|
|
|
"referer": lr.referer,
|
|
|
|
"proto": lr.proto,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|