| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | // Package log implements basic but useful request (access) logging plugin.
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | package log
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import (
 | 
					
						
							| 
									
										
										
										
											2018-04-22 08:34:35 +01:00
										 |  |  | 	"context"
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 	"log"
 | 
					
						
							| 
									
										
										
										
											2016-04-09 16:17:53 +01:00
										 |  |  | 	"time"
 | 
					
						
							| 
									
										
										
										
											2016-03-19 07:18:57 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 	"github.com/coredns/coredns/plugin"
 | 
					
						
							|  |  |  | 	"github.com/coredns/coredns/plugin/metrics/vars"
 | 
					
						
							| 
									
										
										
										
											2017-09-21 15:15:47 +01:00
										 |  |  | 	"github.com/coredns/coredns/plugin/pkg/dnstest"
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 	"github.com/coredns/coredns/plugin/pkg/rcode"
 | 
					
						
							|  |  |  | 	"github.com/coredns/coredns/plugin/pkg/replacer"
 | 
					
						
							|  |  |  | 	"github.com/coredns/coredns/plugin/pkg/response"
 | 
					
						
							| 
									
										
										
										
											2017-02-21 22:51:47 -08:00
										 |  |  | 	"github.com/coredns/coredns/request"
 | 
					
						
							| 
									
										
										
										
											2016-04-09 16:17:53 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 	"github.com/miekg/dns"
 | 
					
						
							|  |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | // Logger is a basic request logging plugin.
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | type Logger struct {
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 	Next      plugin.Handler
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 	Rules     []Rule
 | 
					
						
							| 
									
										
										
										
											2018-04-18 09:42:20 +01:00
										 |  |  | 	ErrorFunc func(context.Context, dns.ResponseWriter, *dns.Msg, int) // failover error handler
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | // ServeDNS implements the plugin.Handler interface.
 | 
					
						
							| 
									
										
										
										
											2016-03-19 07:18:57 +00:00
										 |  |  | func (l Logger) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
 | 
					
						
							| 
									
										
										
										
											2016-09-07 11:10:16 +01:00
										 |  |  | 	state := request.Request{W: w, Req: r}
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 	for _, rule := range l.Rules {
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 		if !plugin.Name(rule.NameScope).Matches(state.Name()) {
 | 
					
						
							| 
									
										
										
										
											2016-10-10 12:09:29 +01:00
										 |  |  | 			continue
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-21 15:15:47 +01:00
										 |  |  | 		rrw := dnstest.NewRecorder(w)
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 		rc, err := plugin.NextOrFailure(l.Name(), l.Next, ctx, rrw, r)
 | 
					
						
							| 
									
										
										
										
											2016-04-04 15:45:17 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-10 12:09:29 +01:00
										 |  |  | 		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 {
 | 
					
						
							| 
									
										
										
										
											2018-04-18 09:42:20 +01:00
										 |  |  | 				l.ErrorFunc(ctx, rrw, r, rc)
 | 
					
						
							| 
									
										
										
										
											2016-10-10 12:09:29 +01:00
										 |  |  | 			} else {
 | 
					
						
							|  |  |  | 				answer := new(dns.Msg)
 | 
					
						
							|  |  |  | 				answer.SetRcode(r, rc)
 | 
					
						
							|  |  |  | 				state.SizeAndDo(answer)
 | 
					
						
							| 
									
										
										
										
											2016-04-09 16:17:53 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-18 09:42:20 +01:00
										 |  |  | 				vars.Report(ctx, state, vars.Dropped, rcode.ToString(rc), answer.Len(), time.Now())
 | 
					
						
							| 
									
										
										
										
											2016-10-10 12:09:29 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 				w.WriteMsg(answer)
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 			}
 | 
					
						
							| 
									
										
										
										
											2016-10-10 12:09:29 +01:00
										 |  |  | 			rc = 0
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-29 15:06:42 +01:00
										 |  |  | 		tpe, _ := response.Typify(rrw.Msg, time.Now().UTC())
 | 
					
						
							|  |  |  | 		class := response.Classify(tpe)
 | 
					
						
							| 
									
										
										
										
											2018-04-11 09:50:16 +03:00
										 |  |  | 		// 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] {
 | 
					
						
							| 
									
										
										
										
											2016-12-20 18:58:05 +00:00
										 |  |  | 			rep := replacer.New(r, rrw, CommonLogEmptyValue)
 | 
					
						
							| 
									
										
										
										
											2016-03-19 11:16:08 +00:00
										 |  |  | 			rule.Log.Println(rep.Replace(rule.Format))
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2016-10-10 12:09:29 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		return rc, err
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 	return plugin.NextOrFailure(l.Name(), l.Next, ctx, w, r)
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-27 11:48:37 +00:00
										 |  |  | // Name implements the Handler interface.
 | 
					
						
							| 
									
										
										
										
											2016-10-26 10:01:52 +01:00
										 |  |  | func (l Logger) Name() string { return "log" }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | // Rule configures the logging plugin.
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | type Rule struct {
 | 
					
						
							| 
									
										
										
										
											2017-11-10 15:17:12 +00:00
										 |  |  | 	NameScope string
 | 
					
						
							| 
									
										
										
										
											2018-04-11 09:50:16 +03:00
										 |  |  | 	Class     map[response.Class]bool
 | 
					
						
							| 
									
										
										
										
											2017-11-10 15:17:12 +00:00
										 |  |  | 	Format    string
 | 
					
						
							|  |  |  | 	Log       *log.Logger
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const (
 | 
					
						
							|  |  |  | 	// CommonLogFormat is the common log format.
 | 
					
						
							| 
									
										
										
										
											2018-02-28 18:15:12 -08:00
										 |  |  | 	CommonLogFormat = `{remote}:{port} ` + CommonLogEmptyValue + ` [{when}] {>id} "{type} {class} {name} {proto} {size} {>do} {>bufsize}" {rcode} {>rflags} {rsize} {duration}`
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 	// CommonLogEmptyValue is the common empty log value.
 | 
					
						
							|  |  |  | 	CommonLogEmptyValue = "-"
 | 
					
						
							|  |  |  | 	// CombinedLogFormat is the combined log format.
 | 
					
						
							| 
									
										
										
										
											2016-03-19 11:16:08 +00:00
										 |  |  | 	CombinedLogFormat = CommonLogFormat + ` "{>opcode}"`
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 	// DefaultLogFormat is the default log format.
 | 
					
						
							|  |  |  | 	DefaultLogFormat = CommonLogFormat
 | 
					
						
							|  |  |  | )
 |