| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | // Package log implements basic but useful request (access) logging middleware.
 | 
					
						
							|  |  |  | package log
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import (
 | 
					
						
							|  |  |  | 	"log"
 | 
					
						
							| 
									
										
										
										
											2016-04-09 16:17:53 +01:00
										 |  |  | 	"time"
 | 
					
						
							| 
									
										
										
										
											2016-03-19 07:18:57 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-21 22:51:47 -08:00
										 |  |  | 	"github.com/coredns/coredns/middleware"
 | 
					
						
							|  |  |  | 	"github.com/coredns/coredns/middleware/metrics/vars"
 | 
					
						
							|  |  |  | 	"github.com/coredns/coredns/middleware/pkg/dnsrecorder"
 | 
					
						
							|  |  |  | 	"github.com/coredns/coredns/middleware/pkg/rcode"
 | 
					
						
							|  |  |  | 	"github.com/coredns/coredns/middleware/pkg/replacer"
 | 
					
						
							|  |  |  | 	"github.com/coredns/coredns/middleware/pkg/response"
 | 
					
						
							|  |  |  | 	"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"
 | 
					
						
							| 
									
										
										
										
											2016-04-09 16:17:53 +01:00
										 |  |  | 	"golang.org/x/net/context"
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Logger is a basic request logging middleware.
 | 
					
						
							|  |  |  | type Logger struct {
 | 
					
						
							|  |  |  | 	Next      middleware.Handler
 | 
					
						
							|  |  |  | 	Rules     []Rule
 | 
					
						
							|  |  |  | 	ErrorFunc func(dns.ResponseWriter, *dns.Msg, int) // failover error handler
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-23 09:14:12 +01:00
										 |  |  | // ServeDNS implements the middleware.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 {
 | 
					
						
							| 
									
										
										
										
											2016-10-10 12:09:29 +01:00
										 |  |  | 		if !middleware.Name(rule.NameScope).Matches(state.Name()) {
 | 
					
						
							|  |  |  | 			continue
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-20 18:58:05 +00:00
										 |  |  | 		rrw := dnsrecorder.New(w)
 | 
					
						
							|  |  |  | 		rc, err := middleware.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 {
 | 
					
						
							| 
									
										
										
										
											2016-12-20 18:58:05 +00:00
										 |  |  | 				l.ErrorFunc(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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-26 10:01:52 +01:00
										 |  |  | 				vars.Report(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
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-20 18:58:05 +00:00
										 |  |  | 		class, _ := response.Classify(rrw.Msg)
 | 
					
						
							| 
									
										
										
										
											2016-10-10 12:09:29 +01:00
										 |  |  | 		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
										 |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2016-12-20 18:58:05 +00:00
										 |  |  | 	return middleware.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" }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | // Rule configures the logging middleware.
 | 
					
						
							|  |  |  | type Rule struct {
 | 
					
						
							| 
									
										
										
										
											2016-03-19 11:16:08 +00:00
										 |  |  | 	NameScope  string
 | 
					
						
							| 
									
										
										
										
											2016-10-10 12:09:29 +01:00
										 |  |  | 	Class      response.Class
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 	OutputFile string
 | 
					
						
							|  |  |  | 	Format     string
 | 
					
						
							|  |  |  | 	Log        *log.Logger
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const (
 | 
					
						
							|  |  |  | 	// DefaultLogFilename is the default log filename.
 | 
					
						
							| 
									
										
										
										
											2016-03-19 11:16:08 +00:00
										 |  |  | 	DefaultLogFilename = "query.log"
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 	// CommonLogFormat is the common log format.
 | 
					
						
							| 
									
										
										
										
											2016-11-30 20:44:00 +00:00
										 |  |  | 	CommonLogFormat = `{remote} ` + CommonLogEmptyValue + ` [{when}] "{type} {class} {name} {proto} {size} {>do} {>bufsize}" {rcode} {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
 | 
					
						
							|  |  |  | )
 |