diff options
author | 2022-03-07 06:49:56 -0800 | |
---|---|---|
committer | 2022-03-07 06:49:56 -0800 | |
commit | e391a39322ee8de3913b87149aa535459dcda424 (patch) | |
tree | b2928e95f712391426a3eca124c39a123a2034fc | |
parent | 4b864a97d10630f0d267e626df0f9617a6df5530 (diff) | |
download | coredns-e391a39322ee8de3913b87149aa535459dcda424.tar.gz coredns-e391a39322ee8de3913b87149aa535459dcda424.tar.zst coredns-e391a39322ee8de3913b87149aa535459dcda424.zip |
[plugin/log] Expand `{combined}` and `{common}` in log format (#5230)
This PR tries to address the issue raised in 5223 where `{combined}`
or `{common}` in log format will not expand when `{combined}` or `{common}`
is not the only token in the format.
This PR fixes 5223.
Signed-off-by: Yong Tang <yong.tang.github@outlook.com>
-rw-r--r-- | plugin/log/setup.go | 12 | ||||
-rw-r--r-- | plugin/log/setup_test.go | 22 |
2 files changed, 24 insertions, 10 deletions
diff --git a/plugin/log/setup.go b/plugin/log/setup.go index dc7db52cf..e1d9913e0 100644 --- a/plugin/log/setup.go +++ b/plugin/log/setup.go @@ -53,15 +53,9 @@ func logParse(c *caddy.Controller) ([]Rule, error) { format := DefaultLogFormat if strings.Contains(args[len(args)-1], "{") { - switch args[len(args)-1] { - case "{common}": - format = CommonLogFormat - case "{combined}": - format = CombinedLogFormat - default: - format = args[len(args)-1] - } - + format = args[len(args)-1] + format = strings.Replace(format, "{common}", CommonLogFormat, -1) + format = strings.Replace(format, "{combined}", CombinedLogFormat, -1) args = args[:len(args)-1] } diff --git a/plugin/log/setup_test.go b/plugin/log/setup_test.go index 6defa8ae1..823071070 100644 --- a/plugin/log/setup_test.go +++ b/plugin/log/setup_test.go @@ -129,6 +129,26 @@ func TestLogParse(t *testing.T) { {`log { unknown }`, true, []Rule{}}, + {`log example.org "{combined} {/forward/upstream}"`, false, []Rule{{ + NameScope: "example.org.", + Format: CombinedLogFormat + " {/forward/upstream}", + Class: map[response.Class]struct{}{response.All: {}}, + }}}, + {`log example.org "{common} {/forward/upstream}"`, false, []Rule{{ + NameScope: "example.org.", + Format: CommonLogFormat + " {/forward/upstream}", + Class: map[response.Class]struct{}{response.All: {}}, + }}}, + {`log example.org "{when} {combined} {/forward/upstream}"`, false, []Rule{{ + NameScope: "example.org.", + Format: "{when} " + CombinedLogFormat + " {/forward/upstream}", + Class: map[response.Class]struct{}{response.All: {}}, + }}}, + {`log example.org "{when} {common} {/forward/upstream}"`, false, []Rule{{ + NameScope: "example.org.", + Format: "{when} " + CommonLogFormat + " {/forward/upstream}", + Class: map[response.Class]struct{}{response.All: {}}, + }}}, } for i, test := range tests { c := caddy.NewTestController("dns", test.inputLogRules) @@ -141,7 +161,7 @@ func TestLogParse(t *testing.T) { i, test.inputLogRules, err) } 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", i, len(test.expectedLogRules), len(actualLogRules)) } for j, actualLogRule := range actualLogRules { |