Skip to content

Commit

Permalink
Logging: add support for additional logger filters other than hostname
Browse files Browse the repository at this point in the history
  • Loading branch information
armadi1809 committed Apr 21, 2024
1 parent d00824f commit 4f04d4c
Show file tree
Hide file tree
Showing 5 changed files with 177 additions and 10 deletions.
10 changes: 8 additions & 2 deletions caddyconfig/httpcaddyfile/builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -915,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
for h.NextBlock(0) {
switch h.Val() {
case "hostnames":
Expand Down Expand Up @@ -1001,14 +1001,20 @@ 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()
}
noLoggerNames = true

default:
return nil, h.Errf("unrecognized subdirective: %s", h.Val())
}
}

var val namedCustomLog
val.hostnames = customHostnames

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

// Skip handling of empty logging configs
Expand Down
10 changes: 7 additions & 3 deletions caddyconfig/httpcaddyfile/httptype.go
Original file line number Diff line number Diff line change
Expand Up @@ -797,6 +797,9 @@ func (st *ServerType) serversFromPairings(
sblockLogHosts := sblock.hostsFromKeys(true)
for _, cval := range sblock.pile["custom_log"] {
ncl := cval.Value.(namedCustomLog)
if ncl.noLoggerNames {
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
Expand Down Expand Up @@ -1591,9 +1594,10 @@ func (c counter) nextGroup() string {
}

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

// sbAddrAssociation is a mapping from a list of
Expand Down
148 changes: 148 additions & 0 deletions caddytest/integration/caddyfile_adapt/log_filter_with_header.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
localhost {
log {
output file ./caddy.access.log
}
log health_check_log {
output file ./caddy.access.health.log
no_hostname
}
log general_log {
output file ./caddy.access.general.log
no_hostname
}
@healthCheck `header_regexp('User-Agent', '^some-regexp$') || path('/healthz*')`
handle @healthCheck {
vars access_logger_names "health_check_log,general_log"
respond "Healthy"
}

handle {
respond "Hello World"
}
}
----------
{
"logging": {
"logs": {
"default": {
"exclude": [
"http.log.access.general_log",
"http.log.access.health_check_log",
"http.log.access.log0"
]
},
"general_log": {
"writer": {
"filename": "./caddy.access.general.log",
"output": "file"
},
"include": [
"http.log.access.general_log"
]
},
"health_check_log": {
"writer": {
"filename": "./caddy.access.health.log",
"output": "file"
},
"include": [
"http.log.access.health_check_log"
]
},
"log0": {
"writer": {
"filename": "./caddy.access.log",
"output": "file"
},
"include": [
"http.log.access.log0"
]
}
}
},
"apps": {
"http": {
"servers": {
"srv0": {
"listen": [
":443"
],
"routes": [
{
"match": [
{
"host": [
"localhost"
]
}
],
"handle": [
{
"handler": "subroute",
"routes": [
{
"group": "group2",
"handle": [
{
"handler": "subroute",
"routes": [
{
"handle": [
{
"access_logger_names": "health_check_log,general_log",
"handler": "vars"
},
{
"body": "Healthy",
"handler": "static_response"
}
]
}
]
}
],
"match": [
{
"expression": {
"expr": "header_regexp('User-Agent', '^some-regexp$') || path('/healthz*')",
"name": "healthCheck"
}
}
]
},
{
"group": "group2",
"handle": [
{
"handler": "subroute",
"routes": [
{
"handle": [
{
"body": "Hello World",
"handler": "static_response"
}
]
}
]
}
]
}
]
}
],
"terminal": true
}
],
"logs": {
"logger_names": {
"localhost": [
"log0"
]
}
}
}
}
}
}
}
14 changes: 12 additions & 2 deletions modules/caddyhttp/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,16 @@ type ServerLogConfig struct {

// wrapLogger wraps logger in one or more logger named
// according to user preferences for the given host.
func (slc ServerLogConfig) wrapLogger(logger *zap.Logger, host string) []*zap.Logger {
hosts := slc.getLoggerHosts(host)
func (slc ServerLogConfig) wrapLogger(logger *zap.Logger, req *http.Request) []*zap.Logger {
hosts := slc.getLoggerHosts(req.Host)
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))
}
return loggers
}
for _, loggerName := range hosts {
if loggerName == "" {
continue
Expand Down Expand Up @@ -211,4 +218,7 @@ const (

// For adding additional fields to the access logs
ExtraLogFieldsCtxKey caddy.CtxKey = "extra_log_fields"

// Variable name used to indicate the logger to be used
AccessLoggerNameVarKey string = "access_logger_names"
)
5 changes: 2 additions & 3 deletions modules/caddyhttp/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
errLog = errLog.With(zap.Duration("duration", duration))
errLoggers := []*zap.Logger{errLog}
if s.Logs != nil {
errLoggers = s.Logs.wrapLogger(errLog, r.Host)
errLoggers = s.Logs.wrapLogger(errLog, r)
}

// get the values that will be used to log the error
Expand Down Expand Up @@ -771,7 +771,7 @@ func (s *Server) logRequest(

loggers := []*zap.Logger{accLog}
if s.Logs != nil {
loggers = s.Logs.wrapLogger(accLog, r.Host)
loggers = s.Logs.wrapLogger(accLog, r)
}

// wrapping may return multiple loggers, so we log to all of them
Expand Down Expand Up @@ -825,7 +825,6 @@ func PrepareRequest(r *http.Request, repl *caddy.Replacer, w http.ResponseWriter
ctx = context.WithValue(ctx, OriginalRequestCtxKey, originalRequest(r, &url2))

ctx = context.WithValue(ctx, ExtraLogFieldsCtxKey, new(ExtraLogFields))

r = r.WithContext(ctx)

// once the pointer to the request won't change
Expand Down

0 comments on commit 4f04d4c

Please sign in to comment.