From 710f38043e54402f3ecd7b4e1e29b986df589adc Mon Sep 17 00:00:00 2001 From: Tw Date: Thu, 11 May 2017 16:15:41 +0800 Subject: [PATCH] log: allow additional prefix/suffix with predefined format Signed-off-by: Tw --- caddyhttp/log/setup.go | 12 ++++-------- caddyhttp/log/setup_test.go | 20 ++++++++++++++++++++ 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/caddyhttp/log/setup.go b/caddyhttp/log/setup.go index 47b25978..f02faf82 100644 --- a/caddyhttp/log/setup.go +++ b/caddyhttp/log/setup.go @@ -1,6 +1,8 @@ package log import ( + "strings" + "github.com/mholt/caddy" "github.com/mholt/caddy/caddyhttp/httpserver" ) @@ -75,14 +77,8 @@ func logParse(c *caddy.Controller) ([]*Rule, error) { format := DefaultLogFormat if len(args) > 2 { - switch args[2] { - case "{common}": - format = CommonLogFormat - case "{combined}": - format = CombinedLogFormat - default: - format = args[2] - } + format = strings.Replace(args[2], "{common}", CommonLogFormat, -1) + format = strings.Replace(format, "{combined}", CombinedLogFormat, -1) } rules = appendEntry(rules, args[0], &Entry{ diff --git a/caddyhttp/log/setup_test.go b/caddyhttp/log/setup_test.go index d248f394..35e7c532 100644 --- a/caddyhttp/log/setup_test.go +++ b/caddyhttp/log/setup_test.go @@ -124,6 +124,16 @@ func TestLogParse(t *testing.T) { Format: CommonLogFormat, }}, }}}, + {`log /myapi log.txt "prefix {common} suffix"`, false, []Rule{{ + PathScope: "/myapi", + Entries: []*Entry{{ + Log: &httpserver.Logger{ + Output: "log.txt", + Roller: httpserver.DefaultLogRoller(), + }, + Format: "prefix " + CommonLogFormat + " suffix", + }}, + }}}, {`log /test accesslog.txt {combined}`, false, []Rule{{ PathScope: "/test", Entries: []*Entry{{ @@ -134,6 +144,16 @@ func TestLogParse(t *testing.T) { Format: CombinedLogFormat, }}, }}}, + {`log /test accesslog.txt "prefix {combined} suffix"`, false, []Rule{{ + PathScope: "/test", + Entries: []*Entry{{ + Log: &httpserver.Logger{ + Output: "accesslog.txt", + Roller: httpserver.DefaultLogRoller(), + }, + Format: "prefix " + CombinedLogFormat + " suffix", + }}, + }}}, {`log /api1 log.txt log /api2 accesslog.txt {combined}`, false, []Rule{{ PathScope: "/api1",