Skip to content

Commit

Permalink
Added log_name directive
Browse files Browse the repository at this point in the history
  • Loading branch information
armadi1809 authored and francislavoie committed May 11, 2024
1 parent 1b12d34 commit aea0e75
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 14 deletions.
16 changes: 13 additions & 3 deletions caddyconfig/httpcaddyfile/builtins.go
Expand Up @@ -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:
Expand Down Expand Up @@ -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{}
noLoggerNames := false
noHostname := false
for h.NextBlock(0) {
switch h.Val() {
case "hostnames":
Expand Down Expand Up @@ -1004,7 +1005,7 @@ func parseLogHelper(h Helper, globalLogNames map[string]struct{}) ([]ConfigValue
if h.NextArg() {
return nil, h.ArgErr()
}
noLoggerNames = true
noHostname = true

default:
return nil, h.Errf("unrecognized subdirective: %s", h.Val())
Expand All @@ -1013,7 +1014,7 @@ func parseLogHelper(h Helper, globalLogNames map[string]struct{}) ([]ConfigValue

var val namedCustomLog
val.hostnames = customHostnames
val.noLoggerNames = noLoggerNames
val.noHostname = noHostname
isEmptyConfig := reflect.DeepEqual(cl, new(caddy.CustomLog))

// Skip handling of empty logging configs
Expand Down Expand Up @@ -1064,3 +1065,12 @@ 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()
access_logger_names := h.RemainingArgs()
return caddyhttp.VarsMiddleware{"access_logger_names": access_logger_names}, nil
}
1 change: 1 addition & 0 deletions caddyconfig/httpcaddyfile/directives.go
Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions caddyconfig/httpcaddyfile/httptype.go
Expand Up @@ -797,7 +797,7 @@ func (st *ServerType) serversFromPairings(
sblockLogHosts := sblock.hostsFromKeys(true)
for _, cval := range sblock.pile["custom_log"] {
ncl := cval.Value.(namedCustomLog)
if ncl.noLoggerNames {
if ncl.noHostname {
continue
}
if sblock.hasHostCatchAllKey() && len(ncl.hostnames) == 0 {
Expand Down Expand Up @@ -1601,10 +1601,10 @@ func (c counter) nextGroup() string {
}

type namedCustomLog struct {
name string
hostnames []string
log *caddy.CustomLog
noLoggerNames bool
name string
hostnames []string
log *caddy.CustomLog
noHostname bool
}

// sbAddrAssociation is a mapping from a list of
Expand Down
Expand Up @@ -12,7 +12,7 @@ localhost {
}
@healthCheck `header_regexp('User-Agent', '^some-regexp$') || path('/healthz*')`
handle @healthCheck {
vars access_logger_names "health_check_log,general_log"
log_name "health_check_log" "general_log"
respond "Healthy"
}

Expand Down Expand Up @@ -89,7 +89,10 @@ localhost {
{
"handle": [
{
"access_logger_names": "health_check_log,general_log",
"access_logger_names": [
"health_check_log",
"general_log"
],
"handler": "vars"
},
{
Expand Down
9 changes: 5 additions & 4 deletions modules/caddyhttp/logging.go
Expand Up @@ -80,10 +80,11 @@ func (slc ServerLogConfig) wrapLogger(logger *zap.Logger, req *http.Request) []*
}
hosts := slc.getLoggerHosts(hostWithoutPort)
loggers := make([]*zap.Logger, 0, len(hosts))
if access_logger_names := GetVar(req.Context(), AccessLoggerNameVarKey); access_logger_names != nil {
accessLoggerNamesSlice := strings.Split(access_logger_names.(string), ",")
for _, loggerName := range accessLoggerNamesSlice {
loggers = append(loggers, logger.Named(loggerName))
if names := GetVar(req.Context(), AccessLoggerNameVarKey); names != nil {
if namesSlice, ok := names.([]any); ok {
for _, loggerName := range namesSlice {
loggers = append(loggers, logger.Named(loggerName.(string)))
}
}
return loggers
}
Expand Down

0 comments on commit aea0e75

Please sign in to comment.