caddy/middleware/log/log.go

76 lines
2.2 KiB
Go
Raw Normal View History

// Package log implements request (access) logging middleware.
package log
2015-01-14 03:43:45 +08:00
import (
"fmt"
2015-01-14 03:43:45 +08:00
"log"
"net/http"
"github.com/mholt/caddy/middleware"
2015-01-14 03:43:45 +08:00
)
2015-05-25 10:52:34 +08:00
// Logger is a basic request logging middleware.
type Logger struct {
Next middleware.Handler
Rules []Rule
ErrorFunc func(http.ResponseWriter, *http.Request, int) // failover error handler
2015-03-29 06:50:42 +08:00
}
func (l Logger) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
for _, rule := range l.Rules {
if middleware.Path(r.URL.Path).Matches(rule.PathScope) {
// Record the response
2015-03-29 06:50:42 +08:00
responseRecorder := middleware.NewResponseRecorder(w)
// Attach the Replacer we'll use so that other middlewares can
// set their own placeholders if they want to.
rep := middleware.NewReplacer(r, responseRecorder, CommonLogEmptyValue)
responseRecorder.Replacer = rep
// Bon voyage, request!
status, err := l.Next.ServeHTTP(responseRecorder, r)
if status >= 400 {
// There was an error up the chain, but no response has been written yet.
// The error must be handled here so the log entry will record the response size.
if l.ErrorFunc != nil {
l.ErrorFunc(responseRecorder, r, status)
} else {
// Default failover error handler
responseRecorder.WriteHeader(status)
fmt.Fprintf(responseRecorder, "%d %s", status, http.StatusText(status))
}
status = 0
}
// Write log entry
2015-03-29 06:50:42 +08:00
rule.Log.Println(rep.Replace(rule.Format))
2015-03-29 06:50:42 +08:00
return status, err
2015-01-14 03:43:45 +08:00
}
}
return l.Next.ServeHTTP(w, r)
2015-03-29 06:50:42 +08:00
}
2015-01-14 03:43:45 +08:00
2015-05-25 10:52:34 +08:00
// Rule configures the logging middleware.
type Rule struct {
2015-03-29 06:50:42 +08:00
PathScope string
OutputFile string
Format string
Log *log.Logger
Roller *middleware.LogRoller
2015-01-14 03:43:45 +08:00
}
const (
// DefaultLogFilename is the default log filename.
DefaultLogFilename = "access.log"
// CommonLogFormat is the common log format.
CommonLogFormat = `{remote} ` + CommonLogEmptyValue + ` [{when}] "{method} {uri} {proto}" {status} {size}`
// CommonLogEmptyValue is the common empty log value.
CommonLogEmptyValue = "-"
// CombinedLogFormat is the combined log format.
CombinedLogFormat = CommonLogFormat + ` "{>Referer}" "{>User-Agent}"`
// DefaultLogFormat is the default log format.
DefaultLogFormat = CommonLogFormat
2015-01-14 03:43:45 +08:00
)