plugin/log: remove OutputFile (#1217)

* plugin/log: remove OutputFile

We use stdout for everything, remove OutputFile as it isn't used and
causes confusion.

Fixes #1216

* PR feedback
This commit is contained in:
Miek Gieben 2017-11-10 15:17:12 +00:00 committed by GitHub
parent 0186aadfcf
commit beef212fbe
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 57 additions and 80 deletions

View file

@ -70,11 +70,10 @@ func (l Logger) Name() string { return "log" }
// Rule configures the logging plugin. // Rule configures the logging plugin.
type Rule struct { type Rule struct {
NameScope string NameScope string
Class response.Class Class response.Class
OutputFile string Format string
Format string Log *log.Logger
Log *log.Logger
} }
const ( const (

View file

@ -29,13 +29,7 @@ func setup(c *caddy.Controller) error {
// Open the log files for writing when the server starts // Open the log files for writing when the server starts
c.OnStartup(func() error { c.OnStartup(func() error {
for i := 0; i < len(rules); i++ { for i := 0; i < len(rules); i++ {
// We only support stdout rules[i].Log = log.New(os.Stdout, "", 0)
writer := os.Stdout
if rules[i].OutputFile != "stdout" {
return plugin.Error("log", fmt.Errorf("invalid log file: %s", rules[i].OutputFile))
}
rules[i].Log = log.New(writer, "", 0)
} }
return nil return nil
@ -57,19 +51,20 @@ func logParse(c *caddy.Controller) ([]Rule, error) {
if len(args) == 0 { if len(args) == 0 {
// Nothing specified; use defaults // Nothing specified; use defaults
rules = append(rules, Rule{ rules = append(rules, Rule{
NameScope: ".", NameScope: ".",
OutputFile: DefaultLogFilename, Format: DefaultLogFormat,
Format: DefaultLogFormat,
}) })
} else if len(args) == 1 { } else if len(args) == 1 {
// Only an output file specified. // 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{ rules = append(rules, Rule{
NameScope: ".", NameScope: ".",
OutputFile: args[0], Format: DefaultLogFormat,
Format: DefaultLogFormat,
}) })
} else { } else {
// Name scope, output file, and maybe a format specified // Name scope, output file (stdout), and maybe a format specified
format := DefaultLogFormat format := DefaultLogFormat
@ -84,10 +79,13 @@ func logParse(c *caddy.Controller) ([]Rule, error) {
} }
} }
if args[1] != "stdout" {
return nil, fmt.Errorf("only stdout is allowed: %s", args[1])
}
rules = append(rules, Rule{ rules = append(rules, Rule{
NameScope: dns.Fqdn(args[0]), NameScope: dns.Fqdn(args[0]),
OutputFile: args[1], Format: format,
Format: format,
}) })
} }

View file

@ -15,80 +15,65 @@ func TestLogParse(t *testing.T) {
expectedLogRules []Rule expectedLogRules []Rule
}{ }{
{`log`, false, []Rule{{ {`log`, false, []Rule{{
NameScope: ".", NameScope: ".",
OutputFile: DefaultLogFilename, Format: DefaultLogFormat,
Format: DefaultLogFormat,
}}}, }}},
{`log log.txt`, false, []Rule{{ {`log example.org stdout`, false, []Rule{{
NameScope: ".", NameScope: "example.org.",
OutputFile: "log.txt", Format: DefaultLogFormat,
Format: DefaultLogFormat,
}}},
{`log example.org log.txt`, false, []Rule{{
NameScope: "example.org.",
OutputFile: "log.txt",
Format: DefaultLogFormat,
}}}, }}},
{`log example.org. stdout`, false, []Rule{{ {`log example.org. stdout`, false, []Rule{{
NameScope: "example.org.", NameScope: "example.org.",
OutputFile: "stdout", Format: DefaultLogFormat,
Format: DefaultLogFormat,
}}}, }}},
{`log example.org log.txt {common}`, false, []Rule{{ {`log example.org stdout {common}`, false, []Rule{{
NameScope: "example.org.", NameScope: "example.org.",
OutputFile: "log.txt", Format: CommonLogFormat,
Format: CommonLogFormat,
}}}, }}},
{`log example.org accesslog.txt {combined}`, false, []Rule{{ {`log example.org stdout {combined}`, false, []Rule{{
NameScope: "example.org.", NameScope: "example.org.",
OutputFile: "accesslog.txt", Format: CombinedLogFormat,
Format: CombinedLogFormat,
}}}, }}},
{`log example.org. log.txt {`log example.org. stdout
log example.net accesslog.txt {combined}`, false, []Rule{{ log example.net stdout {combined}`, false, []Rule{{
NameScope: "example.org.", NameScope: "example.org.",
OutputFile: "log.txt", Format: DefaultLogFormat,
Format: DefaultLogFormat,
}, { }, {
NameScope: "example.net.", NameScope: "example.net.",
OutputFile: "accesslog.txt", Format: CombinedLogFormat,
Format: CombinedLogFormat,
}}}, }}},
{`log example.org stdout {host} {`log example.org stdout {host}
log example.org log.txt {when}`, false, []Rule{{ log example.org stdout {when}`, false, []Rule{{
NameScope: "example.org.", NameScope: "example.org.",
OutputFile: "stdout", Format: "{host}",
Format: "{host}",
}, { }, {
NameScope: "example.org.", NameScope: "example.org.",
OutputFile: "log.txt", Format: "{when}",
Format: "{when}",
}}}, }}},
{`log example.org log.txt { {`log example.org stdout {
class all class all
}`, false, []Rule{{ }`, false, []Rule{{
NameScope: "example.org.", NameScope: "example.org.",
OutputFile: "log.txt", Format: CommonLogFormat,
Format: CommonLogFormat, Class: response.All,
Class: response.All,
}}}, }}},
{`log example.org log.txt { {`log example.org stdout {
class denial class denial
}`, false, []Rule{{ }`, false, []Rule{{
NameScope: "example.org.", NameScope: "example.org.",
OutputFile: "log.txt", Format: CommonLogFormat,
Format: CommonLogFormat, Class: response.Denial,
Class: response.Denial,
}}}, }}},
{`log { {`log {
class denial class denial
}`, false, []Rule{{ }`, false, []Rule{{
NameScope: ".", NameScope: ".",
OutputFile: DefaultLogFilename, Format: CommonLogFormat,
Format: CommonLogFormat, Class: response.Denial,
Class: response.Denial,
}}}, }}},
{`log log.txt`, true, nil},
} }
for i, test := range tests { for i, test := range tests {
c := caddy.NewTestController("dns", test.inputLogRules) c := caddy.NewTestController("dns", test.inputLogRules)
@ -110,11 +95,6 @@ func TestLogParse(t *testing.T) {
i, j, test.expectedLogRules[j].NameScope, actualLogRule.NameScope) i, j, test.expectedLogRules[j].NameScope, actualLogRule.NameScope)
} }
if actualLogRule.OutputFile != test.expectedLogRules[j].OutputFile {
t.Errorf("Test %d expected %dth LogRule OutputFile to be %s , but got %s",
i, j, test.expectedLogRules[j].OutputFile, actualLogRule.OutputFile)
}
if actualLogRule.Format != test.expectedLogRules[j].Format { if actualLogRule.Format != test.expectedLogRules[j].Format {
t.Errorf("Test %d expected %dth LogRule Format to be %s , but got %s", t.Errorf("Test %d expected %dth LogRule Format to be %s , but got %s",
i, j, test.expectedLogRules[j].Format, actualLogRule.Format) i, j, test.expectedLogRules[j].Format, actualLogRule.Format)