Files
coredns/plugin/log/setup.go

115 lines
2.3 KiB
Go
Raw Normal View History

package log
2016-03-18 20:57:35 +00:00
import (
"fmt"
2016-03-18 20:57:35 +00:00
"log"
"os"
"github.com/coredns/coredns/core/dnsserver"
"github.com/coredns/coredns/plugin"
"github.com/coredns/coredns/plugin/pkg/response"
"github.com/mholt/caddy"
2016-03-19 11:16:08 +00:00
"github.com/miekg/dns"
2016-03-18 20:57:35 +00:00
)
func init() {
caddy.RegisterPlugin("log", caddy.Plugin{
ServerType: "dns",
Action: setup,
})
}
func setup(c *caddy.Controller) error {
2016-03-18 20:57:35 +00:00
rules, err := logParse(c)
if err != nil {
return plugin.Error("log", err)
2016-03-18 20:57:35 +00:00
}
// Open the log files for writing when the server starts
c.OnStartup(func() error {
2016-03-18 20:57:35 +00:00
for i := 0; i < len(rules); i++ {
rules[i].Log = log.New(os.Stdout, "", 0)
2016-03-18 20:57:35 +00:00
}
return nil
})
dnsserver.GetConfig(c).AddPlugin(func(next plugin.Handler) plugin.Handler {
return Logger{Next: next, Rules: rules, ErrorFunc: dnsserver.DefaultErrorFunc}
})
return nil
2016-03-18 20:57:35 +00:00
}
func logParse(c *caddy.Controller) ([]Rule, error) {
var rules []Rule
2016-03-18 20:57:35 +00:00
for c.Next() {
args := c.RemainingArgs()
if len(args) == 0 {
// Nothing specified; use defaults
rules = append(rules, Rule{
NameScope: ".",
Format: DefaultLogFormat,
2016-03-18 20:57:35 +00:00
})
} else if len(args) == 1 {
// Only an output file specified, can only be *stdout*
if args[0] != "stdout" {
return nil, fmt.Errorf("only stdout is allowed: %s", args[0])
}
rules = append(rules, Rule{
NameScope: ".",
Format: DefaultLogFormat,
2016-03-18 20:57:35 +00:00
})
} else {
// Name scope, output file (stdout), and maybe a format specified
2016-03-18 20:57:35 +00:00
format := DefaultLogFormat
2016-03-18 20:57:35 +00:00
if len(args) > 2 {
switch args[2] {
case "{common}":
format = CommonLogFormat
2016-03-18 20:57:35 +00:00
case "{combined}":
format = CombinedLogFormat
2016-03-18 20:57:35 +00:00
default:
format = args[2]
}
}
if args[1] != "stdout" {
return nil, fmt.Errorf("only stdout is allowed: %s", args[1])
}
rules = append(rules, Rule{
NameScope: dns.Fqdn(args[0]),
Format: format,
2016-03-18 20:57:35 +00:00
})
}
// Class refinements in an extra block.
for c.NextBlock() {
switch c.Val() {
// class followed by all, denial, error or success.
case "class":
classes := c.RemainingArgs()
if len(classes) == 0 {
return nil, c.ArgErr()
}
cls, err := response.ClassFromString(classes[0])
if err != nil {
return nil, err
}
// update class and the last added Rule (bit icky)
rules[len(rules)-1].Class = cls
default:
return nil, c.ArgErr()
}
}
2016-03-18 20:57:35 +00:00
}
return rules, nil
}