2016-03-21 11:56:13 +08:00
|
|
|
// Package log implements request (access) logging middleware.
|
2015-01-30 13:05:21 +08:00
|
|
|
package log
|
2015-01-14 03:43:45 +08:00
|
|
|
|
|
|
|
import (
|
2015-06-13 00:21:19 +08:00
|
|
|
"fmt"
|
2015-01-14 03:43:45 +08:00
|
|
|
"log"
|
|
|
|
"net/http"
|
2015-01-30 13:05:21 +08:00
|
|
|
|
|
|
|
"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.
|
2015-05-04 20:53:54 +08:00
|
|
|
type Logger struct {
|
2015-06-16 00:17:09 +08:00
|
|
|
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) {
|
2016-03-21 11:56:13 +08:00
|
|
|
// Record the response
|
2015-03-29 06:50:42 +08:00
|
|
|
responseRecorder := middleware.NewResponseRecorder(w)
|
2016-03-21 11:56:13 +08:00
|
|
|
|
|
|
|
// 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!
|
2015-04-03 13:30:54 +08:00
|
|
|
status, err := l.Next.ServeHTTP(responseRecorder, r)
|
2016-03-21 11:56:13 +08:00
|
|
|
|
2015-06-13 00:21:19 +08:00
|
|
|
if status >= 400 {
|
2015-06-16 00:17:09 +08:00
|
|
|
// 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))
|
|
|
|
}
|
2015-06-13 00:21:19 +08:00
|
|
|
status = 0
|
|
|
|
}
|
2016-03-21 11:56:13 +08:00
|
|
|
|
|
|
|
// Write log entry
|
2015-03-29 06:50:42 +08:00
|
|
|
rule.Log.Println(rep.Replace(rule.Format))
|
2016-03-21 11:56:13 +08:00
|
|
|
|
2015-03-29 06:50:42 +08:00
|
|
|
return status, err
|
2015-01-14 03:43:45 +08:00
|
|
|
}
|
|
|
|
}
|
2015-04-03 13:30:54 +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
|
2015-09-02 21:13:31 +08:00
|
|
|
Roller *middleware.LogRoller
|
2015-01-14 03:43:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
2015-11-03 03:28:50 +08:00
|
|
|
// 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.
|
2015-07-25 00:11:34 +08:00
|
|
|
CommonLogEmptyValue = "-"
|
2015-11-03 03:28:50 +08:00
|
|
|
// 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
|
|
|
)
|