mirror of
https://github.com/caddyserver/caddy.git
synced 2025-01-23 17:16:40 +01:00
log: allow additional prefix/suffix with predefined format
Signed-off-by: Tw <tw19881113@gmail.com>
This commit is contained in:
parent
958abcfa4c
commit
710f38043e
2 changed files with 24 additions and 8 deletions
|
@ -1,6 +1,8 @@
|
||||||
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) {
|
||||||
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) {
|
||||||
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) {
|
||||||
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",
|
||||||
|
|
Loading…
Reference in a new issue