caddy/middleware/log/log.go

125 lines
2.7 KiB
Go
Raw Normal View History

2015-03-29 06:50:42 +08:00
// Package log implements basic but useful request (access) logging middleware.
package log
2015-01-14 03:43:45 +08:00
import (
"log"
"net/http"
"os"
"github.com/mholt/caddy/middleware"
2015-01-14 03:43:45 +08:00
)
// New instantiates a new instance of logging middleware.
func New(c middleware.Controller) (middleware.Middleware, error) {
2015-03-29 06:50:42 +08:00
rules, err := parse(c)
if err != nil {
return nil, err
}
2015-03-29 06:50:42 +08:00
// Open the log files for writing when the server starts
c.Startup(func() error {
for i := 0; i < len(rules); i++ {
var err error
var file *os.File
2015-03-29 06:50:42 +08:00
if rules[i].OutputFile == "stdout" {
file = os.Stdout
} else if rules[i].OutputFile == "stderr" {
file = os.Stderr
} else {
file, err = os.OpenFile(rules[i].OutputFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0644)
if err != nil {
return err
}
}
rules[i].Log = log.New(file, "", 0)
}
2015-03-29 06:50:42 +08:00
return nil
})
return func(next middleware.Handler) middleware.Handler {
return Logger{Next: next, Rules: rules}
2015-03-29 06:50:42 +08:00
}, nil
}
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-29 06:50:42 +08:00
rep := middleware.NewReplacer(r, responseRecorder)
rule.Log.Println(rep.Replace(rule.Format))
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-03-29 06:50:42 +08:00
func parse(c middleware.Controller) ([]LogRule, error) {
var rules []LogRule
for c.Next() {
args := c.RemainingArgs()
2015-03-29 06:50:42 +08:00
if len(args) == 0 {
// Nothing specified; use defaults
rules = append(rules, LogRule{
PathScope: "/",
OutputFile: defaultLogFilename,
Format: defaultLogFormat,
})
} else if len(args) == 1 {
// Only an output file specified
rules = append(rules, LogRule{
PathScope: "/",
OutputFile: args[0],
Format: defaultLogFormat,
})
} else {
2015-03-29 06:50:42 +08:00
// Path scope, output file, and maybe a format specified
format := defaultLogFormat
if len(args) > 2 {
switch args[2] {
case "{common}":
format = commonLogFormat
case "{combined}":
format = combinedLogFormat
default:
format = args[2]
2015-03-29 06:50:42 +08:00
}
}
2015-03-29 06:50:42 +08:00
rules = append(rules, LogRule{
PathScope: args[0],
OutputFile: args[1],
Format: format,
})
}
2015-03-29 06:50:42 +08:00
}
2015-03-29 06:50:42 +08:00
return rules, nil
}
2015-03-29 06:50:42 +08:00
type Logger struct {
Next middleware.Handler
2015-03-29 06:50:42 +08:00
Rules []LogRule
}
type LogRule struct {
PathScope string
OutputFile string
Format string
Log *log.Logger
2015-01-14 03:43:45 +08:00
}
const (
2015-03-29 06:50:42 +08:00
defaultLogFilename = "access.log"
commonLogFormat = `{remote} ` + middleware.EmptyStringReplacer + ` [{when}] "{method} {uri} {proto}" {status} {size}`
combinedLogFormat = commonLogFormat + ` "{>Referer}" "{>User-Agent}"`
defaultLogFormat = commonLogFormat
2015-01-14 03:43:45 +08:00
)