Use logging (#1718)

* update docs

* plugins: use plugin specific logging

Hooking up pkg/log also changed NewWithPlugin to just take a string
instead of a plugin.Handler as that is more flexible and for instance
the Root "plugin" doesn't implement it fully.

Same logging from the reload plugin:

.:1043
2018/04/22 08:56:37 [INFO] CoreDNS-1.1.1
2018/04/22 08:56:37 [INFO] linux/amd64, go1.10.1,
CoreDNS-1.1.1
linux/amd64, go1.10.1,
2018/04/22 08:56:37 [INFO] plugin/reload: Running configuration MD5 = ec4c9c55cd19759ea1c46b8c45742b06
2018/04/22 08:56:54 [INFO] Reloading
2018/04/22 08:56:54 [INFO] plugin/reload: Running configuration MD5 = 9e2bfdd85bdc9cceb740ba9c80f34c1a
2018/04/22 08:56:54 [INFO] Reloading complete

* update docs

* better doc
This commit is contained in:
Miek Gieben 2018-04-22 21:40:33 +01:00 committed by GitHub
parent 0930eb8beb
commit 12b2ff9740
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
36 changed files with 61 additions and 45 deletions

View file

@ -38,7 +38,15 @@ See a couple of blog posts on how to write and add plugin to CoreDNS:
If your plugin needs to output a log line you should use the `plugin/pkg/log` package. This package If your plugin needs to output a log line you should use the `plugin/pkg/log` package. This package
implements log levels. The standard way of outputting is: `log.Info` for info level messages. The implements log levels. The standard way of outputting is: `log.Info` for info level messages. The
levels available are `log.Info`, `log.Warning`, `log.Error`, `log.Debug`. Each of these also has levels available are `log.Info`, `log.Warning`, `log.Error`, `log.Debug`. Each of these also has
a `f` variant. a `f` variant. The plugin's name should be included, by using the log package like so:
~~~ go
import clog "github.com/coredns/coredns/plugin/pkg/log"
var log = clog.NewWithPlugin("whoami")
log.Info("message") // outputs: [INFO] plugin/whoami: message
~~~
In general, logging should be left to the higher layers by returning an error. However, if there is In general, logging should be left to the higher layers by returning an error. However, if there is
a reason to consume the error and notify the user, then logging in the plugin itself can be a reason to consume the error and notify the user, then logging in the plugin itself can be

View file

@ -10,13 +10,15 @@ import (
"github.com/coredns/coredns/core/dnsserver" "github.com/coredns/coredns/core/dnsserver"
"github.com/coredns/coredns/plugin" "github.com/coredns/coredns/plugin"
"github.com/coredns/coredns/plugin/metrics" "github.com/coredns/coredns/plugin/metrics"
"github.com/coredns/coredns/plugin/pkg/log" clog "github.com/coredns/coredns/plugin/pkg/log"
"github.com/coredns/coredns/plugin/pkg/parse" "github.com/coredns/coredns/plugin/pkg/parse"
"github.com/coredns/coredns/plugin/pkg/upstream" "github.com/coredns/coredns/plugin/pkg/upstream"
"github.com/mholt/caddy" "github.com/mholt/caddy"
) )
var log = clog.NewWithPlugin("auto")
func init() { func init() {
caddy.RegisterPlugin("auto", caddy.Plugin{ caddy.RegisterPlugin("auto", caddy.Plugin{
ServerType: "dns", ServerType: "dns",

View file

@ -7,7 +7,6 @@ import (
"regexp" "regexp"
"github.com/coredns/coredns/plugin/file" "github.com/coredns/coredns/plugin/file"
"github.com/coredns/coredns/plugin/pkg/log"
"github.com/miekg/dns" "github.com/miekg/dns"
) )

View file

@ -8,7 +8,6 @@ import (
"github.com/coredns/coredns/plugin" "github.com/coredns/coredns/plugin"
"github.com/coredns/coredns/plugin/pkg/cache" "github.com/coredns/coredns/plugin/pkg/cache"
"github.com/coredns/coredns/plugin/pkg/log"
"github.com/coredns/coredns/plugin/pkg/response" "github.com/coredns/coredns/plugin/pkg/response"
"github.com/coredns/coredns/request" "github.com/coredns/coredns/request"

View file

@ -9,10 +9,13 @@ import (
"github.com/coredns/coredns/plugin" "github.com/coredns/coredns/plugin"
"github.com/coredns/coredns/plugin/metrics" "github.com/coredns/coredns/plugin/metrics"
"github.com/coredns/coredns/plugin/pkg/cache" "github.com/coredns/coredns/plugin/pkg/cache"
clog "github.com/coredns/coredns/plugin/pkg/log"
"github.com/mholt/caddy" "github.com/mholt/caddy"
) )
var log = clog.NewWithPlugin("cache")
func init() { func init() {
caddy.RegisterPlugin("cache", caddy.Plugin{ caddy.RegisterPlugin("cache", caddy.Plugin{
ServerType: "dns", ServerType: "dns",

View file

@ -4,7 +4,6 @@ import (
"time" "time"
"github.com/coredns/coredns/plugin" "github.com/coredns/coredns/plugin"
"github.com/coredns/coredns/plugin/pkg/log"
"github.com/coredns/coredns/request" "github.com/coredns/coredns/request"
"github.com/miekg/dns" "github.com/miekg/dns"

View file

@ -9,10 +9,13 @@ import (
"github.com/coredns/coredns/plugin" "github.com/coredns/coredns/plugin"
"github.com/coredns/coredns/plugin/metrics" "github.com/coredns/coredns/plugin/metrics"
"github.com/coredns/coredns/plugin/pkg/cache" "github.com/coredns/coredns/plugin/pkg/cache"
clog "github.com/coredns/coredns/plugin/pkg/log"
"github.com/mholt/caddy" "github.com/mholt/caddy"
) )
var log = clog.NewWithPlugin("dnssec")
func init() { func init() {
caddy.RegisterPlugin("dnssec", caddy.Plugin{ caddy.RegisterPlugin("dnssec", caddy.Plugin{
ServerType: "dns", ServerType: "dns",

View file

@ -5,12 +5,14 @@ import (
"sync/atomic" "sync/atomic"
"time" "time"
"github.com/coredns/coredns/plugin/pkg/log" clog "github.com/coredns/coredns/plugin/pkg/log"
tap "github.com/dnstap/golang-dnstap" tap "github.com/dnstap/golang-dnstap"
fs "github.com/farsightsec/golang-framestream" fs "github.com/farsightsec/golang-framestream"
) )
var log = clog.NewWithPlugin("dnstap")
const ( const (
tcpWriteBufSize = 1024 * 1024 tcpWriteBufSize = 1024 * 1024
tcpTimeout = 4 * time.Second tcpTimeout = 4 * time.Second

View file

@ -7,11 +7,14 @@ import (
"github.com/coredns/coredns/plugin" "github.com/coredns/coredns/plugin"
"github.com/coredns/coredns/plugin/dnstap/dnstapio" "github.com/coredns/coredns/plugin/dnstap/dnstapio"
"github.com/coredns/coredns/plugin/pkg/dnsutil" "github.com/coredns/coredns/plugin/pkg/dnsutil"
clog "github.com/coredns/coredns/plugin/pkg/log"
"github.com/mholt/caddy" "github.com/mholt/caddy"
"github.com/mholt/caddy/caddyfile" "github.com/mholt/caddy/caddyfile"
) )
var log = clog.NewWithPlugin("dnstap")
func init() { func init() {
caddy.RegisterPlugin("dnstap", caddy.Plugin{ caddy.RegisterPlugin("dnstap", caddy.Plugin{
ServerType: "dns", ServerType: "dns",

View file

@ -6,6 +6,7 @@ import (
"github.com/coredns/coredns/core/dnsserver" "github.com/coredns/coredns/core/dnsserver"
"github.com/coredns/coredns/plugin" "github.com/coredns/coredns/plugin"
clog "github.com/coredns/coredns/plugin/pkg/log"
mwtls "github.com/coredns/coredns/plugin/pkg/tls" mwtls "github.com/coredns/coredns/plugin/pkg/tls"
"github.com/coredns/coredns/plugin/pkg/upstream" "github.com/coredns/coredns/plugin/pkg/upstream"
"github.com/coredns/coredns/plugin/proxy" "github.com/coredns/coredns/plugin/proxy"
@ -14,6 +15,8 @@ import (
"github.com/mholt/caddy" "github.com/mholt/caddy"
) )
var log = clog.NewWithPlugin("etcd")
func init() { func init() {
caddy.RegisterPlugin("etcd", caddy.Plugin{ caddy.RegisterPlugin("etcd", caddy.Plugin{
ServerType: "dns", ServerType: "dns",

View file

@ -7,7 +7,6 @@ import (
"github.com/coredns/coredns/plugin/etcd/msg" "github.com/coredns/coredns/plugin/etcd/msg"
"github.com/coredns/coredns/plugin/pkg/dnsutil" "github.com/coredns/coredns/plugin/pkg/dnsutil"
"github.com/coredns/coredns/plugin/pkg/log"
"github.com/coredns/coredns/plugin/proxy" "github.com/coredns/coredns/plugin/proxy"
"github.com/coredns/coredns/request" "github.com/coredns/coredns/request"

View file

@ -4,7 +4,6 @@ import (
"context" "context"
"errors" "errors"
"github.com/coredns/coredns/plugin/pkg/log"
"github.com/coredns/coredns/request" "github.com/coredns/coredns/request"
"github.com/miekg/dns" "github.com/miekg/dns"

View file

@ -7,12 +7,14 @@ import (
"io" "io"
"github.com/coredns/coredns/plugin" "github.com/coredns/coredns/plugin"
"github.com/coredns/coredns/plugin/pkg/log" clog "github.com/coredns/coredns/plugin/pkg/log"
"github.com/coredns/coredns/request" "github.com/coredns/coredns/request"
"github.com/miekg/dns" "github.com/miekg/dns"
) )
var log = clog.NewWithPlugin("file")
type ( type (
// File is the plugin that reads zone data from disk. // File is the plugin that reads zone data from disk.
File struct { File struct {

View file

@ -4,7 +4,6 @@ import (
"fmt" "fmt"
"net" "net"
"github.com/coredns/coredns/plugin/pkg/log"
"github.com/coredns/coredns/plugin/pkg/rcode" "github.com/coredns/coredns/plugin/pkg/rcode"
"github.com/coredns/coredns/request" "github.com/coredns/coredns/request"

View file

@ -3,8 +3,6 @@ package file
import ( import (
"os" "os"
"time" "time"
"github.com/coredns/coredns/plugin/pkg/log"
) )
// TickTime is the default time we use to reload zone. Exported to be tweaked in tests. // TickTime is the default time we use to reload zone. Exported to be tweaked in tests.

View file

@ -4,8 +4,6 @@ import (
"math/rand" "math/rand"
"time" "time"
"github.com/coredns/coredns/plugin/pkg/log"
"github.com/miekg/dns" "github.com/miekg/dns"
) )

View file

@ -5,7 +5,6 @@ import (
"fmt" "fmt"
"github.com/coredns/coredns/plugin" "github.com/coredns/coredns/plugin"
"github.com/coredns/coredns/plugin/pkg/log"
"github.com/coredns/coredns/request" "github.com/coredns/coredns/request"
"github.com/miekg/dns" "github.com/miekg/dns"

View file

@ -8,9 +8,11 @@ import (
"sync" "sync"
"time" "time"
"github.com/coredns/coredns/plugin/pkg/log" clog "github.com/coredns/coredns/plugin/pkg/log"
) )
var log = clog.NewWithPlugin("health")
// Health implements healthchecks by polling plugins. // Health implements healthchecks by polling plugins.
type health struct { type health struct {
Addr string Addr string

View file

@ -8,11 +8,13 @@ import (
"github.com/coredns/coredns/core/dnsserver" "github.com/coredns/coredns/core/dnsserver"
"github.com/coredns/coredns/plugin" "github.com/coredns/coredns/plugin"
"github.com/coredns/coredns/plugin/pkg/log" clog "github.com/coredns/coredns/plugin/pkg/log"
"github.com/mholt/caddy" "github.com/mholt/caddy"
) )
var log = clog.NewWithPlugin("hosts")
func init() { func init() {
caddy.RegisterPlugin("hosts", caddy.Plugin{ caddy.RegisterPlugin("hosts", caddy.Plugin{
ServerType: "dns", ServerType: "dns",

View file

@ -7,7 +7,6 @@ import (
"net/http" "net/http"
"github.com/coredns/coredns/plugin/pkg/healthcheck" "github.com/coredns/coredns/plugin/pkg/healthcheck"
"github.com/coredns/coredns/plugin/pkg/log"
) )
type proxyHandler struct { type proxyHandler struct {

View file

@ -12,6 +12,7 @@ import (
"github.com/coredns/coredns/core/dnsserver" "github.com/coredns/coredns/core/dnsserver"
"github.com/coredns/coredns/plugin" "github.com/coredns/coredns/plugin"
"github.com/coredns/coredns/plugin/pkg/dnsutil" "github.com/coredns/coredns/plugin/pkg/dnsutil"
clog "github.com/coredns/coredns/plugin/pkg/log"
"github.com/coredns/coredns/plugin/pkg/parse" "github.com/coredns/coredns/plugin/pkg/parse"
"github.com/coredns/coredns/plugin/pkg/upstream" "github.com/coredns/coredns/plugin/pkg/upstream"
@ -20,6 +21,8 @@ import (
meta "k8s.io/apimachinery/pkg/apis/meta/v1" meta "k8s.io/apimachinery/pkg/apis/meta/v1"
) )
var log = clog.NewWithPlugin("kubernetes")
func init() { func init() {
// Kubernetes plugin uses the kubernetes library, which uses glog (ugh), we must set this *flag*, // Kubernetes plugin uses the kubernetes library, which uses glog (ugh), we must set this *flag*,
// so we don't log to the filesystem, which can fill up and crash CoreDNS indirectly by calling os.Exit(). // so we don't log to the filesystem, which can fill up and crash CoreDNS indirectly by calling os.Exit().

View file

@ -8,7 +8,6 @@ import (
"github.com/coredns/coredns/plugin" "github.com/coredns/coredns/plugin"
"github.com/coredns/coredns/plugin/etcd/msg" "github.com/coredns/coredns/plugin/etcd/msg"
"github.com/coredns/coredns/plugin/pkg/log"
"github.com/coredns/coredns/request" "github.com/coredns/coredns/request"
"github.com/miekg/dns" "github.com/miekg/dns"

View file

@ -2,8 +2,6 @@
package loadbalance package loadbalance
import ( import (
"github.com/coredns/coredns/plugin/pkg/log"
"github.com/miekg/dns" "github.com/miekg/dns"
) )

View file

@ -3,9 +3,13 @@ package loadbalance
import ( import (
"github.com/coredns/coredns/core/dnsserver" "github.com/coredns/coredns/core/dnsserver"
"github.com/coredns/coredns/plugin" "github.com/coredns/coredns/plugin"
clog "github.com/coredns/coredns/plugin/pkg/log"
"github.com/mholt/caddy" "github.com/mholt/caddy"
) )
var log = clog.NewWithPlugin("loadbalance")
func init() { func init() {
caddy.RegisterPlugin("loadbalance", caddy.Plugin{ caddy.RegisterPlugin("loadbalance", caddy.Plugin{
ServerType: "dns", ServerType: "dns",

View file

@ -9,7 +9,6 @@ import (
"github.com/coredns/coredns/plugin" "github.com/coredns/coredns/plugin"
"github.com/coredns/coredns/plugin/metrics/vars" "github.com/coredns/coredns/plugin/metrics/vars"
"github.com/coredns/coredns/plugin/pkg/log"
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp" "github.com/prometheus/client_golang/prometheus/promhttp"

View file

@ -7,10 +7,13 @@ import (
"github.com/coredns/coredns/core/dnsserver" "github.com/coredns/coredns/core/dnsserver"
"github.com/coredns/coredns/coremain" "github.com/coredns/coredns/coremain"
"github.com/coredns/coredns/plugin" "github.com/coredns/coredns/plugin"
clog "github.com/coredns/coredns/plugin/pkg/log"
"github.com/mholt/caddy" "github.com/mholt/caddy"
) )
var log = clog.NewWithPlugin("prometheus")
func init() { func init() {
caddy.RegisterPlugin("prometheus", caddy.Plugin{ caddy.RegisterPlugin("prometheus", caddy.Plugin{
ServerType: "dns", ServerType: "dns",

View file

@ -3,8 +3,6 @@ package log
import ( import (
"fmt" "fmt"
golog "log" golog "log"
"github.com/coredns/coredns/plugin"
) )
// P is a logger that includes the plugin doing the logging. // P is a logger that includes the plugin doing the logging.
@ -12,9 +10,9 @@ type P struct {
plugin string plugin string
} }
// NewWithPlugin return a logger that shows the plugin that logs the message. // NewWithPlugin returns a logger that includes "plugin/name: " in the log message.
// I.e [INFO] plugin/<name>: message. // I.e [INFO] plugin/<name>: message.
func NewWithPlugin(h plugin.Handler) P { return P{h.Name()} } func NewWithPlugin(name string) P { return P{name} }
func (p P) logf(level, format string, v ...interface{}) { func (p P) logf(level, format string, v ...interface{}) {
s := level + pFormat(p.plugin) + fmt.Sprintf(format, v...) s := level + pFormat(p.plugin) + fmt.Sprintf(format, v...)

View file

@ -2,28 +2,17 @@ package log
import ( import (
"bytes" "bytes"
"context"
golog "log" golog "log"
"strings" "strings"
"testing" "testing"
"github.com/miekg/dns"
) )
type p struct{}
func (p p) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
return 0, nil
}
func (p p) Name() string { return "testplugin" }
func TestPlugins(t *testing.T) { func TestPlugins(t *testing.T) {
var f bytes.Buffer var f bytes.Buffer
const ts = "test" const ts = "test"
golog.SetOutput(&f) golog.SetOutput(&f)
lg := NewWithPlugin(p{}) lg := NewWithPlugin("testplugin")
lg.Info(ts) lg.Info(ts)
if x := f.String(); !strings.Contains(x, "plugin/testplugin") { if x := f.String(); !strings.Contains(x, "plugin/testplugin") {

View file

@ -6,8 +6,6 @@ import (
"net" "net"
"net/http" "net/http"
pp "net/http/pprof" pp "net/http/pprof"
"github.com/coredns/coredns/plugin/pkg/log"
) )
type handler struct { type handler struct {

View file

@ -5,10 +5,13 @@ import (
"sync" "sync"
"github.com/coredns/coredns/plugin" "github.com/coredns/coredns/plugin"
clog "github.com/coredns/coredns/plugin/pkg/log"
"github.com/mholt/caddy" "github.com/mholt/caddy"
) )
var log = clog.NewWithPlugin("pprof")
const defaultAddr = "localhost:6053" const defaultAddr = "localhost:6053"
func init() { func init() {

View file

@ -12,7 +12,6 @@ import (
"time" "time"
"github.com/coredns/coredns/plugin/pkg/healthcheck" "github.com/coredns/coredns/plugin/pkg/healthcheck"
"github.com/coredns/coredns/plugin/pkg/log"
"github.com/coredns/coredns/request" "github.com/coredns/coredns/request"
"github.com/miekg/dns" "github.com/miekg/dns"

View file

@ -6,7 +6,6 @@ import (
"fmt" "fmt"
"github.com/coredns/coredns/pb" "github.com/coredns/coredns/pb"
"github.com/coredns/coredns/plugin/pkg/log"
"github.com/coredns/coredns/plugin/pkg/trace" "github.com/coredns/coredns/plugin/pkg/trace"
"github.com/coredns/coredns/request" "github.com/coredns/coredns/request"

View file

@ -4,10 +4,13 @@ import (
"github.com/coredns/coredns/core/dnsserver" "github.com/coredns/coredns/core/dnsserver"
"github.com/coredns/coredns/plugin" "github.com/coredns/coredns/plugin"
"github.com/coredns/coredns/plugin/metrics" "github.com/coredns/coredns/plugin/metrics"
clog "github.com/coredns/coredns/plugin/pkg/log"
"github.com/mholt/caddy" "github.com/mholt/caddy"
) )
var log = clog.NewWithPlugin("proxy")
func init() { func init() {
caddy.RegisterPlugin("proxy", caddy.Plugin{ caddy.RegisterPlugin("proxy", caddy.Plugin{
ServerType: "dns", ServerType: "dns",

View file

@ -4,8 +4,6 @@ import (
"crypto/md5" "crypto/md5"
"time" "time"
"github.com/coredns/coredns/plugin/pkg/log"
"github.com/mholt/caddy" "github.com/mholt/caddy"
) )

View file

@ -7,10 +7,13 @@ import (
"time" "time"
"github.com/coredns/coredns/plugin" "github.com/coredns/coredns/plugin"
clog "github.com/coredns/coredns/plugin/pkg/log"
"github.com/mholt/caddy" "github.com/mholt/caddy"
) )
var log = clog.NewWithPlugin("reload")
func init() { func init() {
caddy.RegisterPlugin("reload", caddy.Plugin{ caddy.RegisterPlugin("reload", caddy.Plugin{
ServerType: "dns", ServerType: "dns",

View file

@ -5,11 +5,13 @@ import (
"github.com/coredns/coredns/core/dnsserver" "github.com/coredns/coredns/core/dnsserver"
"github.com/coredns/coredns/plugin" "github.com/coredns/coredns/plugin"
"github.com/coredns/coredns/plugin/pkg/log" clog "github.com/coredns/coredns/plugin/pkg/log"
"github.com/mholt/caddy" "github.com/mholt/caddy"
) )
var log = clog.NewWithPlugin("root")
func init() { func init() {
caddy.RegisterPlugin("root", caddy.Plugin{ caddy.RegisterPlugin("root", caddy.Plugin{
ServerType: "dns", ServerType: "dns",