plugin/log: remove need to specify stdout (#1221)
* plugin/log: remove need to specify stdout Since log will only be output to stdout is doesn't make sense to specify it in Corefile. Fixes: #1218 * fixup! plugin/log: remove need to specify stdout
This commit is contained in:
parent
9b8ee1c119
commit
46a187df8f
4 changed files with 29 additions and 52 deletions
|
@ -10,14 +10,10 @@ log
|
||||||
|
|
||||||
* With no arguments, a query log entry is written to *stdout* in the common log format for all requests
|
* With no arguments, a query log entry is written to *stdout* in the common log format for all requests
|
||||||
|
|
||||||
~~~ txt
|
|
||||||
log [stdout]
|
|
||||||
~~~
|
|
||||||
|
|
||||||
Or if you want/need slightly more control:
|
Or if you want/need slightly more control:
|
||||||
|
|
||||||
~~~ txt
|
~~~ txt
|
||||||
log [NAME] stdout [FORMAT]
|
log [NAME] [FORMAT]
|
||||||
~~~
|
~~~
|
||||||
|
|
||||||
* `NAME` is the name to match in order to be logged
|
* `NAME` is the name to match in order to be logged
|
||||||
|
@ -26,7 +22,7 @@ log [NAME] stdout [FORMAT]
|
||||||
You can further specify the class of responses that get logged:
|
You can further specify the class of responses that get logged:
|
||||||
|
|
||||||
~~~ txt
|
~~~ txt
|
||||||
log [NAME] stdout [FORMAT] {
|
log [NAME] [FORMAT] {
|
||||||
class [success|denial|error|all]
|
class [success|denial|error|all]
|
||||||
}
|
}
|
||||||
~~~
|
~~~
|
||||||
|
@ -88,7 +84,7 @@ Custom log format, for all zones (`.`)
|
||||||
|
|
||||||
~~~ corefile
|
~~~ corefile
|
||||||
. {
|
. {
|
||||||
log . stdout "{proto} Request: {name} {type} {>id}"
|
log . "{proto} Request: {name} {type} {>id}"
|
||||||
}
|
}
|
||||||
~~~
|
~~~
|
||||||
|
|
||||||
|
@ -96,7 +92,7 @@ Only log denials for example.org (and below to a file)
|
||||||
|
|
||||||
~~~ corefile
|
~~~ corefile
|
||||||
. {
|
. {
|
||||||
log example.org stdout {
|
log example.org {
|
||||||
class denial
|
class denial
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -77,8 +77,6 @@ type Rule struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
// DefaultLogFilename is the default output name. This is the only supported value.
|
|
||||||
DefaultLogFilename = "stdout"
|
|
||||||
// CommonLogFormat is the common log format.
|
// CommonLogFormat is the common log format.
|
||||||
CommonLogFormat = `{remote} ` + CommonLogEmptyValue + ` [{when}] "{type} {class} {name} {proto} {size} {>do} {>bufsize}" {rcode} {>rflags} {rsize} {duration}`
|
CommonLogFormat = `{remote} ` + CommonLogEmptyValue + ` [{when}] "{type} {class} {name} {proto} {size} {>do} {>bufsize}" {rcode} {>rflags} {rsize} {duration}`
|
||||||
// CommonLogEmptyValue is the common empty log value.
|
// CommonLogEmptyValue is the common empty log value.
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
package log
|
package log
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
|
@ -55,32 +54,21 @@ func logParse(c *caddy.Controller) ([]Rule, error) {
|
||||||
Format: DefaultLogFormat,
|
Format: DefaultLogFormat,
|
||||||
})
|
})
|
||||||
} else if len(args) == 1 {
|
} 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{
|
rules = append(rules, Rule{
|
||||||
NameScope: ".",
|
NameScope: dns.Fqdn(args[0]),
|
||||||
Format: DefaultLogFormat,
|
Format: DefaultLogFormat,
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
// Name scope, output file (stdout), and maybe a format specified
|
// Name scope, and maybe a format specified
|
||||||
|
|
||||||
format := DefaultLogFormat
|
format := DefaultLogFormat
|
||||||
|
|
||||||
if len(args) > 2 {
|
switch args[1] {
|
||||||
switch args[2] {
|
|
||||||
case "{common}":
|
case "{common}":
|
||||||
format = CommonLogFormat
|
format = CommonLogFormat
|
||||||
case "{combined}":
|
case "{combined}":
|
||||||
format = CombinedLogFormat
|
format = CombinedLogFormat
|
||||||
default:
|
default:
|
||||||
format = args[2]
|
format = args[1]
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if args[1] != "stdout" {
|
|
||||||
return nil, fmt.Errorf("only stdout is allowed: %s", args[1])
|
|
||||||
}
|
}
|
||||||
|
|
||||||
rules = append(rules, Rule{
|
rules = append(rules, Rule{
|
||||||
|
|
|
@ -18,32 +18,28 @@ func TestLogParse(t *testing.T) {
|
||||||
NameScope: ".",
|
NameScope: ".",
|
||||||
Format: DefaultLogFormat,
|
Format: DefaultLogFormat,
|
||||||
}}},
|
}}},
|
||||||
{`log example.org stdout`, false, []Rule{{
|
{`log example.org`, false, []Rule{{
|
||||||
NameScope: "example.org.",
|
NameScope: "example.org.",
|
||||||
Format: DefaultLogFormat,
|
Format: DefaultLogFormat,
|
||||||
}}},
|
}}},
|
||||||
{`log example.org. stdout`, false, []Rule{{
|
{`log example.org. {common}`, false, []Rule{{
|
||||||
NameScope: "example.org.",
|
|
||||||
Format: DefaultLogFormat,
|
|
||||||
}}},
|
|
||||||
{`log example.org stdout {common}`, false, []Rule{{
|
|
||||||
NameScope: "example.org.",
|
NameScope: "example.org.",
|
||||||
Format: CommonLogFormat,
|
Format: CommonLogFormat,
|
||||||
}}},
|
}}},
|
||||||
{`log example.org stdout {combined}`, false, []Rule{{
|
{`log example.org {combined}`, false, []Rule{{
|
||||||
NameScope: "example.org.",
|
NameScope: "example.org.",
|
||||||
Format: CombinedLogFormat,
|
Format: CombinedLogFormat,
|
||||||
}}},
|
}}},
|
||||||
{`log example.org. stdout
|
{`log example.org.
|
||||||
log example.net stdout {combined}`, false, []Rule{{
|
log example.net {combined}`, false, []Rule{{
|
||||||
NameScope: "example.org.",
|
NameScope: "example.org.",
|
||||||
Format: DefaultLogFormat,
|
Format: DefaultLogFormat,
|
||||||
}, {
|
}, {
|
||||||
NameScope: "example.net.",
|
NameScope: "example.net.",
|
||||||
Format: CombinedLogFormat,
|
Format: CombinedLogFormat,
|
||||||
}}},
|
}}},
|
||||||
{`log example.org stdout {host}
|
{`log example.org {host}
|
||||||
log example.org stdout {when}`, false, []Rule{{
|
log example.org {when}`, false, []Rule{{
|
||||||
NameScope: "example.org.",
|
NameScope: "example.org.",
|
||||||
Format: "{host}",
|
Format: "{host}",
|
||||||
}, {
|
}, {
|
||||||
|
@ -51,14 +47,14 @@ func TestLogParse(t *testing.T) {
|
||||||
Format: "{when}",
|
Format: "{when}",
|
||||||
}}},
|
}}},
|
||||||
|
|
||||||
{`log example.org stdout {
|
{`log example.org {
|
||||||
class all
|
class all
|
||||||
}`, false, []Rule{{
|
}`, false, []Rule{{
|
||||||
NameScope: "example.org.",
|
NameScope: "example.org.",
|
||||||
Format: CommonLogFormat,
|
Format: CommonLogFormat,
|
||||||
Class: response.All,
|
Class: response.All,
|
||||||
}}},
|
}}},
|
||||||
{`log example.org stdout {
|
{`log example.org {
|
||||||
class denial
|
class denial
|
||||||
}`, false, []Rule{{
|
}`, false, []Rule{{
|
||||||
NameScope: "example.org.",
|
NameScope: "example.org.",
|
||||||
|
@ -72,17 +68,16 @@ func TestLogParse(t *testing.T) {
|
||||||
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)
|
||||||
actualLogRules, err := logParse(c)
|
actualLogRules, err := logParse(c)
|
||||||
|
|
||||||
if err == nil && test.shouldErr {
|
if err == nil && test.shouldErr {
|
||||||
t.Errorf("Test %d didn't error, but it should have", i)
|
t.Errorf("Test %d with input '%s' didn't error, but it should have", i, test.inputLogRules)
|
||||||
} else if err != nil && !test.shouldErr {
|
} else if err != nil && !test.shouldErr {
|
||||||
t.Errorf("Test %d errored, but it shouldn't have; got '%v'", i, err)
|
t.Errorf("Test %d with input '%s' errored, but it shouldn't have; got '%v'",
|
||||||
|
i, test.inputLogRules, err)
|
||||||
}
|
}
|
||||||
if len(actualLogRules) != len(test.expectedLogRules) {
|
if len(actualLogRules) != len(test.expectedLogRules) {
|
||||||
t.Fatalf("Test %d expected %d no of Log rules, but got %d ",
|
t.Fatalf("Test %d expected %d no of Log rules, but got %d ",
|
||||||
|
@ -91,13 +86,13 @@ func TestLogParse(t *testing.T) {
|
||||||
for j, actualLogRule := range actualLogRules {
|
for j, actualLogRule := range actualLogRules {
|
||||||
|
|
||||||
if actualLogRule.NameScope != test.expectedLogRules[j].NameScope {
|
if actualLogRule.NameScope != test.expectedLogRules[j].NameScope {
|
||||||
t.Errorf("Test %d expected %dth LogRule NameScope to be %s , but got %s",
|
t.Errorf("Test %d expected %dth LogRule NameScope for '%s' to be %s , but got %s",
|
||||||
i, j, test.expectedLogRules[j].NameScope, actualLogRule.NameScope)
|
i, j, test.inputLogRules, test.expectedLogRules[j].NameScope, actualLogRule.NameScope)
|
||||||
}
|
}
|
||||||
|
|
||||||
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 for '%s' to be %s , but got %s",
|
||||||
i, j, test.expectedLogRules[j].Format, actualLogRule.Format)
|
i, j, test.inputLogRules, test.expectedLogRules[j].Format, actualLogRule.Format)
|
||||||
}
|
}
|
||||||
|
|
||||||
if actualLogRule.Class != test.expectedLogRules[j].Class {
|
if actualLogRule.Class != test.expectedLogRules[j].Class {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue