summaryrefslogtreecommitdiff
path: root/caddyconfig/httpcaddyfile/httptype.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/httptype.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/httptype.go')
-rw-r--r--caddyconfig/httpcaddyfile/httptype.go49
1 files changed, 36 insertions, 13 deletions
diff --git a/caddyconfig/httpcaddyfile/httptype.go b/caddyconfig/httpcaddyfile/httptype.go
index f9195a6..4288076 100644
--- a/caddyconfig/httpcaddyfile/httptype.go
+++ b/caddyconfig/httpcaddyfile/httptype.go
@@ -240,20 +240,29 @@ func (st ServerType) Setup(inputServerBlocks []caddyfile.ServerBlock,
// extract any custom logs, and enforce configured levels
var customLogs []namedCustomLog
var hasDefaultLog bool
+ addCustomLog := func(ncl namedCustomLog) {
+ if ncl.name == "" {
+ return
+ }
+ if ncl.name == "default" {
+ hasDefaultLog = true
+ }
+ if _, ok := options["debug"]; ok && ncl.log.Level == "" {
+ ncl.log.Level = "DEBUG"
+ }
+ customLogs = append(customLogs, ncl)
+ }
+ // Apply global log options, when set
+ if options["log"] != nil {
+ for _, logValue := range options["log"].([]ConfigValue) {
+ addCustomLog(logValue.Value.(namedCustomLog))
+ }
+ }
+ // Apply server-specific log options
for _, p := range pairings {
for _, sb := range p.serverBlocks {
for _, clVal := range sb.pile["custom_log"] {
- ncl := clVal.Value.(namedCustomLog)
- if ncl.name == "" {
- continue
- }
- if ncl.name == "default" {
- hasDefaultLog = true
- }
- if _, ok := options["debug"]; ok && ncl.log.Level == "" {
- ncl.log.Level = "DEBUG"
- }
- customLogs = append(customLogs, ncl)
+ addCustomLog(clVal.Value.(namedCustomLog))
}
}
}
@@ -313,7 +322,7 @@ func (st ServerType) Setup(inputServerBlocks []caddyfile.ServerBlock,
// most users seem to prefer not writing access logs
// to the default log when they are directed to a
// file or have any other special customization
- if len(ncl.log.Include) > 0 {
+ if ncl.name != "default" && len(ncl.log.Include) > 0 {
defaultLog, ok := cfg.Logging.Logs["default"]
if !ok {
defaultLog = new(caddy.CustomLog)
@@ -362,11 +371,25 @@ func (ServerType) evaluateGlobalOptionsBlock(serverBlocks []serverBlock, options
}
serverOpts, ok := val.(serverOptions)
if !ok {
- return nil, fmt.Errorf("unexpected type from 'servers' global options")
+ return nil, fmt.Errorf("unexpected type from 'servers' global options: %T", val)
}
options[opt] = append(existingOpts, serverOpts)
continue
}
+ // Additionally, fold multiple "log" options together into an
+ // array so that multiple loggers can be configured.
+ if opt == "log" {
+ existingOpts, ok := options[opt].([]ConfigValue)
+ if !ok {
+ existingOpts = []ConfigValue{}
+ }
+ logOpts, ok := val.([]ConfigValue)
+ if !ok {
+ return nil, fmt.Errorf("unexpected type from 'log' global options: %T", val)
+ }
+ options[opt] = append(existingOpts, logOpts...)
+ continue
+ }
options[opt] = val
}