How to configure access logs sampling?

1. The problem I’m having:

Hi, I’m trying to configure access logs sampling for a subset of logs for some specific requests, namely GET /healthz from Caddyfile. I just can’t get the config right. Is that even possible with Caddyfile? If yes, how does it look like? If not, how to configure it through API?

2. Error messages and/or full log output:

No error message or log.

3. Caddy version:

2.8.4

4. How I installed and ran Caddy:

a. System environment:

Docker – through FrankenPHP image (1.2.5-php8.3), not sure if relevant but I thought I mention it anyway.

b. Command:

n/a

c. Service/unit/compose file:

n/a

d. My complete Caddy config:

{
	{$CADDY_GLOBAL_OPTIONS}

	admin off

	log default {
		output stdout
		format json
		level {$CADDY_SERVER_LOG_LEVEL:INFO}
	}

	frankenphp {
		{$FRANKENPHP_CONFIG}
	}
}

{$CADDY_EXTRA_CONFIG}

{$SERVER_NAME:localhost} {
	root * public/
	encode zstd br gzip

	vulcain

	{$CADDY_SERVER_EXTRA_DIRECTIVES}

	log {
		output stdout
		level {$CADDY_SERVER_ACCESS_LOG_LEVEL:INFO}

		format filter {
            wrap json

            fields {
                request>headers>Cf-Access-Jwt-Assertion replace REDACTED
                request>headers>Content-Security-Policy delete
                request>headers>Content-Security-Policy-Report-Only delete
            }
        }
	}

	# security headers
	header * {
		Permissions-Policy interest-cohort=() # disable FLoC tracking

		X-Content-Type-Options "nosniff"
		X-Frame-Options "DENY"
		X-XSS-Protection "0"
		Referrer-Policy "strict-origin-when-cross-origin"

		-X-Powered-By
		-Server
	}

	handle /version {
        respond "{\"version\":\"{$APP_VERSION}\",\"revision\":\"{$APP_REVISION}\"}"
        header Content-Type "application/json"
    }

	php_server
}

5. Links to relevant resources:

Thank you :pray:

You can use log_skip to mark specific requests as not getting logged. So you might do something like:

@not-health not path /healthz
log_skip @not-health

So that only your /healthz requests get logged.

Also you might want to look at log_name which can let you route request logs to different loggers by name, if you want them to get written to separate files or whatever.

But regarding sampling, I guess you’re right, we never wired that up in the Caddyfile. We do have sampling support in the JSON config though. It’s just not a very popular feature so I guess it just got forgotten.

I just opened an issue for that, if you want to take a shot at implementing it:

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.