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

@ -72,7 +72,6 @@ func (l Logger) Name() string { return "log" }
type Rule struct {
NameScope string
Class response.Class
OutputFile string
Format string
Log *log.Logger
}

View file

@ -29,13 +29,7 @@ func setup(c *caddy.Controller) error {
// Open the log files for writing when the server starts
c.OnStartup(func() error {
for i := 0; i < len(rules); i++ {
// We only support stdout
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)
rules[i].Log = log.New(os.Stdout, "", 0)
}
return nil
@ -58,18 +52,19 @@ func logParse(c *caddy.Controller) ([]Rule, error) {
// Nothing specified; use defaults
rules = append(rules, Rule{
NameScope: ".",
OutputFile: DefaultLogFilename,
Format: DefaultLogFormat,
})
} 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{
NameScope: ".",
OutputFile: args[0],
Format: DefaultLogFormat,
})
} else {
// Name scope, output file, and maybe a format specified
// Name scope, output file (stdout), and maybe a format specified
format := DefaultLogFormat
@ -84,9 +79,12 @@ 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{
NameScope: dns.Fqdn(args[0]),
OutputFile: args[1],
Format: format,
})
}

View file

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