Skip to content

Commit

Permalink
httpcaddyfile: Allow hostnames override for log directive
Browse files Browse the repository at this point in the history
  • Loading branch information
francislavoie committed Jul 21, 2023
1 parent f857b32 commit 7c2cf6f
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 8 deletions.
24 changes: 20 additions & 4 deletions caddyconfig/httpcaddyfile/builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -789,6 +789,7 @@ func parseInvoke(h Helper) (caddyhttp.MiddlewareHandler, error) {
// parseLog parses the log directive. Syntax:
//
// log {
// hostnames <hostnames...>
// output <writer_module> ...
// format <encoder_module> ...
// level <level>
Expand Down Expand Up @@ -842,8 +843,23 @@ func parseLogHelper(h Helper, globalLogNames map[string]struct{}) ([]ConfigValue

cl := new(caddy.CustomLog)

// allow overriding the current site block's hostnames for this logger;
// this is useful for setting up loggers per subdomain in a site block
// with a wildcard domain
customHostnames := []string{}

for h.NextBlock(0) {
switch h.Val() {
case "hostnames":
if parseAsGlobalOption {
return nil, h.Err("hostnames is not allowed in the log global options")
}
args := h.RemainingArgs()
if len(args) == 0 {
return nil, h.ArgErr()
}
customHostnames = append(customHostnames, args...)

case "output":
if !h.NextArg() {
return nil, h.ArgErr()
Expand Down Expand Up @@ -902,18 +918,16 @@ func parseLogHelper(h Helper, globalLogNames map[string]struct{}) ([]ConfigValue
}

case "include":
// This configuration is only allowed in the global options
if !parseAsGlobalOption {
return nil, h.ArgErr()
return nil, h.Err("include is not allowed in the log directive")
}
for h.NextArg() {
cl.Include = append(cl.Include, h.Val())
}

case "exclude":
// This configuration is only allowed in the global options
if !parseAsGlobalOption {
return nil, h.ArgErr()
return nil, h.Err("exclude is not allowed in the log directive")
}
for h.NextArg() {
cl.Exclude = append(cl.Exclude, h.Val())
Expand All @@ -925,6 +939,8 @@ func parseLogHelper(h Helper, globalLogNames map[string]struct{}) ([]ConfigValue
}

var val namedCustomLog
val.hostnames = customHostnames

// Skip handling of empty logging configs
if !reflect.DeepEqual(cl, new(caddy.CustomLog)) {
if parseAsGlobalOption {
Expand Down
17 changes: 13 additions & 4 deletions caddyconfig/httpcaddyfile/httptype.go
Original file line number Diff line number Diff line change
Expand Up @@ -770,12 +770,20 @@ func (st *ServerType) serversFromPairings(
sblockLogHosts := sblock.hostsFromKeys(true)
for _, cval := range sblock.pile["custom_log"] {
ncl := cval.Value.(namedCustomLog)
if sblock.hasHostCatchAllKey() {
if sblock.hasHostCatchAllKey() && len(ncl.hostnames) == 0 {
// all requests for hosts not able to be listed should use
// this log because it's a catch-all-hosts server block
srv.Logs.DefaultLoggerName = ncl.name
} else if len(ncl.hostnames) > 0 {
// if the logger overrides the hostnames, map that to the logger name
for _, h := range ncl.hostnames {
if srv.Logs.LoggerNames == nil {
srv.Logs.LoggerNames = make(map[string]string)
}
srv.Logs.LoggerNames[h] = ncl.name
}
} else {
// map each host to the user's desired logger name
// otherwise, map each host to the logger name
for _, h := range sblockLogHosts {
if srv.Logs.LoggerNames == nil {
srv.Logs.LoggerNames = make(map[string]string)
Expand Down Expand Up @@ -1564,8 +1572,9 @@ func (c counter) nextGroup() string {
}

type namedCustomLog struct {
name string
log *caddy.CustomLog
name string
hostnames []string
log *caddy.CustomLog
}

// sbAddrAssociation is a mapping from a list of
Expand Down
71 changes: 71 additions & 0 deletions caddytest/integration/caddyfile_adapt/log_override_hostname.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
*.example.com {
log {
hostnames foo.example.com bar.example.com
output file /foo-bar.txt
}
log {
hostnames baz.example.com
output file /baz.txt
}
}
----------
{
"logging": {
"logs": {
"default": {
"exclude": [
"http.log.access.log0",
"http.log.access.log1"
]
},
"log0": {
"writer": {
"filename": "/foo-bar.txt",
"output": "file"
},
"include": [
"http.log.access.log0"
]
},
"log1": {
"writer": {
"filename": "/baz.txt",
"output": "file"
},
"include": [
"http.log.access.log1"
]
}
}
},
"apps": {
"http": {
"servers": {
"srv0": {
"listen": [
":443"
],
"routes": [
{
"match": [
{
"host": [
"*.example.com"
]
}
],
"terminal": true
}
],
"logs": {
"logger_names": {
"bar.example.com": "log0",
"baz.example.com": "log1",
"foo.example.com": "log0"
}
}
}
}
}
}
}

0 comments on commit 7c2cf6f

Please sign in to comment.