Commit e391a393 authored by Yong Tang's avatar Yong Tang Committed by GitHub

[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: default avatarYong Tang <yong.tang.github@outlook.com>
parent 4b864a97
...@@ -53,15 +53,9 @@ func logParse(c *caddy.Controller) ([]Rule, error) { ...@@ -53,15 +53,9 @@ func logParse(c *caddy.Controller) ([]Rule, error) {
format := DefaultLogFormat format := DefaultLogFormat
if strings.Contains(args[len(args)-1], "{") { if strings.Contains(args[len(args)-1], "{") {
switch args[len(args)-1] { format = args[len(args)-1]
case "{common}": format = strings.Replace(format, "{common}", CommonLogFormat, -1)
format = CommonLogFormat format = strings.Replace(format, "{combined}", CombinedLogFormat, -1)
case "{combined}":
format = CombinedLogFormat
default:
format = args[len(args)-1]
}
args = args[:len(args)-1] args = args[:len(args)-1]
} }
......
...@@ -129,6 +129,26 @@ func TestLogParse(t *testing.T) { ...@@ -129,6 +129,26 @@ func TestLogParse(t *testing.T) {
{`log { {`log {
unknown unknown
}`, true, []Rule{}}, }`, 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 { for i, test := range tests {
c := caddy.NewTestController("dns", test.inputLogRules) c := caddy.NewTestController("dns", test.inputLogRules)
...@@ -141,7 +161,7 @@ func TestLogParse(t *testing.T) { ...@@ -141,7 +161,7 @@ func TestLogParse(t *testing.T) {
i, test.inputLogRules, err) 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",
i, len(test.expectedLogRules), len(actualLogRules)) i, len(test.expectedLogRules), len(actualLogRules))
} }
for j, actualLogRule := range actualLogRules { for j, actualLogRule := range actualLogRules {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment