Skip to content

Commit

Permalink
Implement access logger name overrides
Browse files Browse the repository at this point in the history
  • Loading branch information
francislavoie committed Jul 25, 2023
1 parent 4511ebe commit 017d63a
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 20 deletions.
52 changes: 34 additions & 18 deletions caddyconfig/httpcaddyfile/builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -788,7 +788,7 @@ func parseInvoke(h Helper) (caddyhttp.MiddlewareHandler, error) {

// parseLog parses the log directive. Syntax:
//
// log {
// log <logger_name> {
// hostnames <hostnames...>
// output <writer_module> ...
// format <encoder_module> ...
Expand All @@ -810,11 +810,13 @@ func parseLogHelper(h Helper, globalLogNames map[string]struct{}) ([]ConfigValue
var configValues []ConfigValue
for h.Next() {
// Logic below expects that a name is always present when a
// global option is being parsed.
var globalLogName string
// global option is being parsed; or an optional override
// is supported for access logs.
var logName string

if parseAsGlobalOption {
if h.NextArg() {
globalLogName = h.Val()
logName = h.Val()

// Only a single argument is supported.
if h.NextArg() {
Expand All @@ -825,19 +827,25 @@ func parseLogHelper(h Helper, globalLogNames map[string]struct{}) ([]ConfigValue
// reference the default logger. See the
// setupNewDefault function in the logging
// package for where this is configured.
globalLogName = caddy.DefaultLoggerName
logName = caddy.DefaultLoggerName
}

// Verify this name is unused.
_, used := globalLogNames[globalLogName]
_, used := globalLogNames[logName]
if used {
return nil, h.Err("duplicate global log option for: " + globalLogName)
return nil, h.Err("duplicate global log option for: " + logName)
}
globalLogNames[globalLogName] = struct{}{}
globalLogNames[logName] = struct{}{}
} else {
// No arguments are supported for the server block log directive
// An optional override of the logger name can be provided;
// otherwise a default will be used, like "log0", "log1", etc.
if h.NextArg() {
return nil, h.ArgErr()
logName = h.Val()

// Only a single argument is supported.
if h.NextArg() {
return nil, h.ArgErr()
}
}
}

Expand Down Expand Up @@ -941,24 +949,32 @@ func parseLogHelper(h Helper, globalLogNames map[string]struct{}) ([]ConfigValue
var val namedCustomLog
val.hostnames = customHostnames

isEmptyConfig := reflect.DeepEqual(cl, new(caddy.CustomLog))

// Skip handling of empty logging configs
if !reflect.DeepEqual(cl, new(caddy.CustomLog)) {
if parseAsGlobalOption {
// Use indicated name for global log options
val.name = globalLogName
val.log = cl
} else {

if parseAsGlobalOption {
// Use indicated name for global log options
val.name = logName
} else {
if logName != "" {
val.name = logName
} else if !isEmptyConfig {
// 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
}
if val.name != "" {
cl.Include = []string{"http.log.access." + val.name}
}
}
if !isEmptyConfig {
val.log = cl
}
configValues = append(configValues, ConfigValue{
Class: "custom_log",
Expand Down
5 changes: 3 additions & 2 deletions caddyconfig/httpcaddyfile/builtins_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,13 @@ func TestLogDirectiveSyntax(t *testing.T) {
},
{
input: `:8080 {
log invalid {
log name-override {
output file foo.log
}
}
`,
expectError: true,
output: `{"logging":{"logs":{"default":{"exclude":["http.log.access.name-override"]},"name-override":{"writer":{"filename":"foo.log","output":"file"},"include":["http.log.access.name-override"]}}},"apps":{"http":{"servers":{"srv0":{"listen":[":8080"],"logs":{"default_logger_name":"name-override"}}}}}}`,
expectError: false,
},
} {

Expand Down
12 changes: 12 additions & 0 deletions caddyconfig/httpcaddyfile/httptype.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"github.com/caddyserver/caddy/v2/modules/caddypki"
"github.com/caddyserver/caddy/v2/modules/caddytls"
"go.uber.org/zap"
"golang.org/x/exp/slices"
)

func init() {
Expand Down Expand Up @@ -325,6 +326,9 @@ func (st ServerType) Setup(
}
}
for _, ncl := range customLogs {
if ncl.log == nil {
continue
}
if ncl.name != "" {
cfg.Logging.Logs[ncl.name] = ncl.log
}
Expand All @@ -338,8 +342,16 @@ func (st ServerType) Setup(
cfg.Logging.Logs[caddy.DefaultLoggerName] = defaultLog
}
defaultLog.Exclude = append(defaultLog.Exclude, ncl.log.Include...)

// avoid duplicates by sorting + compacting
slices.Sort[string](defaultLog.Exclude)
defaultLog.Exclude = slices.Compact[[]string, string](defaultLog.Exclude)
}
}
// we may have not actually added anything, so remove if empty
if len(cfg.Logging.Logs) == 0 {
cfg.Logging = nil
}
}

return cfg, warnings, nil
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
{
log access-console {
include http.log.access.foo
output file access-localhost.log
format console
}

log access-json {
include http.log.access.foo
output file access-localhost.json
format json
}
}

http://localhost:8881 {
log foo
}
----------
{
"logging": {
"logs": {
"access-console": {
"writer": {
"filename": "access-localhost.log",
"output": "file"
},
"encoder": {
"format": "console"
},
"include": [
"http.log.access.foo"
]
},
"access-json": {
"writer": {
"filename": "access-localhost.json",
"output": "file"
},
"encoder": {
"format": "json"
},
"include": [
"http.log.access.foo"
]
},
"default": {
"exclude": [
"http.log.access.foo"
]
}
}
},
"apps": {
"http": {
"servers": {
"srv0": {
"listen": [
":8881"
],
"routes": [
{
"match": [
{
"host": [
"localhost"
]
}
],
"terminal": true
}
],
"automatic_https": {
"skip": [
"localhost"
]
},
"logs": {
"logger_names": {
"localhost:8881": "foo"
}
}
}
}
}
}
}

0 comments on commit 017d63a

Please sign in to comment.