Commit 6bb84ba1 authored by Matt Holt's avatar Matt Holt Committed by GitHub

Merge pull request #1664 from tw4452852/1663-log

log: allow additional prefix/suffix with predefined format
parents 958abcfa 710f3804
package log package log
import ( import (
"strings"
"github.com/mholt/caddy" "github.com/mholt/caddy"
"github.com/mholt/caddy/caddyhttp/httpserver" "github.com/mholt/caddy/caddyhttp/httpserver"
) )
...@@ -75,14 +77,8 @@ func logParse(c *caddy.Controller) ([]*Rule, error) { ...@@ -75,14 +77,8 @@ func logParse(c *caddy.Controller) ([]*Rule, error) {
format := DefaultLogFormat format := DefaultLogFormat
if len(args) > 2 { if len(args) > 2 {
switch args[2] { format = strings.Replace(args[2], "{common}", CommonLogFormat, -1)
case "{common}": format = strings.Replace(format, "{combined}", CombinedLogFormat, -1)
format = CommonLogFormat
case "{combined}":
format = CombinedLogFormat
default:
format = args[2]
}
} }
rules = appendEntry(rules, args[0], &Entry{ rules = appendEntry(rules, args[0], &Entry{
......
...@@ -124,6 +124,16 @@ func TestLogParse(t *testing.T) { ...@@ -124,6 +124,16 @@ func TestLogParse(t *testing.T) {
Format: CommonLogFormat, 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{{ {`log /test accesslog.txt {combined}`, false, []Rule{{
PathScope: "/test", PathScope: "/test",
Entries: []*Entry{{ Entries: []*Entry{{
...@@ -134,6 +144,16 @@ func TestLogParse(t *testing.T) { ...@@ -134,6 +144,16 @@ func TestLogParse(t *testing.T) {
Format: CombinedLogFormat, 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 /api1 log.txt
log /api2 accesslog.txt {combined}`, false, []Rule{{ log /api2 accesslog.txt {combined}`, false, []Rule{{
PathScope: "/api1", PathScope: "/api1",
......
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