Skip to content

Commit

Permalink
httpcaddyfile: Allow hostname 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 2906f7c
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 10 deletions.
23 changes: 19 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 {
// hostname <hostname>
// output <writer_module> ...
// format <encoder_module> ...
// level <level>
Expand Down Expand Up @@ -842,8 +843,22 @@ 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
customHostname := ""

for h.NextBlock(0) {
switch h.Val() {
case "hostname":
if parseAsGlobalOption {
return nil, h.Err("hostname is not allowed in the log global options")
}
if !h.NextArg() {
return nil, h.ArgErr()
}
customHostname = h.Val()

case "output":
if !h.NextArg() {
return nil, h.ArgErr()
Expand Down Expand Up @@ -902,18 +917,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 +938,8 @@ func parseLogHelper(h Helper, globalLogNames map[string]struct{}) ([]ConfigValue
}

var val namedCustomLog
val.hostname = customHostname

// Skip handling of empty logging configs
if !reflect.DeepEqual(cl, new(caddy.CustomLog)) {
if parseAsGlobalOption {
Expand Down
21 changes: 15 additions & 6 deletions caddyconfig/httpcaddyfile/httptype.go
Original file line number Diff line number Diff line change
Expand Up @@ -770,17 +770,25 @@ 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() && ncl.hostname == "" {
// 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 {
// map each host to the user's desired logger name
for _, h := range sblockLogHosts {
if ncl.hostname != "" {
// if the logger overrides the host, map that to the logger name;
if srv.Logs.LoggerNames == nil {
srv.Logs.LoggerNames = make(map[string]string)
}
srv.Logs.LoggerNames[h] = ncl.name
srv.Logs.LoggerNames[ncl.hostname] = ncl.name
} else {
// otherwise, map each host to the logger name
for _, h := range sblockLogHosts {
if srv.Logs.LoggerNames == nil {
srv.Logs.LoggerNames = make(map[string]string)
}
srv.Logs.LoggerNames[h] = ncl.name
}
}
}
}
Expand Down Expand Up @@ -1564,8 +1572,9 @@ func (c counter) nextGroup() string {
}

type namedCustomLog struct {
name string
log *caddy.CustomLog
name string
hostname string
log *caddy.CustomLog
}

// sbAddrAssociation is a mapping from a list of
Expand Down
70 changes: 70 additions & 0 deletions caddytest/integration/caddyfile_adapt/log_override_hostname.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
*.example.com {
log {
hostname foo.example.com
output file /foo.txt
}
log {
hostname bar.example.com
output file /bar.txt
}
}
----------
{
"logging": {
"logs": {
"default": {
"exclude": [
"http.log.access.log0",
"http.log.access.log1"
]
},
"log0": {
"writer": {
"filename": "/foo.txt",
"output": "file"
},
"include": [
"http.log.access.log0"
]
},
"log1": {
"writer": {
"filename": "/bar.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": "log1",
"foo.example.com": "log0"
}
}
}
}
}
}
}

0 comments on commit 2906f7c

Please sign in to comment.