diff options
author | Aziz Rmadi <[email protected]> | 2024-05-11 08:31:44 -0500 |
---|---|---|
committer | GitHub <[email protected]> | 2024-05-11 13:31:44 +0000 |
commit | 4356635d123ee71b72654738a0c30653a3a29d98 (patch) | |
tree | 4f81798ae58c0e6288cc08b5475f52d7c20e094a /caddyconfig | |
parent | 4af38e5ac8622df412ef8043f348185b050ec0b9 (diff) | |
download | caddy-4356635d123ee71b72654738a0c30653a3a29d98.tar.gz caddy-4356635d123ee71b72654738a0c30653a3a29d98.zip |
logging: Add support for additional logger filters other than hostname (#6082)
Co-authored-by: Francis Lavoie <[email protected]>
Diffstat (limited to 'caddyconfig')
-rw-r--r-- | caddyconfig/httpcaddyfile/builtins.go | 21 | ||||
-rw-r--r-- | caddyconfig/httpcaddyfile/directives.go | 1 | ||||
-rw-r--r-- | caddyconfig/httpcaddyfile/httptype.go | 16 |
3 files changed, 33 insertions, 5 deletions
diff --git a/caddyconfig/httpcaddyfile/builtins.go b/caddyconfig/httpcaddyfile/builtins.go index 27ab0a182..35a08ef27 100644 --- a/caddyconfig/httpcaddyfile/builtins.go +++ b/caddyconfig/httpcaddyfile/builtins.go @@ -51,6 +51,7 @@ func init() { RegisterDirective("log", parseLog) RegisterHandlerDirective("skip_log", parseLogSkip) RegisterHandlerDirective("log_skip", parseLogSkip) + RegisterHandlerDirective("log_name", parseLogName) } // parseBind parses the bind directive. Syntax: @@ -914,7 +915,7 @@ func parseLogHelper(h Helper, globalLogNames map[string]struct{}) ([]ConfigValue // this is useful for setting up loggers per subdomain in a site block // with a wildcard domain customHostnames := []string{} - + noHostname := false for h.NextBlock(0) { switch h.Val() { case "hostnames": @@ -1000,6 +1001,12 @@ func parseLogHelper(h Helper, globalLogNames map[string]struct{}) ([]ConfigValue cl.Exclude = append(cl.Exclude, h.Val()) } + case "no_hostname": + if h.NextArg() { + return nil, h.ArgErr() + } + noHostname = true + default: return nil, h.Errf("unrecognized subdirective: %s", h.Val()) } @@ -1007,7 +1014,7 @@ func parseLogHelper(h Helper, globalLogNames map[string]struct{}) ([]ConfigValue var val namedCustomLog val.hostnames = customHostnames - + val.noHostname = noHostname isEmptyConfig := reflect.DeepEqual(cl, new(caddy.CustomLog)) // Skip handling of empty logging configs @@ -1058,3 +1065,13 @@ func parseLogSkip(h Helper) (caddyhttp.MiddlewareHandler, error) { } return caddyhttp.VarsMiddleware{"log_skip": true}, nil } + +// parseLogName parses the log_name directive. Syntax: +// +// log_name <names...> +func parseLogName(h Helper) (caddyhttp.MiddlewareHandler, error) { + h.Next() // consume directive name + return caddyhttp.VarsMiddleware{ + caddyhttp.AccessLoggerNameVarKey: h.RemainingArgs(), + }, nil +} diff --git a/caddyconfig/httpcaddyfile/directives.go b/caddyconfig/httpcaddyfile/directives.go index 172594218..3e688ebcf 100644 --- a/caddyconfig/httpcaddyfile/directives.go +++ b/caddyconfig/httpcaddyfile/directives.go @@ -53,6 +53,7 @@ var defaultDirectiveOrder = []string{ "log_append", "skip_log", // TODO: deprecated, renamed to log_skip "log_skip", + "log_name", "header", "copy_response_headers", // only in reverse_proxy's handle_response diff --git a/caddyconfig/httpcaddyfile/httptype.go b/caddyconfig/httpcaddyfile/httptype.go index 8e7d21fa0..a8a2ae5b3 100644 --- a/caddyconfig/httpcaddyfile/httptype.go +++ b/caddyconfig/httpcaddyfile/httptype.go @@ -797,6 +797,15 @@ func (st *ServerType) serversFromPairings( sblockLogHosts := sblock.hostsFromKeys(true) for _, cval := range sblock.pile["custom_log"] { ncl := cval.Value.(namedCustomLog) + + // if `no_hostname` is set, then this logger will not + // be associated with any of the site block's hostnames, + // and only be usable via the `log_name` directive + // or the `access_logger_names` variable + if ncl.noHostname { + continue + } + 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 @@ -1598,9 +1607,10 @@ func (c counter) nextGroup() string { } type namedCustomLog struct { - name string - hostnames []string - log *caddy.CustomLog + name string + hostnames []string + log *caddy.CustomLog + noHostname bool } // sbAddrAssociation is a mapping from a list of |