2015-03-29 06:50:42 +08:00
|
|
|
// Package log implements basic but useful request (access) logging middleware.
|
2015-01-30 13:05:21 +08:00
|
|
|
package log
|
2015-01-14 03:43:45 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"net/http"
|
2015-01-19 14:11:21 +08:00
|
|
|
"os"
|
2015-01-30 13:05:21 +08:00
|
|
|
|
|
|
|
"github.com/mholt/caddy/middleware"
|
2015-01-14 03:43:45 +08:00
|
|
|
)
|
|
|
|
|
2015-01-30 13:05:21 +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-01-19 14:11:21 +08:00
|
|
|
|
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-01-19 14:11:21 +08:00
|
|
|
|
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-01-19 14:11:21 +08:00
|
|
|
}
|
2015-03-29 06:50:42 +08:00
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
2015-04-03 13:30:54 +08:00
|
|
|
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)
|
2015-04-03 13:30:54 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
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-03-29 06:50:42 +08:00
|
|
|
func parse(c middleware.Controller) ([]LogRule, error) {
|
|
|
|
var rules []LogRule
|
|
|
|
|
|
|
|
for c.Next() {
|
|
|
|
args := c.RemainingArgs()
|
2015-01-19 14:11:21 +08:00
|
|
|
|
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,
|
|
|
|
})
|
2015-01-19 14:11:21 +08:00
|
|
|
} 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
|
2015-04-24 03:35:21 +08:00
|
|
|
default:
|
|
|
|
format = args[2]
|
2015-03-29 06:50:42 +08:00
|
|
|
}
|
2015-01-19 14:11:21 +08:00
|
|
|
}
|
2015-03-29 06:50:42 +08:00
|
|
|
|
|
|
|
rules = append(rules, LogRule{
|
|
|
|
PathScope: args[0],
|
|
|
|
OutputFile: args[1],
|
|
|
|
Format: format,
|
|
|
|
})
|
2015-01-19 14:11:21 +08:00
|
|
|
}
|
2015-03-29 06:50:42 +08:00
|
|
|
}
|
2015-01-19 14:11:21 +08:00
|
|
|
|
2015-03-29 06:50:42 +08:00
|
|
|
return rules, nil
|
|
|
|
}
|
2015-01-19 14:11:21 +08:00
|
|
|
|
2015-03-29 06:50:42 +08:00
|
|
|
type Logger struct {
|
2015-04-03 13:30:54 +08:00
|
|
|
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
|
|
|
)
|