0
Fork 0
mirror of https://github.com/caddyserver/caddy.git synced 2024-12-23 22:27:38 -05:00
caddy/middleware/log/log.go

42 lines
1 KiB
Go
Raw Normal View History

2015-03-28 17:50:42 -05:00
// Package log implements basic but useful request (access) logging middleware.
package log
2015-01-13 14:43:45 -05:00
import (
"log"
"net/http"
"github.com/mholt/caddy/middleware"
2015-01-13 14:43:45 -05:00
)
type Logger struct {
Next middleware.Handler
Rules []LogRule
2015-03-28 17:50:42 -05: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) {
responseRecorder := middleware.NewResponseRecorder(w)
status, err := l.Next.ServeHTTP(responseRecorder, r)
2015-03-28 17:50:42 -05:00
rep := middleware.NewReplacer(r, responseRecorder)
rule.Log.Println(rep.Replace(rule.Format))
return status, err
2015-01-13 14:43:45 -05:00
}
}
return l.Next.ServeHTTP(w, r)
2015-03-28 17:50:42 -05:00
}
2015-01-13 14:43:45 -05:00
2015-03-28 17:50:42 -05:00
type LogRule struct {
PathScope string
OutputFile string
Format string
Log *log.Logger
2015-01-13 14:43:45 -05:00
}
const (
DefaultLogFilename = "access.log"
CommonLogFormat = `{remote} ` + middleware.EmptyStringReplacer + ` [{when}] "{method} {uri} {proto}" {status} {size}`
CombinedLogFormat = CommonLogFormat + ` "{>Referer}" "{>User-Agent}"`
DefaultLogFormat = CommonLogFormat
2015-01-13 14:43:45 -05:00
)