diff --git a/plugin/log/README.md b/plugin/log/README.md index 48e5acb6c..f2d89741c 100644 --- a/plugin/log/README.md +++ b/plugin/log/README.md @@ -61,6 +61,7 @@ The following place holders are supported: * `{class}`: qclass of the request * `{proto}`: protocol used (tcp or udp) * `{remote}`: client's IP address, for IPv6 addresses these are enclosed in brackets: `[::1]` +* `{local}`: server's IP address, for IPv6 addresses these are enclosed in brackets: `[::1]` * `{size}`: request size in bytes * `{port}`: client's port * `{duration}`: response duration @@ -72,6 +73,10 @@ The following place holders are supported: * `{>do}`: is the EDNS0 DO (DNSSEC OK) bit set in the query * `{>id}`: query ID * `{>opcode}`: query OPCODE +* `{/[LABEL]}`: any metadata label is accepted as a place holder if it is enclosed between `{/` and `}`. +the place holder will be replaced by the corresponding metadata value or the default value `-` if label is not defined. + + The default Common Log Format is: diff --git a/plugin/log/log.go b/plugin/log/log.go index 685c55191..98f00d762 100644 --- a/plugin/log/log.go +++ b/plugin/log/log.go @@ -57,7 +57,7 @@ func (l Logger) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) // If we don't set up a class in config, the default "all" will be added // and we shouldn't have an empty rule.Class. if rule.Class[response.All] || rule.Class[class] { - rep := replacer.New(r, rrw, CommonLogEmptyValue) + rep := replacer.New(ctx, r, rrw, CommonLogEmptyValue) clog.Infof(rep.Replace(rule.Format)) } diff --git a/plugin/pkg/replacer/replacer.go b/plugin/pkg/replacer/replacer.go index 695e9e392..8c8032c76 100644 --- a/plugin/pkg/replacer/replacer.go +++ b/plugin/pkg/replacer/replacer.go @@ -1,10 +1,12 @@ package replacer import ( + "context" "strconv" "strings" "time" + "github.com/coredns/coredns/plugin/metadata" "github.com/coredns/coredns/plugin/pkg/dnstest" "github.com/coredns/coredns/request" @@ -21,6 +23,7 @@ type Replacer interface { } type replacer struct { + ctx context.Context replacements map[string]string emptyValue string } @@ -31,9 +34,10 @@ type replacer struct { // values into the replacer. rr may be nil if it is not // available. emptyValue should be the string that is used // in place of empty string (can still be empty string). -func New(r *dns.Msg, rr *dnstest.Recorder, emptyValue string) Replacer { +func New(ctx context.Context, r *dns.Msg, rr *dnstest.Recorder, emptyValue string) Replacer { req := request.Request{W: rr, Req: r} rep := replacer{ + ctx: ctx, replacements: map[string]string{ "{type}": req.Type(), "{name}": req.Name(), @@ -43,6 +47,7 @@ func New(r *dns.Msg, rr *dnstest.Recorder, emptyValue string) Replacer { "{size}": strconv.Itoa(req.Len()), "{remote}": addrToRFC3986(req.IP()), "{port}": req.Port(), + "{local}": addrToRFC3986(req.LocalIP()), }, emptyValue: emptyValue, } @@ -71,23 +76,36 @@ func New(r *dns.Msg, rr *dnstest.Recorder, emptyValue string) Replacer { // Replace performs a replacement of values on s and returns // the string with the replaced values. func (r replacer) Replace(s string) string { - // Header replacements - these are case-insensitive, so we can't just use strings.Replace() - for strings.Contains(s, headerReplacer) { - idxStart := strings.Index(s, headerReplacer) - endOffset := idxStart + len(headerReplacer) - idxEnd := strings.Index(s[endOffset:], "}") - if idxEnd > -1 { - placeholder := strings.ToLower(s[idxStart : endOffset+idxEnd+1]) - replacement := r.replacements[placeholder] - if replacement == "" { - replacement = r.emptyValue + + // declare a function that replace based on header matching + fscanAndReplace := func(s string, header string, replace func(string) string) string { + b := strings.Builder{} + for strings.Contains(s, header) { + idxStart := strings.Index(s, header) + endOffset := idxStart + len(header) + idxEnd := strings.Index(s[endOffset:], "}") + if idxEnd > -1 { + placeholder := strings.ToLower(s[idxStart : endOffset+idxEnd+1]) + replacement := replace(placeholder) + if replacement == "" { + replacement = r.emptyValue + } + b.WriteString(s[:idxStart]) + b.WriteString(replacement) + s = s[endOffset+idxEnd+1:] + } else { + break } - s = s[:idxStart] + replacement + s[endOffset+idxEnd+1:] - } else { - break } + b.WriteString(s) + return b.String() } + // Header replacements - these are case-insensitive, so we can't just use strings.Replace() + s = fscanAndReplace(s, headerReplacer, func(placeholder string) string { + return r.replacements[placeholder] + }) + // Regular replacements - these are easier because they're case-sensitive for placeholder, replacement := range r.replacements { if replacement == "" { @@ -96,6 +114,16 @@ func (r replacer) Replace(s string) string { s = strings.Replace(s, placeholder, replacement, -1) } + // Metadata label replacements + s = fscanAndReplace(s, headerLabelReplacer, func(placeholder string) string { + // label place holder has the format {/