mirror of
https://github.com/coredns/coredns.git
synced 2025-12-08 19:35:10 -05:00
Remove the word middleware (#1067)
* Rename middleware to plugin first pass; mostly used 'sed', few spots where I manually changed text. This still builds a coredns binary. * fmt error * Rename AddMiddleware to AddPlugin * Readd AddMiddleware to remain backwards compat
This commit is contained in:
91
plugin/log/log.go
Normal file
91
plugin/log/log.go
Normal file
@@ -0,0 +1,91 @@
|
||||
// Package log implements basic but useful request (access) logging plugin.
|
||||
package log
|
||||
|
||||
import (
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"github.com/coredns/coredns/plugin"
|
||||
"github.com/coredns/coredns/plugin/metrics/vars"
|
||||
"github.com/coredns/coredns/plugin/pkg/dnsrecorder"
|
||||
"github.com/coredns/coredns/plugin/pkg/rcode"
|
||||
"github.com/coredns/coredns/plugin/pkg/replacer"
|
||||
"github.com/coredns/coredns/plugin/pkg/response"
|
||||
"github.com/coredns/coredns/request"
|
||||
|
||||
"github.com/miekg/dns"
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
// Logger is a basic request logging plugin.
|
||||
type Logger struct {
|
||||
Next plugin.Handler
|
||||
Rules []Rule
|
||||
ErrorFunc func(dns.ResponseWriter, *dns.Msg, int) // failover error handler
|
||||
}
|
||||
|
||||
// ServeDNS implements the plugin.Handler interface.
|
||||
func (l Logger) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
|
||||
state := request.Request{W: w, Req: r}
|
||||
for _, rule := range l.Rules {
|
||||
if !plugin.Name(rule.NameScope).Matches(state.Name()) {
|
||||
continue
|
||||
}
|
||||
|
||||
rrw := dnsrecorder.New(w)
|
||||
rc, err := plugin.NextOrFailure(l.Name(), l.Next, ctx, rrw, r)
|
||||
|
||||
if rc > 0 {
|
||||
// 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(rrw, r, rc)
|
||||
} else {
|
||||
answer := new(dns.Msg)
|
||||
answer.SetRcode(r, rc)
|
||||
state.SizeAndDo(answer)
|
||||
|
||||
vars.Report(state, vars.Dropped, rcode.ToString(rc), answer.Len(), time.Now())
|
||||
|
||||
w.WriteMsg(answer)
|
||||
}
|
||||
rc = 0
|
||||
}
|
||||
|
||||
tpe, _ := response.Typify(rrw.Msg, time.Now().UTC())
|
||||
class := response.Classify(tpe)
|
||||
if rule.Class == response.All || rule.Class == class {
|
||||
rep := replacer.New(r, rrw, CommonLogEmptyValue)
|
||||
rule.Log.Println(rep.Replace(rule.Format))
|
||||
}
|
||||
|
||||
return rc, err
|
||||
|
||||
}
|
||||
return plugin.NextOrFailure(l.Name(), l.Next, ctx, w, r)
|
||||
}
|
||||
|
||||
// Name implements the Handler interface.
|
||||
func (l Logger) Name() string { return "log" }
|
||||
|
||||
// Rule configures the logging plugin.
|
||||
type Rule struct {
|
||||
NameScope string
|
||||
Class response.Class
|
||||
OutputFile string
|
||||
Format string
|
||||
Log *log.Logger
|
||||
}
|
||||
|
||||
const (
|
||||
// DefaultLogFilename is the default output name. This is the only supported value.
|
||||
DefaultLogFilename = "stdout"
|
||||
// CommonLogFormat is the common log format.
|
||||
CommonLogFormat = `{remote} ` + CommonLogEmptyValue + ` [{when}] "{type} {class} {name} {proto} {size} {>do} {>bufsize}" {rcode} {>rflags} {rsize} {duration}`
|
||||
// CommonLogEmptyValue is the common empty log value.
|
||||
CommonLogEmptyValue = "-"
|
||||
// CombinedLogFormat is the combined log format.
|
||||
CombinedLogFormat = CommonLogFormat + ` "{>opcode}"`
|
||||
// DefaultLogFormat is the default log format.
|
||||
DefaultLogFormat = CommonLogFormat
|
||||
)
|
||||
Reference in New Issue
Block a user