summaryrefslogtreecommitdiff
path: root/caddyconfig/httpcaddyfile/builtins.go
diff options
context:
space:
mode:
authorAaron Taylor <git@ataylor.io>2021-03-12 15:00:02 -0500
committerGitHub <noreply@github.com>2021-03-12 13:00:02 -0700
commit2a127ac3d1650052f39e93f277e1dfd8c7c730e4 (patch)
tree912bb9a181d0a4139b5b3320ff1bb8c938da5c04 /caddyconfig/httpcaddyfile/builtins.go
parent802f80c3821940825ec278e1d9c153a321e956a9 (diff)
caddyconfig: add global option for configuring loggers (#4028)
This change is aimed at enhancing the logging module within the Caddyfile directive to allow users to configure logs other than the HTTP access log stream, which is the current capability of the Caddyfile [1]. The intent here is to leverage the same syntax as the server log directive at a global level, so that similar customizations can be added without needing to resort to a JSON-based configuration. Discussion for this approach happened in the referenced issue. Closes https://github.com/caddyserver/caddy/issues/3958 [1] https://caddyserver.com/docs/caddyfile/directives/log
Diffstat (limited to 'caddyconfig/httpcaddyfile/builtins.go')
-rw-r--r--caddyconfig/httpcaddyfile/builtins.go87
1 files changed, 76 insertions, 11 deletions
diff --git a/caddyconfig/httpcaddyfile/builtins.go b/caddyconfig/httpcaddyfile/builtins.go
index 8a8f3cc..8830f52 100644
--- a/caddyconfig/httpcaddyfile/builtins.go
+++ b/caddyconfig/httpcaddyfile/builtins.go
@@ -619,11 +619,50 @@ func parseHandleErrors(h Helper) ([]ConfigValue, error) {
// }
//
func parseLog(h Helper) ([]ConfigValue, error) {
+ return parseLogHelper(h, nil)
+}
+
+// parseLogHelper is used both for the parseLog directive within Server Blocks,
+// as well as the global "log" option for configuring loggers at the global
+// level. The parseAsGlobalOption parameter is used to distinguish any differing logic
+// between the two.
+func parseLogHelper(h Helper, globalLogNames map[string]struct{}) ([]ConfigValue, error) {
+ // When the globalLogNames parameter is passed in, we make
+ // modifications to the parsing behavior.
+ parseAsGlobalOption := globalLogNames != nil
+
var configValues []ConfigValue
for h.Next() {
- // log does not currently support any arguments
- if h.NextArg() {
- return nil, h.ArgErr()
+ // Logic below expects that a name is always present when a
+ // global option is being parsed.
+ var globalLogName string
+ if parseAsGlobalOption {
+ if h.NextArg() {
+ globalLogName = h.Val()
+
+ // Only a single argument is supported.
+ if h.NextArg() {
+ return nil, h.ArgErr()
+ }
+ } else {
+ // If there is no log name specified, we
+ // reference the default logger. See the
+ // setupNewDefault function in the logging
+ // package for where this is configured.
+ globalLogName = "default"
+ }
+
+ // Verify this name is unused.
+ _, used := globalLogNames[globalLogName]
+ if used {
+ return nil, h.Err("duplicate global log option for: " + globalLogName)
+ }
+ globalLogNames[globalLogName] = struct{}{}
+ } else {
+ // No arguments are supported for the server block log directive
+ if h.NextArg() {
+ return nil, h.ArgErr()
+ }
}
cl := new(caddy.CustomLog)
@@ -687,22 +726,48 @@ func parseLog(h Helper) ([]ConfigValue, error) {
return nil, h.ArgErr()
}
+ case "include":
+ // This configuration is only allowed in the global options
+ if !parseAsGlobalOption {
+ return nil, h.ArgErr()
+ }
+ 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()
+ }
+ for h.NextArg() {
+ cl.Exclude = append(cl.Exclude, h.Val())
+ }
+
default:
return nil, h.Errf("unrecognized subdirective: %s", h.Val())
}
}
var val namedCustomLog
+ // Skip handling of empty logging configs
if !reflect.DeepEqual(cl, new(caddy.CustomLog)) {
- logCounter, ok := h.State["logCounter"].(int)
- if !ok {
- logCounter = 0
+ if parseAsGlobalOption {
+ // Use indicated name for global log options
+ val.name = globalLogName
+ val.log = cl
+ } else {
+ // Construct a log name for server log streams
+ logCounter, ok := h.State["logCounter"].(int)
+ if !ok {
+ logCounter = 0
+ }
+ val.name = fmt.Sprintf("log%d", logCounter)
+ cl.Include = []string{"http.log.access." + val.name}
+ val.log = cl
+ logCounter++
+ h.State["logCounter"] = logCounter
}
- val.name = fmt.Sprintf("log%d", logCounter)
- cl.Include = []string{"http.log.access." + val.name}
- val.log = cl
- logCounter++
- h.State["logCounter"] = logCounter
}
configValues = append(configValues, ConfigValue{
Class: "custom_log",